整合MyBatis
1. 创建项目
创建SpringBoot项目,添加MyBatis依赖、数据库驱动以及数据库连接池依赖,代码如下:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
2. 创建数据库、表、实体类
2.1 创建数据库和表
CREATE DATABASE `chapter05` DEFAULT CHARACTER SET utf8;
USE `chapter05`
CREATE TABLE `book`(
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(128) DEFAULT NULL,
`author` VARCHAR(64) DEFAULT NULL,
PRIMARY KEY(`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `book`(`id`,`name`,`author`) VALUES (1,'刻在你心底的名字','DiFer'),(2,'活着','余华');
2.2 数据库配置
在application.yml 中配置数据库基本连接信息:
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/chapter05?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
druid:
filters: stat,wall
aop-patterns: com.chen.*
stat-view-servlet:
enabled: true
login-username: admin
login-password: 123456
reset-enable: false
web-stat-filter:
enabled: true
url-pattern: /*
exclusions: '*.js,*.gif,*jpg,*.png,*.ico,/druid/*'
filter:
stat:
slow-sql-millis: 1000
log-slow-sql: true
enabled: true
wall:
enabled: true
注意:msql8.0版本以上要有时区的配置serverTimezone=GMT%2B8
在application.properties配置MyBatis
mybatis.type-aliases-package=com.chen.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
第一行是起别名。 第二行扫描xml文件。
2.3 创建实体类
创建Book实体,代码如下:
public class Book {
private Integer id;
private String name;
private String author;
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
'}';
}
public Book() {
}
public Book(Integer id, String name, String author) {
this.id = id;
this.name = name;
this.author = author;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
3. 创建数据库访问层
创建BookMapper,代码如下:
@Mapper
public interface BookMapper {
int addBook(Book book);
int deleteBookById(Integer id);
int updateBookById(Book book);
Book getBookById(Integer id);
List<Book> getAllBooks();
}
代码解释: 有两种方式指明该类是一个Mapper:第一种,在BookMapper上添加@Mapper注解,表明该接口是一个MyBatis中的Mapper,这种方式需要在每一个Mapper上都添加注解;还有一种简单的方式在配置类上添加@MapperScan(“org.sang.mapper”)注解,表示扫描org.sang.mapper包下所有接口作为Mapper。
4. 创建BookMapper.xml
在resources下在创建: 代码如下:
<?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.mapper.BookMapper">
<insert id="addBook" parameterType="Book">
insert into book(name,author) value (#{name},#{author})
</insert>
<delete id="deleteBookById" parameterType="int">
delete from book where id=#{id}
</delete>
<update id="updateBookById" parameterType="Book">
update book set name=#{name},author=#{author} where id=#{id}
</update>
<select id="getBookById" resultType="Book">
select * from book where id=#{id}
</select>
<select id="getAllBooks" resultType="Book">
select * from book
</select>
</mapper>
5.创建Service
@Service
public class BookService {
@Autowired
BookMapper bookMapper;
public int addBook(Book book){
return bookMapper.addBook(book);
}
public int updateBook(Book book){
return bookMapper.updateBookById(book);
}
public int deleteBookById (Integer id) {
return bookMapper.deleteBookById(id);
}
public Book getBookById(Integer id){
return bookMapper.getBookById(id);
}
public List<Book> getAllBooks(){
return bookMapper.getAllBooks();
}
}
6. 创建Controller
@RestController
public class BookController {
@Autowired
BookService bookService;
@GetMapping("/bookOps")
public void bookOps(){
Book b1 = new Book();
b1.setName("Java");
b1.setAuthor("Json");
int i = bookService.addBook(b1);
System.out.println("addBook>>>"+i);
Book b2 = new Book();
b2.setId(1);
b2.setName("许三观卖血记");
b2.setAuthor("余华");
int updateBook = bookService.updateBook(b2);
System.out.println("update>>>"+updateBook);
Book b3 = bookService.getBookById(1);
System.out.println("getBookById>>>"+b3);
List<Book> allBooks = bookService.getAllBooks();
System.out.println("getAllBooks>>>"+allBooks);
}
}
接下来在浏览器中输入“http://localhost: 8080/bookOps ”,即可看到数据库中数据的变化,控制台 也打印出相应的日志。 数据库的数据更新变化
|