記錄在 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.0</version>
</dependency>
<!-- MySQL 連接依賴 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
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.springbootstudy01.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 + '\'' +
'}';
}
}
UserMapper.java ,Mapper類 ,相當於 DAO 層。
package com.syrdbt.springbootstudy01.mapper;
import com.syrdbt.springbootstudy01.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
public interface UserMapper {
@Select("SELECT * FROM USER WHERE NAME = #{name}")
User getUser(@Param("name") String name);
@Insert("INSERT INTO USER(NAME) VALUES(#{name})")
int insert(@Param("name") String name);
}
UserService.java:
package com.syrdbt.springbootstudy01.services;
import com.syrdbt.springbootstudy01.entity.User;
public interface UserService {
public User getUser(String name);
public void insertUser(String name);
}
UserServiceImpl.java,Service 層 調用 DAO 層。
package com.syrdbt.springbootstudy01.services.impl;
import com.syrdbt.springbootstudy01.entity.User;
import com.syrdbt.springbootstudy01.mapper.UserMapper;
import com.syrdbt.springbootstudy01.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class UserServiceImpl implements UserService {
@Resource
UserMapper userMapper;
@Override
public User getUser(String name) {
return userMapper.getUser(name);
}
@Override
public void insertUser(String name) {
userMapper.insert(name);
}
}
TestMybatisController.java,控制器。
package com.syrdbt.springbootstudy01.controller.testMybatis;
import com.syrdbt.springbootstudy01.entity.User;
import com.syrdbt.springbootstudy01.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
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;
@EnableAutoConfiguration
@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();
}
}
SpringbootStudy01Application.java,啓動類。
掃描 Mapper使用 @MapperScan("com.syrdbt.springbootstudy01.mapper") 。
package com.syrdbt.springbootstudy01;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
// 啓動類上加上@SpringBootApplication註解,當前包下或者子包下所有的類都可以掃到
@SpringBootApplication
// @EnableAsync 開啓異步調用
//@EnableAsync
// 掃描 Mapper
@MapperScan("com.syrdbt.springbootstudy01.mapper")
public class SpringbootStudy01Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootStudy01Application.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。