------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
继续上次的开始,这次记录的是增删改查,上次重复过的代码不做过多解释
首先先创建mysql的表和实体类Book
BOOK
package cn.dawn.demo01.entity; public class Book { private Integer bookID; private String bookName; private String bookAuthor; private Integer bookPrice; public Book() { } public Integer getBookID() { return this.bookID; } public void setBookID(Integer bookID) { this.bookID = bookID; } public String getBookName() { return this.bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getBookAuthor() { return this.bookAuthor; } public void setBookAuthor(String bookAuthor) { this.bookAuthor = bookAuthor; } public Integer getBookPrice() { return this.bookPrice; } public void setBookPrice(Integer bookPrice) { this.bookPrice = bookPrice; } }
接下来是接口,我写到一块了,一次全部拿出来,我会标清除注释,给解释一下
package cn.dawn.demo01.dao; import cn.dawn.demo01.entity.Book; import java.util.List; /** * Created by Dawn on 2018/1/29. */ public interface IBookDAO { //查全部 public List<Book> findAllBook(); //添加 public int insertBook(Book book); //删除 public int deleteBook(int id); //修改 public int updateBook(Book book); //查都单独 public Book findOneBookByPK(int id); }
增删改得到的返回值都是受影响的行数,查全部返回List泛型,查单独根据bookid进行查,返回的是单个对象Book
接下来是dao层同名的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"> <mapper namespace="cn.dawn.demo01.dao.IBookDAO"> <select id="findAllBook" resultType="Book"> select * from book </select> <insert id="insertBook"> INSERT INTO book(bookname,bookauthor,bookprice) VALUES (#{bookName},#{bookAuthor},#{bookPrice}) </insert> <update id="updateBook"> UPDATE book SET bookname=#{bookName},bookauthor=#{bookAuthor},bookprice=#{bookPrice} WHERE bookid=#{bookID} </update> <delete id="deleteBook"> DELETE FROM book WHERE bookid=#{bookID} </delete> <select id="findOneBookByPK" resultType="Book"> SELECT * FROM book WHERE bookid=#{bookID} </select> </mapper>
下面我说一下这个xml中要注意的点,
mapper中的属性namespace的值就是刚才和这个同名的dao的全类名
其中的id要写成你刚才那个接口里的方法名,mybatis的这种方法这儿需要这么干,resultType="Book",这儿他是返回值类型,他这儿默认得写全类名,例如"cn.dawn.day01.entity.Book",
但是我这儿经过一个处理,它可以直接写成book,这个小知识点叫别名,稍微引一下,这儿放截图,截图好理解
parameterType=""这儿这个属性,我上面省略掉了,他的意思是什么呢,就是传进去的参数类型,刚才不是有个带参方法是根据id拿到所对应的书吗,这就可以写id的类型,但是这儿可以省略,所以我就不写了
准备工作都做完成之后,我们开始用单测的方式来进行单测
我下面把单测的方法放上来,使用的是较为原始的方式,更直接的剖析
import cn.dawn.demo01.entity.Book; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import java.util.List; /** * Created by Dawn on 2018/1/29. */ public class Test20180129 { /*Logger logger=Logger.getLogger("xxx");*/ ///查单独 @Test public void t5findOne(){ /*logger.info("这是查单独");*/ String path = "mybatis-config.xml"; try { InputStream e = Resources.getResourceAsStream(path); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(e); SqlSession session = factory.openSession(); Book book = session.selectOne("findOneBookByPK", 1); System.out.println("书名"+book.getBookName()); session.close(); } catch (IOException var9) { var9.printStackTrace(); } } ///删除 @Test public void t4delete(){ String path = "mybatis-config.xml"; try { InputStream e = Resources.getResourceAsStream(path); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(e); SqlSession session = factory.openSession(); int count123 = session.delete("deleteBook",4); session.commit(); System.out.println(count123); session.close(); } catch (IOException var9) { var9.printStackTrace(); } } ///修改 @Test public void t3update(){ String path = "mybatis-config.xml"; try { InputStream e = Resources.getResourceAsStream(path); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(e); SqlSession session = factory.openSession(); Book book=new Book(); book.setBookID(4); book.setBookName("心想事成888"); int count123 = session.update("updateBook", book); session.commit(); System.out.println(count123); session.close(); } catch (IOException var9) { var9.printStackTrace(); } } ///添加 @Test public void t2Insert(){ String path = "mybatis-config.xml"; try { InputStream e = Resources.getResourceAsStream(path); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(e); SqlSession session = factory.openSession(); Book book=new Book(); book.setBookName("心想事成"); book.setBookAuthor("孟六"); book.setBookPrice(999); int count123 = session.insert("insertBook", book); session.commit(); System.out.println(count123); session.close(); } catch (IOException var9) { var9.printStackTrace(); } } ///查全部 @Test public void t1SelectAll(){ String path = "mybatis-config.xml"; try { InputStream e = Resources.getResourceAsStream(path); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(e); SqlSession session = factory.openSession(); List lists = session.selectList("findAllBook"); Iterator var7 = lists.iterator(); while(var7.hasNext()) { Book book = (Book)var7.next(); System.out.println(book.getBookName() + "\t" + book.getBookAuthor()); } session.close(); } catch (IOException var9) { var9.printStackTrace(); } } }
只有selectOne需要看一下,他是查单个的意思,别的都比较好理解,有的前一个博客有写,有的从字面意思就可以理解
增删改查就此结束