- Mybatis介绍
MyBatis是一款优秀的持久层框架,原名叫作iBaits, 2010年由ApacheSoftwareFoundation迁移到Google Code并改名为MyBatis, 2013年又迁移到GitHub上。MyBatis支持定制化SQL、存储过程以及高级映射。MyBatis几乎避免了所有的JDBC代码手动设置参数以及获取结果集。在传统的SSM框架整合中,使用MyBatis需要大量的XML配置,而在Spring Boot中,MyBatis官方提供了一套自动化配置方案,可以做到MyBatis开箱即用。具体使用步骤如下。 - (1)创建Spring Boot项目,添加MyBatis依赖、数据库驱动依赖以及数据库连接池依赖
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--druid数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
(2)创建数据库和表
CREATE DATABASE 'chapter' DEFAULT CHARACTER SET utf8;
USE 'chapter';
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`name` varchar(128) DEFAULT NULL,
`author` varchar(64) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `book` VALUES ('1', '三国演义', '罗贯中');
INSERT INTO `book` VALUES ('2', '水浒传', '施耐庵');
(3)数据库配置
#数据库配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/chapter?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
(4)创建实体类
public class Book {
private Integer id;
private String name;
private String author;
//省略 getter/setter
}
(5)创建数据库访问层
@Mapper
public interface BookMapper {
int addBook(Book book);
int deleteBookByld(Integer id);
int updateBookByld(Book book);
Book getBookByld();
List<Book> getAHBooks();
}
(6)创建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.example.springbootstudy.Mapper.BookMapper">
<insert id="addBook" parameterType="com.example.springbootstudy.pojo.Book">
INSERT INTO book(name,author) VALUES (#{name},#(author})
</insert>
<delete id="deleteBookByld" parameterType="int">
DELETE FROM book WHERE id=#{id}
</delete>
<update id="updateBookByld" parameterType="com.example.springbootstudy.pojo.Book">
UPDATE book set name=#{name},author=#{author} WHERE id=#{id}
</update>
<select id="getBookById" parameterType="int" resultType="com.example.springbootstudy.pojo.Book">
SELECT * FROM book WHERE id=#{id}
</select>
<select id="getAllBooks" resultType="com.example.springbootstudy.pojo.Book">
SELECT * FROM book
</select>
</mapper>
(7)Service
@Service
public class BookService2 {
@Autowired
BookMapper bookMapper;
public int addBook(Book book) {
return bookMapper.addBook(book);
}
public int updateBook(Book book) {
return bookMapper.updateBookByld(book);
}
public int deleteBookByld(Integer id) {
return bookMapper.deleteBookByld(id);
}
public Book getBookByld(Integer id){
return bookMapper.getBookByld(id);
}
public List<Book> getAllBooks(){
return bookMapper.getAllBooks();
}
}
(8)Controller
@Autowired
BookService2 bookService2;
@GetMapping ("/bookOps2")
public void bookOps2() {
Book bl = new Book();
bl.setName("西厢记");
bl.setAuthor("王实甫");
int i = bookService.addBook(bl);
System.out.println("addBook>»" + i);
Book b2 = new Book();
b2.setId(1);
b2.setName("朝花夕拾");
b2.setAuthor("鲁迅");
int updateBook = bookService.updateBook(b2);
System.out.println("updateBook»>" + updateBook);
Book b3 = bookService.getBookByld(1);
System.out.println("getBookById>»" + b3.toString());
int delete = bookService.deleteBookByld(2);
System.out.println("deleteBookById>»" + delete);
List<Book> allBooks = bookService.getAllBooks();
System.out.println("getAllBooks>»" + allBooks);
}
(9) 重启服务器 测试 如图
提示
在Maven工程中,XML配置文件建议写在resources目录下,但是上文的Mapper.xml文件写在包下,Maven在运行时会忽略包下的XML文件,因此需要在pom.xml文件中重新指明资源文件位置,配置如下:
pom依赖需要添加
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>