您的位置:首页 > 其它

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: