MyBatis(二) MyBatis的关联映射和动态SQL
2017-07-06 16:30
489 查看
一、关联映射
1:一对一
1)在需要关联的实体对象中引用另外一个对象
2)在xml中的的<resultMap>引用进来:
<association property="card" column="card_id" select="xx.xx.xx.CardMapper.selectCardById" javaType="xx.xx.xx.Card"/>
select属性表示会使用column属性的card_id值作为参数执行CardMapper中定义的selectCardById
查询对应的Card数据,查询出的数据被封装到引用的对象中。
2:一对多
1)在需要关联的实体对象中引用另外的对象封装成List,例如:
private List<Student> students; get、set方法
2)在xml中的的<resultMap>引用进来:
<collection property="students" javaType="ArrayList"column="id" ofType="xx.xx.xx.Student"
select="xx.xx.mapper.StudentMapper.selectStudentByClazzId" fechType="lazy">
这里加载需要的字段
</collection>fetchType有两个值 eager、lazy 表示立即加载和懒加载。
3)需要在mybatis-config.xml中增加如下配置:
aggressiveLazyLoading:启用时,会使带有延迟加载属性的对象立即加载;反之,每种属性将会按需加载。默认true。
3:多对一
跟一对多差不多
<collection property="students" javaType="xx.xx.xx.Student">
这里加载需要的字段
</collection>
4:多对多
1)在需要关联的实体对象中引用另外一个对象,例如:private List<Student> students; get、set方法
2)在xml中的的<resultMap>引用进来:
3)一般多对多查询需要借助中间表来关联
二、动态SQL
1:if
2:choose
表示: if else if else
3:where
在<if>标签外面加用于判断语句
4:set:
可以被用于动态包含需要更新的列,而舍去其他的。
5:foreach
6:bind
表示创建一个变量然后绑定到上下文中
1:一对一
1)在需要关联的实体对象中引用另外一个对象
2)在xml中的的<resultMap>引用进来:
<association property="card" column="card_id" select="xx.xx.xx.CardMapper.selectCardById" javaType="xx.xx.xx.Card"/>
select属性表示会使用column属性的card_id值作为参数执行CardMapper中定义的selectCardById
查询对应的Card数据,查询出的数据被封装到引用的对象中。
2:一对多
1)在需要关联的实体对象中引用另外的对象封装成List,例如:
private List<Student> students; get、set方法
2)在xml中的的<resultMap>引用进来:
<collection property="students" javaType="ArrayList"column="id" ofType="xx.xx.xx.Student"
select="xx.xx.mapper.StudentMapper.selectStudentByClazzId" fechType="lazy">
这里加载需要的字段
</collection>fetchType有两个值 eager、lazy 表示立即加载和懒加载。
3)需要在mybatis-config.xml中增加如下配置:
<settings> <setting name="lazyLoadingEnabled" value="true"/> setting name="aggressiveLazyLoading" value="false"/> </settings>lazyLoadingEnabled:表示延迟加载的全局开关,当开启时所有关联对象都会延迟加载。默认false。
aggressiveLazyLoading:启用时,会使带有延迟加载属性的对象立即加载;反之,每种属性将会按需加载。默认true。
3:多对一
跟一对多差不多
<collection property="students" javaType="xx.xx.xx.Student">
这里加载需要的字段
</collection>
4:多对多
1)在需要关联的实体对象中引用另外一个对象,例如:private List<Student> students; get、set方法
2)在xml中的的<resultMap>引用进来:
<collection property="articles" javaType="ArrayList" column="oid" oftype="xx.xx.xx.Article" select="xx.xx.mapper.ArticleMapper.selectArticleByOrderId" fetchType="lazy"> 这里配置需要加载的字段 </collection><!--如果查询出来的列同名,则需要使用别名来区分,在resultMap 里面更改别名-->
3)一般多对多查询需要借助中间表来关联
二、动态SQL
1:if
<if test="name != null and name != ''" > </if>
2:choose
<choose> <when test="id != null"></when> <when test="variety_id!=1"></when> <otherwise> and id=1 </otherwise> </choose>
表示: if else if else
3:where
在<if>标签外面加用于判断语句
4:set:
可以被用于动态包含需要更新的列,而舍去其他的。
<set> <if test="userId != null"> user_id = #{userId,jdbcType=BIGINT}, </if> <if test="name != null"> name = #{name,jdbcType=VARCHAR}, </if> </set>
5:foreach
bd54 <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach>
6:bind
表示创建一个变量然后绑定到上下文中
<select id="findAnalystPageList" resultType="com.bob.analyst.vo.AnalystVO" parameterType="com.bob.analyst.vo.AnalystVO"> <bind name="name" value="'%'+_parameter.getName()+'%'"/> select <include refid="Base_Column_List" /> from tbl_analyst where is_delete=0 <if test="name != null and name != ''" > and name like #{name} </if> ORDER BY create_time desc </select>
相关文章推荐
- Mybatis 动态SQL和关联映射
- MyBatis-动态SQL、关联映射、MyBatis整合Spring
- MyBatis学习总结(二)表的关联以及动态SQL
- MyBatis(三)配置文件,输入输出映射,动态SQL
- Mybatis (五)输入映射 HashMap输出 传递pojo包装 resultType resultMap 动态sql
- mybatis(六) 输入映射 输出映射 resultType:resultMap 动态sql sql片段 if判断 foreach
- Mybatis高级映射、动态SQL及获得自增主键的解析
- mybatis之入门到开发(三)之常用对象SqlSessionFactory和SqlSession,动态sql,输入映射和输出映射
- MyBatis框架的学习(四)——Mapper.xml文件中的输入和输出映射以及动态sql
- mybatis学习四之输入输出映射以及动态sql
- Mybatis学习(4)输入映射、输出映射、动态sql
- MyBatis框架的学习(四)——Mapper.xml文件中的输入和输出映射以及动态sql
- 小峰mybatis(5)mybatis使用注解配置sql映射器--动态sql
- mybatis映射文件与动态sql
- Mybatis高级映射、动态SQL及获得自增主键
- Mybatis输入输出映射及动态SQL Review
- mybatis 动态sql语句 输出映射
- MyBatis输入映射、输出映射、动态SQL、关联关系、Spring集成加强笔记
- mybatis-动态sql(foreach),映射
- Mybatis动态Sql映射标签总结