mybatis 的crud及批量cud操作
2013-09-27 23:47
417 查看
直接贴mapper的xml文件和dao接口,具体使用方法前参照上一篇文章 /article/9188299.html
bookMapper.xml
BookMapper
ok,那个批量更新花了稍长一点时间去弄明白,需要多看几下。
执行的sql语句类似下面,可以参照着片文章 /article/10113153.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)
相关文章推荐
- 008 使用MyBatis,easyUI实现CRUD操作样例-CUD(CRUD完毕)
- 【Mybatis】常见的crud操作
- MyBatis入门学习教程-使用MyBatis对表执行CRUD操作
- mybatis入门-实现基本CRUD操作
- MyBatis 批量操作
- MyBatis 学习总结(二)——MyBatis 实现 crud 操作
- Mybatis 批量操作
- spring 集成 mybatis 及mybatis 的 crud 操作
- 数据库--MyBatis的(insert,update,delete)三种批量操作
- MyBatis批量操作报错:Parameter 'xxxList' not found. Available parameters are [list]
- MyBatis传参方式和批量操作总结
- Mybatis(三):批量操作
- 使用MyBatis对标进行CRUD操作
- 数据库--MyBatis的(insert,update,delete)三种批量操作
- MyBatis学习总结(10)——批量操作
- MyBatis:Insert操作详解(返回主键、批量插入)
- Mybatis批量修改的操作代码
- MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
- mybatis的批量操作
- MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作