您的位置:首页 > 其它

mybatis 的crud及批量cud操作

2013-09-27 23:47 417 查看
直接贴mapper的xml文件和dao接口,具体使用方法前参照上一篇文章 /article/9188299.html

bookMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC
"-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!--这块等于dao接口的实现 namespace必须和接口的类路径一样 -->
<mapper namespace="com.yang.dao.BookMapper">

<!-- id必须和接口中的方法名一样 返回一个Books 就是刚才的别名 如果不弄别名要连类路径一起写 麻烦 -->
<!-- 根据id查找一个Books对象,返回 Books -->
<select id="findById" parameterType="int" resultType="Books">
select
*
from books where book_id=#{book_id}
</select>

<!-- 根据book_name查找多个Books对象,返回 list -->
<select id="findByName" parameterType="string" resultType="Books">
select
*
from books where book_name like #{book_name}
</select>

<!-- insert一个Books对象,如果执行成功返回1,否则0 -->
<insert id="save" parameterType="Books" useGeneratedKeys="true"
keyProperty="book_id">
insert into books(book_name,book_author,book_date)
values(#{book_name},#{book_author},#{book_date})
</insert>

<!-- update一个Books对象,如果执行成功返回1,否则0 -->
<update id="update" parameterType="Books">
update books set
book_name=#{book_name},book_author=#{book_author},book_date=#{book_date}
where book_id=#{book_id}
</update>

<!-- delete一个Books对象,如果执行成功返回1,否则0 -->
<delete id="delete" parameterType="int">
delete from books where
book_id=#{book_id}
</delete>

<!-- 根据多个查询参数,查找多个Books对象,返回 list -->
<select id="findByArgs" parameterType="map" resultType="Books">
select
*
from books where book_name like #{book_name}
<if test="book_publish!=null">
and book_publish like #{book_publish}
</if>
</select>

<!-- batch operation -->

<!-- 批量insert多个对象,如果执行成功返回批量个数 -->
<insert id="saveBatch" parameterType="ArrayList">
insert into books(book_name,book_author,book_date)
values
<foreach collection="list" item="book" index="index"
separator=",">
(#{book.book_name},#{book.book_author},#{book.book_date})
</foreach>
</insert>

<!-- 批量delete多个对象,如果执行成功返回批量个数 -->
<delete id="deleteBatch" parameterType="ArrayList">
delete from books where
book_id in
<foreach collection="list" item="id" index="index" open="("
separator="," close=")">
#{id}
</foreach>
</delete>

<!-- 批量update多个对象,如果执行成功返回批量个数 ,这个批量update比较复杂一点 -->
<update id="updateBatch">
update books
<trim prefix="set" suffixOverrides=",">
<trim prefix="book_name=case" suffix="end,">
<foreach collection="list" item="book" index="index">
<if test="book.book_name!=null">
when (book_id=#{book.book_id}) then #{book.book_name}
</if>
</foreach>
</trim>
<trim prefix=" book_author=case" suffix="end,">
<foreach collection="list" item="book" index="index">
<if test="book.book_author!=null">
when (book_id=#{book.book_id}) then #{book.book_author}
</if>
</foreach>
</trim>
<trim prefix="book_date=case" suffix="end,">
<foreach collection="list" item="book" index="index">
<if test="book.book_date!=null">
when (book_id=#{book.book_id}) then #{book.book_date}
</if>
</foreach>
</trim>
</trim>
where
<foreach collection="list" item="book" index="index"
separator="or">
book_id=#{book.book_id}
</foreach>
</update>

</mapper>


BookMapper

public interface BookMapper {

public Books findById(int book_id);

public List<Books> findByName(String book_name);

public int save(Books books);

public int update(Books books);

public int delete(int book_id);

public List<Books> findByArgs(Map<String, String> map);

public int saveBatch(List<Books> books);

public int deleteBatch(List<Integer> ids);

public int updateBatch(List<Books> books);

}


ok,那个批量更新花了稍长一点时间去弄明白,需要多看几下。

执行的sql语句类似下面,可以参照着片文章 /article/10113153.html

Sql:
update tblsupertitleresult set result =case
when (userHhCode=2001 and titleId=1)then  90
when (userHhCode=2001 and titleId=2)then  70
end
,checkState = case
when (userHhCode=2001 and titleId=1)then  80
when (userHhCode=2001 andtitleId=2)then  120
end
where (userHhCode=2001 and titleId=1) or(userHhCode=2001 and titleId=2)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: