Mybatis框架学习笔记 lesson2
2016-09-07 09:55
260 查看
Mybatis 分页
封装相关数据成一个类public class Page {
private int PageNo; 页号
private int PageSize; 每页数量
private int PageStart; 起始页
private String orderByField; 排序条件
private String orderByDescAndAsc; 升序降序
}
DeptMapper
<select id="selectPage" resultType="d" parameterType="page">
select * from dept order by #{orderByField} desc limit #{pageStart},#{pageSize}
</select>
动态sql (条件查询)
在Page内添加查询条件属性private Dept condition
DeptMapper
<select id="selectPageBySql" resultType="d" parameterType="page">
select * from dept
<where>
<if
test="condition!=null">
<if test="condition.dname!=null
and condition.dname!=''">
dname like #{condition.dname}
</if>
<if test="condition.loc!=null and condition.loc!=''">
loc like #{condition.loc}
</if>
</if>
</where>
order by #{orderByField} desc limit #{pageStart},#{pageSize}
</select>
sql片段
<sql id="myCondition1"><if test="condition!=null">
<if test="condition.dname!=null and condition.dname!=''">
and dname like #{condition.dname}
</if>
<if test="condition.loc!=null and condition.loc!=''">
and loc like #{condition.loc}
</if>
</if>
</sql>
需要sql片段的地方:
<include refid="sql片段的id"><include refid="myCondition1"/>
多表查询下的mybatis配置
OneToOne<resultMap type="Order"
id="mymap">
<id property="id" column="oid"/>
<result property="create_date" column="create_date"/>
<result property="num" column="num"/>
<result property="custom_id" column="custom_id"/>
<!-- 配置一个订单属于一个客户的 -->
<association property="custom" javaType="custom">
<id column="cid" property="id"/>
<result column="cname" property="cname"/>
<result column="age" property="age"/>
</association>
</resultMap>
<select id="selectOrderAndCustom2" resultMap="mymap">
SELECT o.id oid,o.create_date,o.num,o.custom_id, c.id cid,c.cname,c.age FROM orders o, custom c
WHERE o.custom_id=c.id
</select>
OneToMany
<resultMap type="Dept" id="mymap2">
<id column="dno" property="dno">
<result column="dname" property="dname">
<result column="loc" property="loc">
<collection
property="emps" ofType="Emp">
<id column="empno" property="empno"/>
<result column="ename" property="ename">
.....
</collection>
相关文章推荐
- Mybatis框架学习笔记 lesson1
- mybatis学习笔记之基础框架(2)
- springMVC学习笔记---day02 springMVC+spring+mybatis整合开发框架搭建
- Mybatis框架学习笔记
- JavaWeb学习笔记-mybatis-02-mybatis框架
- Mybatis框架学习笔记一
- mybatis学习笔记 一对一 和一对多 以及 如何通过框架直接返回主键id
- JAVA学习笔记19——MyBatis框架第二章
- Spring学习笔记----三大框架(Spring+SpringMVC+MyBatis)整合详细教程
- mybatis框架总体说明---Mybatis学习笔记(二)
- spring+mybatis轻量级企业框架的学习之路之错误笔记day04
- JAVA学习笔记20——MyBatis框架第三章
- springMVC+Mybatis 学习笔记-【1】Mybatis的框架原理
- [知了堂学习笔记]_mybatis_02如何快速搭建mybatis框架之一
- Maven学习笔记(五)-Maven整合SSM(spring+springMVC+Mybatis)框架
- [知了堂学习笔记]_mybatis_03如何快速搭建mybatis框架之二
- Spring+SpringMVC+Mybatis框架整合例子(SSM) 学习笔记
- JAVA学习笔记18——MyBatis框架第一章
- J2EE系列之MyBatis学习笔记(十三)-- SSM框架搭建
- MyBatis 学习笔记之Logging -【使用日志框架是有根据顺序选择的】