Mybatis的CRUD
1.修改配置文件Category.xml,提供CRUD对应的sql语句。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <mapper namespace="mybatis.pojo"> 7 <insert id="addCategory" parameterType="Category"> 8 insert into category ( name ) values (#{name}) 9 </insert> 10 11 <delete id="deleteCategory" parameterType="Category"> 12 delete from category where id= #{id} 13 </delete> 14 15 <select id="getCategory" parameterType="_int" resultType="Category"> 16 select * from category where id= #{id} 17 </select> 18 19 <update id="updateCategory" parameterType="Category"> 20 update category set name=#{name} where id=#{id} 21 </update> 22 <select id="listCategory" resultType="Category"> 23 select * from category 24 </select> 25 </mapper>
2.增加(C)
向数据库中插入10条数据
1 private void Myinset() throws IOException { 2 create(); 3 for (int i = 0; i < 10; i++) { 4 Category category = new Category(); 5 category.setName("xiaomi" + i); 6 session.insert("addCategory", category); 7 } 8 listAll(session); 9 close(); 10 }
通过session.insert调用addCategory对应的SQL语句,addCategory对应的插入sql语句,#{name}会自动获取category对象的name属性值
1 <insert id="addCategory" parameterType="Category"> 2 insert into category ( name ) values (#{name}) 3 </insert>
3.删除(D)
删除id=6的对象
1 private void Mydelete() throws IOException { 2 create(); 3 Category category = new Category(); 4 category.setId(6); 5 session.delete("deleteCategory", category); 6 listAll(session); 7 close(); 8 }
通过session.delete调用deleteCategory对应的SQL语句,deleteCategory对应删除sql语句,#{id}会获取category对象的id。
1 <delete id="deleteCategory" parameterType="Category"> 2 delete from category where id= #{id} 3 </delete>
4.更新(U)
通过session.update进行修改。updateCategory对应的sql语句,#{name}和#{id}获取category对象的name和id。
1 <update id="updateCategory" parameterType="Category"> 2 update category set name=#{name} where id=#{id} 3 </update>
1 private void Myupdate() throws IOException { 2 create(); 3 Category category = session.selectOne("getCategory", 3); 4 category.setName("修改了的Category名字"); 5 session.update("updateCategory", category); 6 listAll(session); 7 8 }
5.查询(R)
<1>查询所有
1 private void Allquery() throws IOException { 2 create(); 3 listAll(session); 4 close(); 5 }
对应的sql查询语句:
1 <select id="listCategory" resultType="Category"> 2 select * from category 3 </select>
<2>根据id查询
通过session.selectOne获取id=3的记录,Category category= session.selectOne("getCategory",3),getCategory对应的sql语句:
1 <select id="getCategory" parameterType="_int" resultType="Category"> 2 select * from category where id= #{id} 3 </select>
1 private void Myquery() throws IOException { 2 create(); 3 Category category = session.selectOne("getCategory", 3); 4 System.out.println(category.getName()); 5 close(); 6 }
testCRUD.java
1 package mybatis.test; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.List; 6 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 11 import mybatis.pojo.Category; 12 13 public class testCRUD { 14 15 private SqlSession session; 16 private InputStream inputStream; 17 private SqlSessionFactory sqlSessionFactory; 18 19 public testCRUD() throws IOException { 20 // TODO Auto-generated constructor stub 21 String resource = "mybatis-config.xml"; 22 this.inputStream = org.apache.ibatis.io.Resources.getResourceAsStream(resource); 23 this.sqlSessionFactory = new SqlSessionFactoryBuilder().build(this.inputStream); 24 } 25 26 public void create() throws IOException { 27 session = sqlSessionFactory.openSession(); 28 } 29 30 public void close() { 31 session.commit(); 32 session.close(); 33 } 34 35 // 1.增加 36 private void Myinset() throws IOException { 37 create(); 38 for (int i = 0; i < 10; i++) { 39 Category category = new Category(); 40 category.setName("xiaomi" + i); 41 session.insert("addCategory", category); 42 } 43 listAll(session); 44 close(); 45 } 46 47 // 2.删除 48 private void Mydelete() throws IOException { 49 create(); 50 Category category = new Category(); 51 category.setId(6); 52 session.delete("deleteCategory", category); 53 listAll(session); 54 close(); 55 } 56 57 // 3.查询 58 private void Myquery() throws IOException { 59 create(); 60 Category category = session.selectOne("getCategory", 3); 61 System.out.println(category.getName()); 62 close(); 63 } 64 65 // 4.更新 66 private void Myupdate() throws IOException { 67 create(); 68 Category category = session.selectOne("getCategory", 3); 69 category.setName("修改了的Category名字"); 70 session.update("updateCategory", category); 71 listAll(session); 72 73 } 74 75 // 5.查询所有 76 private void Allquery() throws IOException { 77 create(); 78 listAll(session); 79 close(); 80 } 81 82 // 列出所有的对象名字 83 private void listAll(SqlSession session) { 84 List<Category> cs = session.selectList("listCategory"); 85 for (Category c : cs) { 86 System.out.println(c.getName()); 87 } 88 } 89 90 public static void main(String[] args) throws IOException { 91 testCRUD testCRUD = new testCRUD(); 92 // testCRUD.Myinset(); 93 // testCRUD.Mydelete(); 94 // testCRUD.Myquery(); 95 testCRUD.Myupdate(); 96 // testCRUD.Allquery(); 97 } 98 }