SpringBoot2.x整合mybatis和MySQL(IDEA)

文章目录

  • SpringBoot2.x整合mybatis
    • idea创建新项目,如下操作
    • 完整项目目录如下
    • pom.xml
    • mysql数据库建表
    • 创建项目实体类
    • 创建UserDao接口
    • 创建mapper映射文件
    • application.properties中配置mybatis
    • 启动类中配置@MapperScan
    • 测试

SpringBoot2.x整合mybatis

idea创建新项目,如下操作

创建springboot项目
SpringBoot2.x整合mybatis和MySQL(IDEA)_第1张图片
填写项目组与项目名
SpringBoot2.x整合mybatis和MySQL(IDEA)_第2张图片
添加启动依赖
SpringBoot2.x整合mybatis和MySQL(IDEA)_第3张图片
SpringBoot2.x整合mybatis和MySQL(IDEA)_第4张图片
SpringBoot2.x整合mybatis和MySQL(IDEA)_第5张图片
SpringBoot2.x整合mybatis和MySQL(IDEA)_第6张图片
点击next完成构建

完整项目目录如下

SpringBoot2.x整合mybatis和MySQL(IDEA)_第7张图片

pom.xml

添加依赖

		<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

完整pom.xml文件


```java
<?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.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>rui.zhang</groupId>
    <artifactId>springboot-mybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-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-thymeleaf</artifactId>
        </dependency>
        <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.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <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>

mysql数据库建表

建一个test库,一个user表如下
在这里插入图片描述
在application.properties中添加数据源,test是库的名字

#数据源
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=*****
spring.datasource.tomcat.max-idle=10
spring.datasource.tomcat.max-wait=50
spring.datasource.tomcat.initial-size=5

创建项目实体类

package rui.zhang.springbootmybatis.entity;

import lombok.Data;
import org.springframework.stereotype.Repository;

@Data
@Repository
public class User {
    private Long id;
    private String name;
    private String pass;
}

创建UserDao接口

import org.springframework.stereotype.Repository;
import rui.zhang.springbootmybatis.entity.User;
@Repository
public interface UserDao {
    User getUserByName(String name);
}

创建mapper映射文件

在这里插入图片描述
主义namespace是UserDao的完整类名
select的id与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="rui.zhang.springbootmybatis.dao.UserDao">
    <select id="getUserByName" parameterType="String" resultType="User">
        SELECT * FROM user WHERE name = #{name}
    </select>

</mapper>

application.properties中配置mybatis

mapper文件地址和实体类别名设置

#mybatis
mybatis.mapper-locations=classpath:mappers/*.xml
mybatis.type-aliases-package=rui.zhang.springbootmybatis.entity

启动类中配置@MapperScan

扫描dao包下的UserDao

package rui.zhang.springbootmybatis;

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

@SpringBootApplication
@MapperScan(basePackages = "rui.zhang.springbootmybatis.dao",annotationClass = Repository.class)
public class SpringbootMybatisApplication {

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

}

测试

package rui.zhang.springbootmybatis;

import org.junit.jupiter.api.Test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Repository;
import rui.zhang.springbootmybatis.dao.UserDao;
import rui.zhang.springbootmybatis.entity.User;

import javax.annotation.Resource;

@SpringBootTest
@MapperScan(basePackages = "rui.zhang.springbootmybatis.dao")
class SpringbootMybatisApplicationTests {
    @Autowired
    UserDao userDao=null;

    @Test
    void contextLoads() {
        User user=userDao.getUserByName("name");
        System.out.println(user);
    }

}

你可能感兴趣的:(SpringBoot,mybatis,mysql)