springboot 与mybatis 整合包含插件生成代码

步骤:

  1. pom文件添加依赖

  2. 创建包结构

  3. 添加application.yml, 配置数据库信息

  4. 使用mybatis-gennerator 生成三个文件

    • 实体类
    • 接口类
    • xml 文件
  5. 错误信息

    xml 绑定异常,没有扫描的 XXXmapper.xml文件

  6. 启动类扫描接口类

  7. 编写测试类

  8. 与传统使用mybatis操作数据的区别

    1. 没有必要使用sqlsession 来使用各种接口类的方法,操作数据库。
    2. 使用接口直接调用方法。

1. pom文件添加依赖

<?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.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.plugens</groupId>
    <artifactId>spring</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--springboot项目-->
        <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>

        <!--配置thymeleaf-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!--配置web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--配置mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>

        <!--配置mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.17</version>
        </dependency>

        <!--配置jdbc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

        </plugins>
    </build>
</project>

2 创建包结构

3配置yml mybatis-location扫描路径

#端口号配置
server:
  port: 8088
spring:
  #模板引擎配置
  thymeleaf:
    prefix: classpath:/templates/
    suffix: .html
    mode: HTML
    encoding: UTF-8
    cache: false
    servlet:
      content-type: text/html
  #静态文件配置
  resources:
    static-locations: classpath:/static,classpath:/META-INF/resources,classpath:/templates/
  #jdbc配置
  datasource:
    url: jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf8
    username: root
    password: 12345678
    driver-class-name: com.mysql.cj.jdbc.Driver
#mybatis配置
mybatis:
  #映射文件路径
  mapper-locations: classpath:mapper/*.xml
  #模型所在的保命
  type-aliases-package: com.plugens.spring.bean

4. mybatis 逆向生成代码

![zhuyi](/Users/yangtao/Library/Application Support/typora-user-images/image-20191118233058081.png)

Mybatis 生成插件的官方网站

https://github.com/kmaster/better-mybatis-generator/blob/master/README.md

必须要写数据库和这个serverTimezone=GMT

5. XXmapper文件

<?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.plugens.spring.dao.UserDaoMapper">



  <resultMap id="BaseResultMap" type="com.plugens.spring.bean.User">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="passwd" jdbcType="VARCHAR" property="passwd" />
  </resultMap>



  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=INTEGER}
  </select>

  
</mapper>

5. 启动类

6. 测试类

7. 配置XXmapper.xml 到其他路径,在pom 文件中的build 标签中添加

<resources>
    <resource>
        <directory>src/main/java/com/plugens/spring/dao</directory>
        <includes>
            <include>**/*.xml</include>
        </includes>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
    </resource>
</resources>
posted @ 2020-01-06 16:39  庭有奇树  阅读(256)  评论(0编辑  收藏  举报