最简单的mybatis增删改查样例
Book.java
- package com.bookstore.app;
- import java.io.Serializable;
- public class Book implements Serializable {
- private static final long serialVersionUID = 233559895466038160L;
- private String isbn;
- private String name;
- private String price;
- public String getIsbn() {
- return isbn;
- }
- public void setIsbn(String isbn) {
- this.isbn = isbn;
- }
- public String getPrice() {
- return price;
- }
- public void setPrice(String price) {
- this.price = price;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- @Override
- public String toString() {
- return "BookName: " + this.name + ", price: " + this.price;
- }
- }
mybatis-config.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- <settings>
- <setting name="lazyLoadingEnabled" value="false" />
- </settings>
- <typeAliases>
- <typeAlias alias="Book" type="com.bookstore.app.Book" />
- </typeAliases>
- <environments default="development">
- <environment id="development">
- <transactionManager type="JDBC" />
- <dataSource type="POOLED">
- <property name="driver" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
- <property name="username" value="root" />
- <property name="password" value="root" />
- </dataSource>
- </environment>
- </environments>
- <mappers>
- <mapper resource="com/bookstore/config/bookMapper.xml" />
- </mappers>
- </configuration>
bookMapper.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="com.bookstore.app.bookMapper">
- <select id="getBookByName" resultType="Book" parameterType="string">
- select *
- from book
- where name=#{name}
- </select>
- <insert id="insertBook" parameterType="Book">
- insert into book(isbn, name, price)
- value(#{isbn}, #{name}, #{price})
- </insert>
- <update id="updateBook" parameterType="Book">
- update book
- set
- name=#{name},
- price=#{price}
- where isbn=#{isbn}
- </update>
- <delete id="deleteBook" parameterType="String">
- delete from book where name=#{name}
- </delete>
- </mapper>
BookApp.java
- package com.bookstore.app;
- import java.io.IOException;
- import java.io.Reader;
- import org.apache.ibatis.io.Resources;
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.apache.ibatis.session.SqlSessionFactoryBuilder;
- public class BookApp {
- public static void main(String[] args) throws IOException {
- String resource = "com/bookstore/config/mybatis-config.xml";
- Reader reader = Resources.getResourceAsReader(resource);
- SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder()
- .build(reader);
- SqlSession session = sessionFactory.openSession();
- //查询
- Book book = (Book) session.selectOne(
- "com.bookstore.app.bookMapper.getBookByName", "Spring In Action");
- System.out.println(book.toString());
- //修改
- book.setName("Spring In Action 2ed Edition");
- book.setPrice("59");
- session.update("com.bookstore.app.bookMapper.updateBook", book);
- //插入
- Book newBook = new Book();
- newBook.setIsbn("ISBN978-7-121-10577-7");
- newBook.setName("人人都是产品经理");
- newBook.setPrice("45");
- session.insert("com.bookstore.app.bookMapper.insertBook", newBook);
- //删除
- session.delete("com.bookstore.app.bookMapper.deleteBook", "Struts In Action");
- session.commit();
- }
- }