1 MyBatics
定义Pojo
写CRUD的XML,或者接口
注册到全局文件中
2 查询
业务逻辑和SQL的耦合
<!--单条件判断-->
<select id="selectReaderMoney" resultType="jikeReader" parameterType="jikeReader">
select * from reader
where 1=1
<if test="money!=null">
and money>#{money}
</if>
</select>
<!--多条件的联合判断-->
<select id="selectJiKeUserChoose" resultType="JiKeUser" parameterType="JiKeUser">
select * from jikeuser where 1=1
<choose>
<when test="userName!=null">
and userName like #{userName}
</when>
<when test="id!=0">
and id =#{id}
</when>
<otherwise>
and password is not null
</otherwise>
</choose>
</select>
<!--where条件 第一ifwhere会自行判定是否有and-->
<select id="selectJiKeUserWhere" resultType="JiKeUser" parameterType="JiKeUser">
select * from jikeuser
<where>
<if test="userName!=null">
and userName like #{userName}
</if>
<if test="id!=null">
and id =#{id}
</if>
</where>
</select>
<!-- foreach 循环查询 (将集合转化in中的条件)-->
<select id="selectJiKeUserForeach" resultType="JiKeUser" parameterType="list">
select * from jikeuser
<where>
id in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</where>
</select>
连表查询
<resultMap id="JiKeAuthorMapByCon" type="Author">
<id property="id" column="author.id" />
<result property="realName" column="realName" />
<result property="IDCard" column="IDCard" />
<association property="jikeUser" column="userID" javaType="JiKeUser">
<constructor>
<arg column="userName" javaType="String" />
<arg column="password" javaType="String" />
</constructor>
</association>
</resultMap>
<select id="selectAuthorJoin" resultMap="JiKeAuthorMapByCon">
select * from author inner join jikeUser
on jikeuser.id=author.userID
</select>
懒加载配置
<select id="findById" parameterType="int" resultType="jike.book.pojo.JiKeUser">
select * from jikeUser where id=#{id}
</select>
<!--标明了连接条件?但是有多条件关联呢-->
<resultMap id="JiKeAuthorSubMap" type="Author">
<id property="id" column="author.id" />
<result property="realName" column="realName" />
<result property="IDCard" column="IDCard" />
<association property="jikeUser" column="userID"
javaType="JiKeUser" select="findById">
</association>
</resultMap>
<select id="selectAuthor" resultMap="JiKeAuthorSubMap">
select * from author
</select>
注:
不明白的地方:1,association 和collector的区别 2 鉴别器
3 更新
<update id="updateJiKeUserSet" parameterType="JiKeUser">
update JiKeUser
<set>
<if test="userName != null">userName=#{userName},</if>
<if test="password != null">password=#{password},</if>
</set>
where id=#{id}
</update>
<!--trim格式化标记(当然也可以替代很多choose,where and so on)-->
<update id="updateUserTrim" parameterType="JiKeUser">
UPDATE JiKeUser
<trim prefix="SET" suffixOverrides="," suffix="WHERE id = #{id}" >
<if test="userName != null and userName != '' ">
userName = #{userName},
</if>
<if test="password != null and password != '' ">
password=#{password},
</if>
</trim>
</update>
4 多条插入
<insert id="insertJiKeUserForeach">
insert into jikeUser (userName, password) values
<foreach item="item" index="key" collection="list"
open="" separator="," close="">(#{key}, #{item.password})
</foreach>
</insert>