mybatis 的crud及批量cud操作
直接貼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)
相關文章
- spring 整合 mybatis 及mybatis 的 crud 操作SpringMyBatis
- Mybatis:CRUD操作及配置解析MyBatis
- MyBatis 的簡單 CRUD 操作MyBatis
- mybatis批量操作MyBatis
- Mybatis批量操作demoMyBatis
- MyBatis-Plus:簡化 CRUD 操作的藝術MyBatis
- ES 筆記四:文件的基本 CRUD 與批量操作筆記
- mybatis CRUDMyBatis
- Mybatis-02 CRUDMyBatis
- MyBatis-03(CRUD)MyBatis
- JPA和mybatis的CRUD比較MyBatis
- 二叉排序樹BST及CRUD操作排序
- 2. MyBatis-CRUDMyBatis
- Spring Boot整合Mybatis完成級聯一對多CRUD操作Spring BootMyBatis
- 3. 使用Mybatis完成CRUDMyBatis
- Elasticsearch CRUD基本操作Elasticsearch
- go操作mongo CRUDGo
- 【mybatis-plus】CRUD必備良藥,mybatis的好搭檔MyBatis
- 07-SpringBoot+MyBatis+Spring 技術整合實現商品模組的CRUD操作Spring BootMyBatis
- JPA工程的建立和CRUD操作
- MyBatis框架介紹及實戰操作MyBatis框架
- MyBatis 批量插入資料MyBatis
- Mybatis批量插入Oracle、MySQLMyBatisOracleMySql
- 使用PreparedStatement實現CRUD操作
- Java Hibernate 之 CRUD 操作Java
- MyBatis框架搭建及增刪改查操作MyBatis框架
- Spring-Mybatis的批量執行SpringMyBatis
- Transact-SQL系列: 單表的CRUD操作SQL
- Mybatis批量更新三種方式MyBatis
- ORACLE批量操作Oracle
- mybatis中使用foreach構造多like查詢及批量插入MyBatis
- Mybatis20_mybatis的多表操作8MyBatis
- MongoDB 4.X CRUD基本操作MongoDB
- 【MyBatis】幾種批量插入效率的比較MyBatis
- Java 8 Streams 中的資料庫 CRUD 操作Java資料庫
- DocumentFragment批量操作domFragment
- JPA之使用JPQL進行CRUD操作
- Spring Boot+MiniUI CRUD操作Spring BootUI