springboot2.x——集成mybatis和druid连接池

springboot2.x——集成mybatis和druid连接池

  •  引入mybatis和druid依赖
  •    配置打包方式
  •    配置mybatis自动生成插件

  完整的pom.xml

  

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>com.baiye</groupId>
 7     <artifactId>spring-boot-mybatis</artifactId>
 8     <version>1.0.0-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>spring-boot-mybatis</name>
12     <description></description>
13 
14     <parent>
15         <groupId>com.baiye</groupId>
16         <artifactId>spring-boot-dependencies</artifactId>
17         <version>1.0.0-SNAPSHOT</version>
18         <relativePath>../spring-boot-dependencies/pom.xml</relativePath>
19     </parent>
20 
21     <properties>
22         <!--update:2018-6-24-->
23         <lombok.version>1.16.18</lombok.version>
24     </properties>
25 
26     <dependencies>
27 
28         <!--集成druid & mybatis start-->
29         <dependency>
30             <groupId>com.alibaba</groupId>
31             <artifactId>druid-spring-boot-starter</artifactId>
32             <version>1.1.10</version>
33         </dependency>
34         <dependency>
35             <groupId>tk.mybatis</groupId>
36             <artifactId>mapper-spring-boot-starter</artifactId>
37             <version>2.0.2</version>
38         </dependency>
39         <!--分页插件-->
40         <dependency>
41             <groupId>com.github.pagehelper</groupId>
42             <artifactId>pagehelper-spring-boot-starter</artifactId>
43             <version>1.2.5</version>
44         </dependency>
45         <!--集成druid & mybatis end-->
46         <!--继承 lombok start-->
47         <dependency>
48             <groupId>org.projectlombok</groupId>
49             <artifactId>lombok</artifactId>
50             <version>${lombok.version}</version>
51             <scope>provided</scope>
52         </dependency>
53         <!--集成 lombok  end-->
54     </dependencies>
55 
56     <build>
57         <plugins>
58             <!--maven 打包插件-->
59             <plugin>
60                 <groupId>org.springframework.boot</groupId>
61                 <artifactId>spring-boot-maven-plugin</artifactId>
62                 <configuration>
63                     <mainClass>com.baiye.spring.boot.mybatis.SpringBootMybatisApplication</mainClass>
64                 </configuration>
65             </plugin>
66             <!--mybatis自动生成插件-->
67             <plugin>
68                 <groupId>org.mybatis.generator</groupId>
69                 <artifactId>mybatis-generator-maven-plugin</artifactId>
70                 <version>1.3.5</version>
71                 <configuration>
72                     <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
73                     <overwrite>true</overwrite>
74                     <verbose>true</verbose>
75                 </configuration>
76                 <dependencies>
77                     <dependency>
78                         <groupId>mysql</groupId>
79                         <artifactId>mysql-connector-java</artifactId>
80                         <version>${mysql.version}</version>
81                     </dependency>
82                     <dependency>
83                         <groupId>tk.mybatis</groupId>
84                         <artifactId>mapper</artifactId>
85                         <version>3.4.4</version>
86                     </dependency>
87                 </dependencies>
88             </plugin>
89         </plugins>
90     </build>
91 
92 </project>

重点:

  注意mybatis代码自动生成插件的xml文件(generatorConfig.xml)路径于pom配置匹配

  注意generatorConfig.xml生成的entity、dao、xml等文件要与项目的包匹配,数据库名称、密码、表等需要一致!!!

  完整的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>
    <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
    <classPathEntry  location="D:\Repositories\Maven\mysql\mysql-connector-java\5.1.29"/>
    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1(ip地址)/springbootdemo" userId="xxx" password="xxx">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成模型的包名和位置-->
        <javaModelGenerator targetPackage="com.baiye.spring.boot.mybatis.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置-->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.baiye.spring.boot.mybatis.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
        <table tableName="t_teacher" domainObjectName="Teacher" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

yml文件的配置

  自行修改数据库名称、密码、mapper文件的扫描路径

 1 server:
 2   port: 8080
 3 spring:
 4   datasource:
 5     druid:
 6       url: jdbc:mysql://127.0.0.1:3306/springbootdemo?useUnicode=true&characterEncoding=utf-8&useSSL=false
 7       username: xxx 
 8       password: xxx  
 9       initial-size: 1
10       min-idle: 1
11       max-active: 20
12       test-on-borrow: true
13       driver-class-name: com.mysql.jdbc.Driver
14 mybatis:
15     type-aliases-package: com.spring.boot.mybatis.entity
16     mapper-locations: classpath:mapper/*.xml

springboot的Junit测试两个关键注解:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootMybatisApplication.class)
package com.baiye.spring.boot.mybatis;

import com.baiye.spring.boot.mybatis.entity.Teacher;
import com.baiye.spring.boot.mybatis.mapper.TeacherMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
//SpringBootMybatisApplication 程序的入口类
@SpringBootTest(classes = SpringBootMybatisApplication.class)
public class MybatisCrudTest {

    @Autowired
    private TeacherMapper teacherMapper;

    @Test
    public void getInfo(){
        Teacher teacher = teacherMapper.selectByPrimaryKey(201L);
        System.out.println(teacher.getTName());
        System.out.println(teacher.getTAddress());
    }
}

最后,项目的目录结构如下:

 

  

  

posted @ 2018-06-24 15:44  白夜丶  阅读(561)  评论(0编辑  收藏  举报