1.Mybatis中集合操作1.1业务说明需求查询id号1,2,4,5,7的数据Sql:select*fromdemo_userwhereidin(1,2,4,5,7…)1.
1. Mybatis中集合操作 1.1 业务说明 需求 查询id号 1,2,4,5,7的数据 Sql: select * from demo_user where id in (1,2,4,5,7…)
1.1 array集合操作 1.1.1 编辑测试代码 &#64;Test public void testFindIn ( ) { SqlSession sqlSession &#61; sqlSessionFactory. openSession ( ) ; UserMapper2 userMapper2 &#61; sqlSession. getMapper ( UserMapper2 . class ) ; int [ ] ids &#61; { 1 , 2 , 4 , 5 , 7 } ; List < User > userList &#61; userMapper2. findIn ( ids) ; System . out. println ( userList) ; sqlSession. close ( ) ; }
1.1.2 编辑Mapper接口
1.1.3 编辑Mapper.xml映射文件 < select id &#61; " findIn" resultType &#61; " User" > select * from demo_user where id in< foreach collection &#61; " array" open &#61; " (" close &#61; " )" separator &#61; " ," item &#61; " id" > #{id} foreach> select>
1.2 List集合操作 1.2.1 编辑测试代码 &#64;Test public void testFindInList ( ) { SqlSession sqlSession &#61; sqlSessionFactory. openSession ( ) ; UserMapper2 userMapper2 &#61; sqlSession. getMapper ( UserMapper2 . class ) ; Integer [ ] ids &#61; { 1 , 2 , 4 , 5 , 7 } ; List list &#61; Arrays . asList ( ids) ; List < User > userList &#61; userMapper2. findInList ( list) ; System . out. println ( userList) ; sqlSession. close ( ) ; }
1.2.2 编辑接口方法 List < User > findInList ( List list) ;
1.2.3 编辑Mapper映射文件 < select id &#61; " findInList" resultType &#61; " User" > select * from demo_user where id in< foreach collection &#61; " list" open &#61; " (" close &#61; " )" separator &#61; " ," item &#61; " id" > #{id} foreach> select>
1.3 Map集合操作 1.3.1 编辑测试类 &#64;Test public void testFindInMap ( ) { SqlSession sqlSession &#61; sqlSessionFactory. openSession ( ) ; UserMapper2 userMapper2 &#61; sqlSession. getMapper ( UserMapper2 . class ) ; int [ ] ids &#61; { 1 , 3 , 5 , 6 , 7 } ; String sex &#61; "男" ; List < User > userList &#61; userMapper2. findInMap ( ids, sex) ; System . out. println ( userList) ; sqlSession. close ( ) ; }
1.3.2 编辑Mapper接口
1.3.3 编辑Mapper xml映射文件 < select id &#61; " findInMap" resultType &#61; " User" > select * from demo_user where id in (< foreach collection &#61; " ids" item &#61; " id" separator &#61; " ," > #{id} foreach> )and sex &#61; #{sex} select>
2. 动态Sql 2.1 动态 sql-where-if 2.1.1 业务需求 说明: 用户传递了一个user对象, 要求根据user中不为null的属性查询数据. 例子1: User {name:“张三”} Sql: select * from demo_user where name &#61; “张三” 例子2: User {name:“张三”, age: 18} Sql: select * from demo_user where name &#61; “张三” and age&#61;18
2.1.2 编辑测试方法 &#64;Test public void testSqlWhere ( ) { User user &#61; new User ( null , "黑熊精" , 3000 , "男" ) ; SqlSession sqlSession &#61; sqlSessionFactory. openSession ( ) ; UserMapper2 userMapper2 &#61; sqlSession. getMapper ( UserMapper2 . class ) ; List < User > userList &#61; userMapper2. findSqlWhere ( user) ; System . out. println ( userList) ; sqlSession. close ( ) ; }
2.1.3 问题说明 User对象中的数据可能为null.但是如果sql不做处理,则将会把null当做参数.导致程序查询异常. 例子:
2.1.4 编辑mapper接口 List < User > findSqlWhere ( User user) ;
2.1.5 编辑xml映射文件 < select id &#61; " findSqlWhere" resultType &#61; " User" > select * from demo_user< where> < if test &#61; " id !&#61; null" > id &#61; #{id} if> < if test &#61; " name !&#61; null" > and name &#61; #{name} if> < if test &#61; " age !&#61; null " > and age &#61; #{age} if> < if test &#61; " sex !&#61; null " > and sex &#61; #{sex} if> where> select>
2.2 动态 sql-set-if 2.2.1 编辑测试类 &#64;Test public void testSqlSet ( ) { SqlSession sqlSession &#61; sqlSessionFactory. openSession ( true ) ; UserMapper2 userMapper &#61; sqlSession. getMapper ( UserMapper2 . class ) ; User user &#61; new User ( 1 , "守山使者" , 3000 , null ) ; int rows &#61; userMapper. updateSqlSet ( user) ; System . out. println ( "影响" &#43; rows&#43; "行" ) ; sqlSession. close ( ) ; }
2.2.2 编辑Mapper接口 int updateSqlSet ( User user) ;
2.2.3 编辑Mapper 映射文件 < update id &#61; " updateSqlSet" > update demo_user< set> < if test &#61; " name !&#61;null" > name&#61;#{name}, if> < if test &#61; " age !&#61;null" > age &#61; #{age}, if> < if test &#61; " sex !&#61;null" > sex &#61; #{sex} if> set> where id &#61; #{id} update>
2.3 动态 sql-分支结构语法 2.3.1 业务说明 需求: 根据属性查询数据, 如果name有值 按照name查询,否则按照年龄查询,如果name,age都没有 按照sex查询 需求分析:
if(name !&#61;null ){name &#61; #{name}}else if( age !&#61;null){age &#61; #{age}}else{sex &#61; #{sex}}
2.3.2 编辑测试方法 &#64;Test public void testChoose ( ) { SqlSession sqlSession &#61; sqlSessionFactory. openSession ( ) ; UserMapper2 userMapper &#61; sqlSession. getMapper ( UserMapper2 . class ) ; User user &#61; new User ( null , null , null , "男" ) ; List < User > userList &#61; userMapper. findChoose ( user) ; System . out. println ( userList) ; sqlSession. close ( ) ; }
2.3.3 编辑Mapper接口 List < User > findChoose ( User user) ;
2.3.4 编辑Mapper映射文件 < select id &#61; " findChoose" resultType &#61; " User" > select * from demo_user< where> < choose> < when test &#61; " name !&#61;null" > name &#61; #{name} when> < when test &#61; " age !&#61;null" > age &#61; #{age} when> < otherwise> sex &#61; #{sex} otherwise> choose> where> select>
3. resultMap说明 3.1 创建dog表 表结构
表名
3.2 构建POJO对象 &#64;Data &#64;Accessors ( chain &#61; true ) &#64;NoArgsConstructor &#64;AllArgsConstructor public class Dog implements Serializable { private Integer dogId; private String dogName; }
3.3 编辑测试类 public class TestDog { private SqlSessionFactory sqlSessionFactory; &#64;BeforeEach public void init ( ) throws IOException { String resource &#61; "mybatis/mybatis-config.xml" ; InputStream inputStream &#61; Resources . getResourceAsStream ( resource) ; sqlSessionFactory &#61; new SqlSessionFactoryBuilder ( ) . build ( inputStream) ; } &#64;Test public void testResultMap ( ) { SqlSession sqlSession &#61; sqlSessionFactory. openSession ( ) ; DogMapper dogMapper &#61; sqlSession. getMapper ( DogMapper . class ) ; List < Dog > dogList &#61; dogMapper. findAll ( ) ; System . out. println ( dogList) ; sqlSession. close ( ) ; } }
3.4 编辑Mapper接口 public interface DogMapper { List < Dog > findAll ( ) ; }
3.5 resultType 和resultMap的区别 DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > < mapper namespace &#61; " com.jt.mapper.DogMapper" > < select id &#61; " findAll" resultMap &#61; " dogRM" > select * from dog select> < resultMap id &#61; " dogRM" type &#61; " com.jt.pojo.Dog" > < id column &#61; " dog_id" property &#61; " dogId" /> < result column &#61; " dog_name" property &#61; " dogName" /> resultMap> mapper>
4 关于Mybatis 注解开发说明 4.1 编辑测试方法 &#64;Test public void testAnno ( ) { SqlSession sqlSession &#61; sqlSessionFactory. openSession ( ) ; UserAnnoMapper userAnnoMapper &#61; sqlSession. getMapper ( UserAnnoMapper . class ) ; List < User > userList &#61; userAnnoMapper. findAll ( ) ; System . out. println ( userList) ; sqlSession. close ( ) ; }
4.2 编辑注解的接口 public interface UserAnnoMapper { &#64;Select ( "select * from demo_user" ) List < User > findAll ( ) ; }
4.3 Mybatis管理接口 < mappers> < mapper resource &#61; " mybatis/mappers/UserMapper.xml" /> < mapper resource &#61; " mybatis/mappers/UserMapper2.xml" /> < mapper resource &#61; " mybatis/mappers/DogMapper.xml" /> < mapper class &#61; " com.jt.mapper.UserAnnoMapper" > mapper> mappers>
4.4 关于Mybatis的注解开发说明 注解开发 只适用于 单表CURD操作. 多表操作一定出问题 如果设计到复杂标签时 where/set/foreach 等标签时,不可以使用注解. 所以应该熟练掌握xml映射文件的写法,注解开发只是辅助的作用. 5. 总结 Mybatis 的集合操作 1.1 数组 foreach collection&#61;“array” 1.2 list集合 foreach collection&#61;“list” 1.3 Map集合 foreach collection&#61;“map中的KEY” foreach 用法 1.collection 表示遍历的集合类型 1.1 数组 关键字 array 1.2 List集合 关键字 list 1.3 Map集合 关键字 Map中的key 2.open 循环开始标签,close 循环结束标签 包裹循环体 3.separator 分割符 4.item 当前循环遍历的数据的变量 动态Sql 1.where if < select id &#61; " findSqlWhere" resultType &#61; " User" > select * from demo_user< where> < if test &#61; " id !&#61; null" > id &#61; #{id} if> < if test &#61; " name !&#61; null" > and name &#61; #{name} if> < if test &#61; " age !&#61; null " > and age &#61; #{age} if> < if test &#61; " sex !&#61; null " > and sex &#61; #{sex} if> where> select>
2.set-if set标签用法: 去除set条件中多余的,号 3.choose-when-otherwise 标识都是互斥事件,只有一个有效. 4.resultMap 字段与属性名称不一致时,使用resultMap 5.Mybatis的注解开发 &#64;insert &#64;update &#64;delete &#64;select