MyBatis+MySQL8.0实现数据持久化
1. 创建数据库
首先,在Mysql中创建该数据库。
CREATE DATABASE `chapter5` DEFAULT CHARACTER SET utf8;
USE chapter5;
CREATE TABLE `book`(
`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(128) DEFAULT NULL,
`author` VARCHAR(64) DEFAULT NULL
)ENGINE=INNODB DEFAULT CHARSET =utf8;
INSERT INTO `book`(`id`,`name`,`author`) VALUES (1,‘三国演义‘,‘罗贯中‘),(2,‘水浒传‘,‘施耐庵‘);
2.加入依赖
在pom.xml中加入相关依赖依赖主要如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>mybatisdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatisdemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.数据库配置
在application.properties配置,配置你的MySQL数据库连接参数
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/chapter5?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT&useSSL=false
spring.datasource.username=root
spring.datasource.password=【数据库密码】
4.编写实体类
编写实体类Book,字段和数据库里面的字段对应
package com.example.mybatisdemo.entry;
public class Book {
private Integer id;
private String name;
private String 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;
}
}
5.数据库访问层
5.1 新建BookMapper.java
package com.example.mybatisdemo.mapper;
import com.example.mybatisdemo.entry.Book;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface BookMapper {
int addBook(Book book);
int deleteBookById(Integer id);
int updateBook(Book book);
Book getBookById(Integer id);
List<Book> getAllBooks();
}
5.2 新建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.mybatisdemo.mapper.BookMapper">
<insert id="addBook" parameterType="com.example.mybatisdemo.entry.Book">
INSERT INTO book(name ,author) values (#{name},#{author})
</insert>
<update id="updateBook" parameterType="com.example.mybatisdemo.entry.Book">
update book set name =#{name},author=#{author} where id=#{id}
</update>
<delete id="deleteBookById" parameterType="int">
delete from book where id=#{id}
</delete>
<select id="getBookById" parameterType="int" resultType="com.example.mybatisdemo.entry.Book">
select * from book where id=#{id}
</select>
<select id="getAllBooks" resultType="com.example.mybatisdemo.entry.Book">
select * from book
</select>
</mapper>
6.创建Service
创建BookService.java
package com.example.mybatisdemo.service;
import com.example.mybatisdemo.entry.Book;
import com.example.mybatisdemo.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookService {
@Autowired
BookMapper bookMapper;
public int addBook(Book book){
return bookMapper.addBook(book);
}
public int updateBook(Book book){
return bookMapper.updateBook(book);
}
public int deteBookById(Integer id){
return bookMapper.deleteBookById(id);
}
public Book getBookById(Integer id){
return bookMapper.getBookById(id);
}
public List<Book> getAllBooks(){
return bookMapper.getAllBooks();
}
}
7.创建Controller
创建BookController.java
package com.example.mybatisdemo.controller;
import com.example.mybatisdemo.entry.Book;
import com.example.mybatisdemo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class BookController {
@Autowired
BookService service;
@GetMapping("/bookOps")
public void bookOps(){
Book b1=new Book();
b1.setName("西厢记");
b1.setAuthor("王实普");
int i =service.addBook(b1);
System.out.println("addBook>>"+i);
Book b2=new Book();
b2.setId(1);
b2.setName("朝花夕拾");
b2.setAuthor("鲁迅");
int updatebook=service.updateBook(b2);
System.out.println("updatebook>>"+updatebook);
Book b3=service.getBookById(1);
System.out.println("getBook>>"+b3);
int delete=service.deteBookById(2);
System.out.println("delete>>"+delete);
List<Book> allbooks=service.getAllBooks();
System.out.println("allBppk>>"+allbooks);
}
}
参考原文:《SpringBoot+Vue 全栈开发实战》 王松 著