最简单的mybatis增删改查样例

时间:2022-06-01 19:39:38

最简单的mybatis增删改查样例 
Book.java

  1. package com.bookstore.app;
  2. import java.io.Serializable;
  3. public class Book implements Serializable {
  4. private static final long serialVersionUID = 233559895466038160L;
  5. private String isbn;
  6. private String name;
  7. private String price;
  8. public String getIsbn() {
  9. return isbn;
  10. }
  11. public void setIsbn(String isbn) {
  12. this.isbn = isbn;
  13. }
  14. public String getPrice() {
  15. return price;
  16. }
  17. public void setPrice(String price) {
  18. this.price = price;
  19. }
  20. public void setName(String name) {
  21. this.name = name;
  22. }
  23. public String getName() {
  24. return name;
  25. }
  26. @Override
  27. public String toString() {
  28. return "BookName: " + this.name + ", price: " + this.price;
  29. }
  30. }

mybatis-config.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <settings>
  7. <setting name="lazyLoadingEnabled" value="false" />
  8. </settings>
  9. <typeAliases>
  10. <typeAlias alias="Book" type="com.bookstore.app.Book" />
  11. </typeAliases>
  12. <environments default="development">
  13. <environment id="development">
  14. <transactionManager type="JDBC" />
  15. <dataSource type="POOLED">
  16. <property name="driver" value="com.mysql.jdbc.Driver" />
  17. <property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
  18. <property name="username" value="root" />
  19. <property name="password" value="root" />
  20. </dataSource>
  21. </environment>
  22. </environments>
  23. <mappers>
  24. <mapper resource="com/bookstore/config/bookMapper.xml" />
  25. </mappers>
  26. </configuration>

bookMapper.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.bookstore.app.bookMapper">
  5. <select id="getBookByName" resultType="Book" parameterType="string">
  6. select *
  7. from book
  8. where name=#{name}
  9. </select>
  10. <insert id="insertBook" parameterType="Book">
  11. insert into book(isbn, name, price)
  12. value(#{isbn}, #{name}, #{price})
  13. </insert>
  14. <update id="updateBook" parameterType="Book">
  15. update book
  16. set
  17. name=#{name},
  18. price=#{price}
  19. where isbn=#{isbn}
  20. </update>
  21. <delete id="deleteBook" parameterType="String">
  22. delete from book where name=#{name}
  23. </delete>
  24. </mapper>

BookApp.java

    1. package com.bookstore.app;
    2. import java.io.IOException;
    3. import java.io.Reader;
    4. import org.apache.ibatis.io.Resources;
    5. import org.apache.ibatis.session.SqlSession;
    6. import org.apache.ibatis.session.SqlSessionFactory;
    7. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    8. public class BookApp {
    9. public static void main(String[] args) throws IOException {
    10. String resource = "com/bookstore/config/mybatis-config.xml";
    11. Reader reader = Resources.getResourceAsReader(resource);
    12. SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder()
    13. .build(reader);
    14. SqlSession session = sessionFactory.openSession();
    15. //查询
    16. Book book = (Book) session.selectOne(
    17. "com.bookstore.app.bookMapper.getBookByName", "Spring In Action");
    18. System.out.println(book.toString());
    19. //修改
    20. book.setName("Spring In Action 2ed Edition");
    21. book.setPrice("59");
    22. session.update("com.bookstore.app.bookMapper.updateBook", book);
    23. //插入
    24. Book newBook = new Book();
    25. newBook.setIsbn("ISBN978-7-121-10577-7");
    26. newBook.setName("人人都是产品经理");
    27. newBook.setPrice("45");
    28. session.insert("com.bookstore.app.bookMapper.insertBook", newBook);
    29. //删除
    30. session.delete("com.bookstore.app.bookMapper.deleteBook", "Struts In Action");
    31. session.commit();
    32. }
    33. }