<insert id="doCreate" parameterType="News"><!--添加数据-->
INSERT INTO news(title,content) VALUES (#{title},#{content}) ;
<selectKey keyColumn="nid" keyProperty="nid" order="AFTER" resultType="java.lang.Long">
SELECT LAST_INSERT_ID()
</selectKey>
</insert>
<update id="doEdit" parameterType="News"> <!--更新数据-->
UPDATE news SET title=#{title},content=#{content} WHERE nid=#{nid}
</update>
<delete id="doRemove" parameterType="java.lang.Long"><!--删除数据-->
DELETE FROM news WHERE nid=#{xiaoyu}
</delete>
查询数据(添加到News.xml)
<select id="findById" parameterType="java.lang.Long" resultType="News"><查询数据>
SELECT nid,title,content FROM news WHERE nid=#{wodenid} ;
</select>
<select id="findAll" resultType="News">
SELECT nid,title,content FROM news ;
</select>
<select id="findAllTitle" resultType="News">
SELECT nid,title FROM news ;
</select>
<select id="findSplit" resultType="News" parameterType="java.util.Map"> <分页查询>
SELECT nid,title,content FROM news WHERE ${column} LIKE #{keyword} LIMIT #{start},#{lineSize} ;
</select>
<select id="getAllCount" resultType="java.lang.Long">
SELECT COUNT(*) FROM news ;
</select>
数据更新测试
1 @Test 2 public void testDelete() throws Exception { //删除 3 System.out.println("数据删除:" + MyBatisSessionFactory.getSession() 4 .update("com.lion.mapper.NewsNS.doRemove", 9L)); 5 MyBatisSessionFactory.getSession().commit(); 6 MyBatisSessionFactory.close(); 7 } 8 @Test 9 public void testEdit() throws Exception { //修改 10 News vo = new News(); 11 vo.setTitle("人生最舒服的日子?"); 12 vo.setContent("你是个屌丝,梦里成为富翁"); 13 vo.setNid(9L); // 修改数据时需要设置准确的编号 14 System.out.println("数据修改:" + MyBatisSessionFactory.getSession().update("com.lion.mapper.NewsNS.doEdit", vo)); 15 MyBatisSessionFactory.getSession().commit(); 16 MyBatisSessionFactory.close(); 17 MyBatisSessionFactory.getSession().commit(); 18 MyBatisSessionFactory.close(); 19 } 20 @Test 21 public void testSplit() throws Exception { //分页查询 22 Map<String,Object> splitParams = new HashMap<String,Object>() ; // 保存分页相关参数 23 splitParams.put("column", "title"); 24 splitParams.put("keyword", "%%"); 25 splitParams.put("start", 5); // (currentPage - 1) * lineSize 26 splitParams.put("lineSize", 5); 27 List<News> all = MyBatisSessionFactory.getSession().selectList("com.lion.mapper.NewsNS.findSplit",splitParams); 28 Long count = MyBatisSessionFactory.getSession().selectOne("com.lion.mapper.NewsNS.getAllCount",splitParams); 29 System.out.println("【分页数据】" + all); 30 System.out.println("【数据行统计】" + count); 31 MyBatisSessionFactory.close(); 32 }