First, prepare the environment
- Development Tools: STS
- JDK Version: 1.8
- SpringBoot Version: 2.1.8
- Database: MySQL 8.0.17
Second, the database ready
Database here, but more discussions, you can create a new database as described below
- New Database local
- New data tables user, comprising a primary key id {int}, field name {varchar}, field prosession {varchar}
Third, the project to build
Complete idea of the project to build the following
- 3.1 SpringBoot new project, and add Spring Web, MySQL Driver and MyBatis Framework relies
- 3.2 perfect project directory structure
- 3.3 application.properties configuration database connection configuration file and configuration MyBatis
- 3.4 start the project, demonstrate CRUD
3.1 SpringBoot new project, and add Spring Web, MySQL Driver and MyBatis Framework relies
- New Project
- Project pom.xml file configuration is as follows
<?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.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mybatis</groupId>
<artifactId>Mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Mybatis</name>
<description>Spring Boot Mybatis</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.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2 perfect project directory structure
- entity layers: physical layer, the database storing the corresponding entity class
- dao layers: data layer, additions and deletions to achieve entity change search
- Layer service: service level, for the specific operational
- the controller layer: the control layer, the external processing request
- classpath: mapper: store MyBatis mapping file
3.2.1 entity Layer New User class, the corresponding User table in the database
package com.mybatis.entity;
public class User {
private int id;
private String name;
private String profession;
public User() {
}
public User(String name, String profession) {
this.name = name;
this.profession = profession;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
}
3.2.2 dao UserDao new layer interfaces, mapping classpath: mapper / UserMapper.xml file
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.mybatis.entity.User;
@Mapper
public interface UserDao {
// 新增
int insert(User user);
// 删除
int delete(int id);
// 修改
int update(User user);
// 查询
List<User> select();
}
3.2.3 service layer UserService new class that implements the business database CRUD
package com.mybatis.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mybatis.dao.UserDao;
import com.mybatis.entity.User;
@Service
public class UserService {
@Autowired
private UserDao userDao;
// 新增业务
public int insert(User user) {
return userDao.insert(user);
}
// 删除业务
public int delete(int id) {
return userDao.delete(id);
}
// 修改业务
public int update(User user) {
return userDao.update(user);
}
// 查询业务
public List<User> select(){
return userDao.select();
}
}
3.2.4 controller added layer UserController class, the processing request
package com.mybatis.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mybatis.entity.User;
import com.mybatis.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
@ResponseBody
@RequestMapping("insert")
public int insert() {
// 返回新增成功的行数
return userService.insert(new User("钟力", "Java高级工程师"));
}
@ResponseBody
@RequestMapping("delete")
public int delete() {
// 返回删除成功的行数
return userService.delete(1);
}
@ResponseBody
@RequestMapping("update")
public int update() {
User user = new User("钟力", "Java架构师");
user.setId(1);
// 返回修改成功的行数
return userService.update(user);
}
@ResponseBody
@RequestMapping("select")
public List<User> select() {
return userService.select();
}
}
3.2.5 classpath: mapper new UserMapper.xml files, maps, new interfaces dao layer UserDao
<?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.mybatis.dao.UserDao">
<insert id="insert" parameterType="User">
insert into user (name, profession) values (#{name}, #{profession})
</insert>
<delete id="delete">
delete from user where id = #{id}
</delete>
<update id="update" parameterType="User">
update user set name = #{name}, profession = #{profession} where id = #{id}
</update>
<select id="select" resultType="User">
SELECT * FROM user
</select>
</mapper>
3.3 application.properties configuration database connection configuration file and configuration MyBatis
spring.datasource.url=jdbc:mysql://localhost:3306/local?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=zl0418
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.mybatis.entity