mybatis 的crud及批量cud操作

君墨痕發表於2013-09-27

直接貼mapper的xml檔案和dao介面,具體使用方法前參照上一篇文章 http://blog.csdn.net/yangxuan0261/article/details/12092059

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語句類似下面,可以參照著片文章 http://blog.csdn.net/cyd1919/article/details/8088402

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)


相關文章