上篇博客部署了我的第一个Mybatis项目,并且成功查询了表中的所有数据,增删改的操作其实和查询大同小异。
1、在接口中定义相关的方法
package demo.dao; import demo.pojo.User; import java.util.List; import java.util.Map; public interface UserMapper { //查询所有用户 List<User> getUserList(); //根据id查询用户 User getUserById(int id); //插入一个用户 int insertUser(User user); // int insertUser2(Map<String,Object> map); //根据id修改用户 int updateUser(User user); //根据删除一个用户 int deleteUser(int id); }
2、在对应xml文件中实现
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="demo.dao.UserMapper"> <!-- select all users--> <select id="getUserList" resultType="demo.pojo.User"> select * from user </select> <!-- select one user by its id--> <select id="getUserById" parameterType="int" resultType="demo.pojo.User" > select * from user where id=#{id} </select> <!-- insert a user--> <insert id="insertUser" parameterType="demo.pojo.User"> insert into user (username,sex,address) value (#{username},#{sex},#{address}) </insert> <!-- <insert id="insertUser2" parameterType="map">--> <!-- insert into user (username,sex,address) value (#{username},#{sex},#{address})--> <!-- </insert>--> <!-- update a user by its id--> <update id="updateUser" parameterType="demo.pojo.User"> update user set username=#{username},sex=#{sex},address=#{address} where id=#{id} </update> <!-- delete a user by its id--> <delete id="deleteUser" parameterType="int"> delete from user where id=#{id} </delete> </mapper>
namespace对应要操作的接口名,id对应要实现的方法名,parameterType为传入的参数类型,resultType为返回的结果类型
3、可以写一个测试类测试了
package demo.dao; import demo.pojo.User; import demo.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class UserDaoTest { /** * 测试 查询所用用户 */ @Test public void test(){ SqlSession sqlSession = null; try{ sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> userList = mapper.getUserList(); System.out.println(userList); }catch (Exception e){ e.printStackTrace(); } finally { sqlSession.close(); } } /** * 测试 根据id查询用户 */ @Test public void test2(){ SqlSession sqlSession = null; try{ sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.getUserById(45); System.out.println(user); } catch (Exception e) { e.printStackTrace(); } finally { sqlSession.close(); } } /** * 测试 插入一个用户 * 增删改需要提交事务 */ @Test public void test3(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = new User(); user.setUsername("王老五"); user.setAddress("深圳"); user.setSex("男"); int result = mapper.insertUser(user); //提交事务 sqlSession.commit(); System.out.println(result); sqlSession.close(); } /** * 测试 根据id修改用户 */ @Test public void test4(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); int result = mapper.updateUser(new User(48, "李老六", "", "男", "广州")); sqlSession.commit(); System.out.println(result); sqlSession.close(); } /** * 测试 根据id删除用户 */ @Test public void test5(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); int result = mapper.deleteUser(43); System.out.println(result); sqlSession.commit(); sqlSession.close(); } }