记录在 IDEA 中 使用SpringBoot2 整合 Mybatis的 实例,环境:Java8 + Maven + MySQL8。
1. 添加依赖
添加 MyBatis 依赖,MySQL 连接依赖,,数据库用的MySQL8。
<!-- MyBatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!-- MySQL 连接依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
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 http://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.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.syrdbt</groupId>
<artifactId>mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis</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>
<!-- MyBatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!-- MySQL 连接依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. 创建数据库的 SQL语句
创建数据库的 SQL语句如下所示:注意 ID 是自增的,而且是主键。
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`name` varchar(255) DEFAULT NULL,
`ID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
数据库 User 表结构 :
3. 工程实例
项目的结构如图所示:有些用不到,红框中的本工程实例使用到的。
实体类 User.java :
package com.syrdbt.mybatis.entity;
public class User {
private String ID;
private String name;
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"ID='" + ID + '\'' +
", name='" + name + '\'' +
'}';
}
}
UserDAO.java ,DAO 层。
package com.syrdbt.mybatis.dao;
import com.syrdbt.mybatis.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
public interface UserDAO {
/**
* 查询一个用户
* @param name 用户姓名
* @return 该用户
*/
@Select("SELECT * FROM USER WHERE NAME = #{name}")
User getUser(@Param("name") String name);
/**
* 插入一个用户
* @param name 用户姓名
*/
@Insert("INSERT INTO USER(NAME) VALUES(#{name})")
int insert(@Param("name") String name);
}
UserService.java, Service 层 接口:
package com.syrdbt.mybatis.service;
import com.syrdbt.mybatis.entity.User;
public interface UserService {
public User getUser(String name);
public void insertUser(String name);
}
UserServiceImpl.java,Service 层 调用 DAO 层。
package com.syrdbt.mybatis.service.impl;
import com.syrdbt.mybatis.dao.UserDAO;
import com.syrdbt.mybatis.entity.User;
import com.syrdbt.mybatis.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class UserServiceImpl implements UserService {
@Resource
UserDAO userDAO;
@Override
public User getUser(String name) {
return userDAO.getUser(name);
}
@Override
public void insertUser(String name) {
userDAO.insert(name);
}
}
TestMybatisController.java,控制器。
package com.syrdbt.mybatis.controller;
import com.syrdbt.mybatis.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class TestMybatisController {
@Resource
UserService userService;
@RequestMapping("/insertUser")
public String insertUser(@RequestParam(value = "name") String name) {
System.out.println(name);
userService.insertUser(name);
return userService.getUser(name).getID();
}
@RequestMapping("/getUser")
public String getUser(@RequestParam(value = "name") String name) {
return userService.getUser(name).toString();
}
}
MybatisApplication.java,启动类。
扫描 Mapper使用 @MapperScan("com.syrdbt.mybatis.dao") 。
package com.syrdbt.mybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.syrdbt.mybatis.dao")
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
}
}
application.properties ,数据库配置文件,记得修改用户名和密码。
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
4. 工程测试
插入一个用户 :访问 http://localhost:8080/insertUser?name=syrdbt。
查询用户:访问 http://localhost:8080/getUser?name=syrdbt。
标签:name,syrdbt,mybatis,实例,SpringBoot2,Mybatis,import,public,String From: https://blog.51cto.com/xuxiangyang/9038510