zoukankan      html  css  js  c++  java
  • MyBatis案例

    1.查询分页数据:

    a.使用注解
    mapper:

     <select id="selectBlogByPage2" resultMap="blogResultMap">
            select * from blog limit #{offset},#{pagesize}
     </select>
    

      接口:注解的value值要和mapper的占位参数一致。

    List<Blog> selectBlogByPage2(@Param(value="offset") int offset, @Param(value="pagesize") int pagesize);
    

    b.使用Map

    mapper中的参数占位符要和测试中的map中的key一一对应

    mapper:

     <select id="selectBlogByPage2" resultMap="blogResultMap">
            select * from blog limit #{offset},#{pagesize}
         </select>  

    接口:

    List<Blog> selectBlogByPage3(Map<String,Object> map);

    测试:

            @Test
    	public void testSelectBlogBySort(){
    		SqlSession session=MyBatisUtil.getSqlSession();
    		BlogMapper blogMapper=session.getMapper(BlogMapper.class);
    		Map<String,Object> map=new HashMap<String,Object>();
    		map.put("offset", 2);
    		map.put("pagesize", 3);
    		List<Blog> blogList=blogMapper.selectBlogByPage3(map);
    		session.close();
    		
    		System.out.println(blogList);
    	}
    	
    

    2.插入-获取刚刚插入的id(自增id) 

    方式一:在mapper中配置 insert节点的属性 useGeneratedKeys="true" keyProperty=" "

        <insert id="insertBlog" parameterType="Blog"  useGeneratedKeys="true" keyProperty="id">
             insert into blog
             (title,author_id,state,featured,style)
             values(
               #{title},#{authorId},#{state},#{featured},#{style}
             )
         </insert>  

    方式二:

    在全局配置文件配置settings选项

     <settings>
        <setting name="useGeneratedKeys" value="true"/>
      </settings>  

    并且在mapper的insert节点配置属性 keyProperty=" "

    方式三:适用于没有自增属性的数据库

    <insert id="insertmMysql" parameterType="Blog">
           <selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id">
              SELECT LAST_INSET_ID()
           </selectKey>
             insert into blog
             (title,author_id,state,featured,style)
             values(
               #{title},#{authorId},#{state},#{featured},#{style}
             )
         
         </insert>
    

    3.批量删除
    mapper:

        <delete id="deleteBlogList" parameterType="java.util.List">
            delete from blog where id in
            <foreach collection="list" item="item" open="(" close=")" separator=",">
               #{item}
            </foreach>
         </delete>
           

    接口:

    int deleteBlogList(List<Integer> ids)  

    测试:

            @Test
    	public void testDeleteBlogList(){
    		SqlSession session=MyBatisUtil.getSqlSession();
    		BlogMapper blogMapper=session.getMapper(BlogMapper.class);
    		
    		List<Integer> ids=Arrays.asList(4,5,7);
    		int blogList=blogMapper.deleteBlogList(ids);
    		
    		session.commit();
    		session.close();
    		
    		System.out.println(blogList);
    	}
    

      

     


     

  • 相关阅读:
    JavaScript (new)创建一个对象的过程
    我所理解的promise的then方法
    promise与async和await的区别
    JS Promise实例 then()方法
    电脑上不去网
    es6----set map应用场景
    ES6新特性一览
    es6继承
    es6中class类的全方面理解(一)
    时间处理金字教程
  • 原文地址:https://www.cnblogs.com/jyy599/p/12548215.html
Copyright © 2011-2022 走看看