Press "Enter" to skip to content

Spring Boot 集成 Mybatis

1. 新建Spring Boot项目

使用Spring Initializr导出一个新的Gradle或Maven项目,这里使用Gradle。

2. Gradle配置

build.gradle配置如下:

plugins {
	id 'org.springframework.boot' version '2.2.0.RELEASE'
	id 'io.spring.dependency-management' version '1.0.8.RELEASE'
	id 'java'
}

group = 'im.mango'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
	maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'}
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.1'
	runtimeOnly 'mysql:mysql-connector-java'
	testImplementation('org.springframework.boot:spring-boot-starter-test') {
		exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
	}
}

test {
	useJUnitPlatform()
}

Gradle刷新下依赖,这时我们需要的包都已经导入了。

3.Mybatis配置

application.properties文件配置

# MyBatis 配置
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

mybatis.type-aliases-package=im.mango.app.dao
mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

AppApplication配置,具体根据自己应用配置文件。

package im.mango.app;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("im.mango.app.dao")
public class AppApplication {

	public static void main(String[] args) {
		SpringApplication.run(AppApplication.class, args);
	}

}

4. Mybatis User使用示例

resources目录下新建mybatis/mapper目录,mapper新建示例UserMapper.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="im.mango.app.dao.UserDao">
    <select id="getUser" resultType="im.mango.app.entity.User">
        select
          id,
          name,
          email
         from users

         where id = #{id}
    </select>
    
</mapper>

P.S:users表准备好,保证有id,name,email字段。

src\main\java\im\mango\app目录下新建以下文件夹:controller,dao,entity

controller下新建UserController

package im.mango.app.controller;
// import包省略

@RestController
@RequestMapping("/rest")
public class UserController {
    @Resource
    private UserDao userDao;

    @GetMapping("user/{id}")
    public User getUser(@PathVariable int id) {
        return userDao.getUser(id);
    }
}

dao下新建UserDao

package im.mango.app.dao;

import im.mango.app.entity.User;
import org.apache.ibatis.annotations.Param;

public interface UserDao {

    User getUser(@Param("id") int id);

}

entity下新建User

package im.mango.app.entity;

public class User {
    private int id;
    private String name;
    private String email;

    // getter setter省略
}

现在user demo已经写好了,idea运行。

GET请求访问localhost:8080/rest/user/1即可看到数据了。

{
    "id": 1,
    "name": "Mr. Torey Nicolas",
    "email": "nbrown@example.org"
}

Be First to Comment

发表评论

电子邮件地址不会被公开。 必填项已用*标注