Spring Boot集成Mybatis

集成Mybatis

引入依赖

<!--集成mybatis-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.37</version>
</dependency>

创建对应数据库的实体类

例如:

public class Admin {
    private Long id;
    private String username;
    private String password;
    private String admin_password;

    //省略get set方法
}

创建mapper

在 src/main/resources/mapper 下创建Admin 实体类对应的mapper文件 AdminMapper.xml

<?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.hanzoy.demo.mapper.AdminMapper">

    <select id="list" resultType="com.hanzoy.demo.domain.Admin">
        select `id`, `username` from `Admin`
    </select>

</mapper>

在 src/main/java/com/hanzoy/demo/mapper 下创建接口 AdminMapper

package com.hanzoy.demo.mapper;

import com.hanzoy.demo.domain.Admin;

import java.util.List;

public interface AdminMapper {

    public List<Admin> list();
    //这里的list对应上面AdminMapper.xml的 list

}

在springboot启动类上添加包扫描注解

package com.hanzoy.demo;

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

@SpringBootApplication
@MapperScan("com.hanzoy.demo.mapper")
//扫描mapper文件夹
public class DemoApplication {

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

}

修改application.yml配置

server:
  port: 8080

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/sipc?userUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver

mybatis:
  mapper-locations: classpath:/mapper/*.xml

创建service与controller

TestService

package com.hanzoy.demo.service;

import com.hanzoy.demo.domain.Admin;
import com.hanzoy.demo.mapper.AdminMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TestService {
    @Resource
    private AdminMapper testMapper;

    public List<Admin> list(){
        return testMapper.list();
    }
}

AuthController

package com.hanzoy.demo.controller;

import com.hanzoy.demo.domain.Admin;
import com.hanzoy.demo.service.TestService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping
public class AuthController {

    @Resource
    private TestService service;

    @GetMapping("/list")
    public List<Admin> test(){
        return service.list();
    }
}

优化Mybatis日志信息

在application.yml中添加以下配置

image-20201229232014864

idea数据库插件

点击右边的Database

image-20201229214822366

新建MySQL数据源

image-20201229214926761

填写相关信息

image-20201229221841994

点击Test Connection,提示下载驱动包,点击下载驱动包

image-20201229222010322

由于我们使用老版本的数据库所以提示我们需要配置timezone选项

image-20201229222237610

image-20201229222536136

进入设置,搜索SQL,如图修改

image-20201229223838411

这时回到之前的AdminMapper.xml便可发现数据库的内容已经有代码提示了

image-20201229224247730

同时idea连接到数据库之后还可以直接运行sql语句

image-20201229230051948

选中需要执行的sql语句右键点击Execute执行

image-20201229230121626

idea集成mybatis generator

添加idea插件

<!-- mybatis generator 自动生成代码插件 -->
<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.7</version>
    <configuration>
        <configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
        <overwrite>true</overwrite>
        <verbose>true</verbose>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
        </dependency>
    </dependencies>
</plugin>

添加配置文件

在 src/main/resources/generator 下面添加内容如下名为 generatorConfig.xml 的文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <!--覆盖生成的XML文件-->
        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"/>

        <!--生成的实体类添加toString()方法-->
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>

        <!--不生成注释-->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/sipc"
                        userId="root"
                        password="123456">
        </jdbcConnection>

        <!--domain类的位置-->
        <javaModelGenerator targetProject="src\main\java"
                            targetPackage="com.hanzoy.demo.domain"/>

        <!--mapper xml的位置-->
        <sqlMapGenerator targetProject="src\main\resources"
                         targetPackage="mapper"/>

        <!--mapper类的位置        -->
        <javaClientGenerator targetProject="src\main\java"
                             targetPackage="com.hanzoy.demo.mapper"
                             type="XMLMAPPER"/>

        <table tableName="admin" domainObjectName="Admin"/>
    </context>
</generatorConfiguration>

创建maven启动命令

点击 Edit Configurations

选择maven

image-20201229204601413

在 Command line添加 mybatis-generator:generate -e 命令

image-20201229204815977

点击启动

image-20201229234609461

随即生成对应的四个文件

image-20201229234656313


一个好奇的人