Mybatis的CRUD
一. 注意点
1. namespace中的包名要和Dao/Mapper接口的包名一致
2. 执行增删改操作时需要提交事务!!!
3. 增删改操作返回值类型为int,查询的返回值类型是实体类
二、增删改查实现
1、 查询
1. 在Dao层接口中添加查询方法
1 2 3
| public interface UserMapper { List<User> getUserList(); }
|
2. 在Dao层的接口配置文件中配置查询语句 <select>标签
1 2 3
| <select id="getUserList" resultType="com.nichu.pojo.User"> select * from mybatis.user </select>
|
3. 编写测试程序
1 2 3 4 5 6 7 8 9 10 11
| @Test public void test(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> userList = mapper.getUserList(); for (User user : userList) { System.out.println(user); } sqlSession.close(); }
|
2、 增加
1. 在Dao层接口中添加增加操作方法
1 2 3 4
| public interface UserMapper { int addUser(User user); }
|
2. 在Dao层的接口配置文件中配置语句 <insert>标签
1 2 3 4
| <insert id="addUser" parameterType="com.nichu.pojo.User"> insert into mybatis.user (id, name, pwd) values (#{id},#{name},#{pwd}) </insert>
|
- **parameterType** 是传入的参数类型,指向实体类 **"com.nichu.pojo.User"**
- 使用 #{} 来获取传入的值
- #{},括号内是实体类属性/数据库字段
3. 编写测试程序
1 2 3 4 5 6 7 8
| public void test2(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.addUser(new User(5,"倪矗5","234333")); sqlSession.commit(); sqlSession.close(); }
|
一定要记得提交事务!!!
3、 修改
1. 在Dao层接口中添加修改操作方法
1 2 3 4
| public interface UserMapper { int updUser(User user); }
|
2. 在Dao层的接口配置文件中配置语句 <update>标签
1 2 3
| <update id="updUser" parameterType="com.nichu.pojo.User"> update mybatis.user set name=#{name}, pwd=#{pwd} where id =#{id} </update>
|
3. 编写测试程序
1 2 3 4 5 6 7 8 9
| @Test public void test4(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.updUser(new User(5,"倪矗6","234333")); sqlSession.commit(); sqlSession.close(); }
|
4、删除
1. 在Dao层接口中添加增加操作方法
1 2 3 4
| public interface UserMapper { int delUser(int id); }
|
2. 在Dao层的接口配置文件中配置语句 <delete>标签
1 2 3
| <delete id="delUser" parameterType="int"> delete from mybatis.user where id =#{id} </delete>
|
3. 编写测试程序
1 2 3 4 5 6 7 8 9
| @Test public void test3(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.delUser(3); sqlSession.commit(); sqlSession.close(); }
|
三、万能的Map
假设,我们的实体类,或者数据库中的表,字段或者参数过多,我们应当考虑使用Map!
1. 使用Map增加记录
1 2 3 4
| <insert id="addUser2" parameterType="map"> insert into mybatis.user (id,name,pwd) values (#{userId},#{userName},#{userPassword}); </insert>
|
1 2 3 4 5 6 7 8 9 10 11 12
| @Test public void test5(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); Map<String, Object> map = new HashMap<>(); map.put("userId",10); map.put("userName","Hello"); map.put("userPassword","222222"); mapper.addUser2(map); sqlSession.commit(); sqlSession.close(); }
|
2. 使用Map查找记录
1
| List<User> getUserListById(Map map);
|
1 2 3
| <select id="getUserListById" parameterType="map" resultType="com.nichu.pojo.User"> select * from mybatis.user where id = #{userId}; </select>
|
1 2 3 4 5 6 7 8 9 10
| @Test public void test6(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); Map<String, Object> map = new HashMap<String, Object>(); map.put("userId", 10); List<User> userListById = mapper.getUserListById(map); System.out.println(userListById); sqlSession.close(); }
|
Map传递参数,直接在sql中取出key即可!对象传递参数,直接在sql中取出对象的属性即可!只有一个参数的情况下,可以直接在sql中取。多个参数用Map,或者注解。
四、Mybatis模糊查询
方法1:在Java代码执行的时候,传递通配符% %
1
| List<User> userListById = mapper.getUserLike("%倪%");
|
1 2 3
| <select id="getUserLike" parameterType="String" resultType="com.nichu.pojo.User"> select * from mybatis.user where name like #{userName}; </select>
|
方法2:在sql拼接中使用通配符
1
| List<User> userListById = mapper.getUserLike("倪");
|
1 2 3
| <select id="getUserLike" parameterType="String" resultType="com.nichu.pojo.User"> select * from mybatis.user where name like "%"#{userName}"%"; </select>
|