• springboot2.X整合mybatis


    github地址:https://github.com/BenchChen/springboot

    1) 创建springboot-maven项目,并修改pom文件

     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.cx</groupId>
     7     <artifactId>180723_mybatisdome</artifactId>
     8     <version>0.0.1-SNAPSHOT</version>
     9     <packaging>jar</packaging>
    10 
    11     <name>180723_mybatisdome</name>
    12     <description>Demo project for Spring Boot</description>
    13 
    14     <parent>
    15         <groupId>org.springframework.boot</groupId>
    16         <artifactId>spring-boot-starter-parent</artifactId>
    17         <version>2.0.3.RELEASE</version>
    18         <relativePath/> <!-- lookup parent from repository -->
    19     </parent>
    20 
    21     <properties>
    22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    24         <java.version>1.8</java.version>
    25     </properties>
    26 
    27     <dependencies>
    28         <dependency>
    29             <groupId>org.springframework.boot</groupId>
    30             <artifactId>spring-boot-starter-web</artifactId>
    31         </dependency>
    32         <dependency>
    33             <groupId>org.mybatis.spring.boot</groupId>
    34             <artifactId>mybatis-spring-boot-starter</artifactId>
    35             <version>1.3.2</version>
    36         </dependency>
    37     <!-- alibaba的druid数据库连接池 -->
    38         <dependency>
    39             <groupId>com.alibaba</groupId>
    40             <artifactId>druid-spring-boot-starter</artifactId>
    41             <version>1.1.9</version>
    42         </dependency>
    43         <dependency>
    44             <groupId>mysql</groupId>
    45             <artifactId>mysql-connector-java</artifactId>
    46             <scope>runtime</scope>
    47         </dependency>
    48         <dependency>
    49             <groupId>org.springframework.boot</groupId>
    50             <artifactId>spring-boot-starter-test</artifactId>
    51             <scope>test</scope>
    52         </dependency>
    53     </dependencies>
    54 
    55     <build>
    56         <plugins>
    57             <plugin>
    58                 <groupId>org.springframework.boot</groupId>
    59                 <artifactId>spring-boot-maven-plugin</artifactId>
    60             </plugin>
    61              <!-- Mybatis-Generator插件,自动生成代码 -->
    62             <plugin>
    63                 <groupId>org.mybatis.generator</groupId>
    64                 <artifactId>mybatis-generator-maven-plugin</artifactId>
    65                 <version>1.3.5</version>
    66                 <configuration>
    67                     <configurationFile>${project.basedir}/src/main/resources/generatorConfig.xml</configurationFile>
    68                     <verbose>true</verbose>
    69                     <overwrite>true</overwrite>
    70                 </configuration>
    71             </plugin>
    72         </plugins>
    73     </build>
    74 
    75 
    76 </project>

    2 ) 在 src/main/resources 目录下新建配置文件 datasource.properties保存连接数据库的信息 , 为generator 准备

     1 ## mysql驱动包的本地路径,这个jar包的版本要和pom.xml里配置的版本一致
     2 db.driverLocation=E:\mavenKu\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar
     3 ## 驱动类的包名
     4 db.driverClassName=com.mysql.jdbc.Driver
     5 ## // 连接数据库的url
     6 db.url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8
     7 ## 用户名
     8 db.username=root
     9 ## 密码
    10 db.password=root

    3 ) 在 src/main/resources 目录下新建配置文件 generatorConfig.xml  , generator 配置信息

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE generatorConfiguration
     3         PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
     4         "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
     5 
     6 <generatorConfiguration>
     7     <!--导入属性配置文件-->
     8     <properties resource="datasource.properties"/>
     9 
    10     <!--指定特定数据库的jdbc驱动jar包的位置-->
    11     <classPathEntry location="${db.driverLocation}"/>
    12 
    13     <!-- 选择运行的mybatis版本 -->
    14     <context id="default" targetRuntime="MyBatis3">
    15 
    16         <!-- optional,旨在创建class时,对注释进行控制 -->
    17         <commentGenerator>
    18             <property name="suppressDate" value="true"/>
    19             <property name="suppressAllComments" value="true"/>
    20         </commentGenerator>
    21 
    22         <!--jdbc的数据库连接 -->
    23         <jdbcConnection
    24                 driverClass="${db.driverClassName}"
    25                 connectionURL="${db.url}"
    26                 userId="${db.username}"
    27                 password="${db.password}">
    28         </jdbcConnection>
    29 
    30         <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
    31         <javaTypeResolver>
    32             <property name="forceBigDecimals" value="false"/>
    33         </javaTypeResolver>
    34 
    35         <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
    36             targetPackage     指定生成的model生成所在的包名
    37             targetProject     指定在该项目下所在的路径
    38         -->
    39         <javaModelGenerator targetPackage="com.cx.user.model" targetProject="./src/main/java">
    40             <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
    41             <property name="enableSubPackages" value="false"/>
    42             <!-- 是否对model添加 构造函数 -->
    43             <property name="constructorBased" value="true"/>
    44             <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
    45             <property name="trimStrings" value="true"/>
    46             <!-- 建立的Model对象是否不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->
    47             <property name="immutable" value="false"/>
    48         </javaModelGenerator>
    49 
    50         <!--mapper xml映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
    51         <sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources">
    52             <property name="enableSubPackages" value="false"/>
    53         </sqlMapGenerator>
    54 
    55         <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
    56                 type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
    57                 type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
    58                 type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
    59         -->
    60 
    61         <!-- targetPackage:dao层mapper接口生成的位置 -->
    62         <javaClientGenerator type="XMLMAPPER" targetPackage="com.cx.user.dao" targetProject="./src/main/java">
    63             <!-- enableSubPackages:是否让schema作为包的后缀 -->
    64             <property name="enableSubPackages" value="false" />
    65         </javaClientGenerator>
    66 
    67         <!-- 配置表格映射 -->
    68         <table tableName="t_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
    69     </context>
    70 </generatorConfiguration>

    4 ) 修改 application.yml  ,  配置数据池

     1 server:
     2   port: 8080
     3 
     4 spring:
     5     datasource:
     6         name: mybatis_test
     7         #使用druid连接池
     8         type: com.alibaba.druid.pool.DruidDataSource
     9         #druid相关配置
    10         druid:
    11           #监控统计拦截的filters
    12           filters: stat
    13           driver-class-name: com.mysql.jdbc.Driver
    14           #配置基本属性
    15           url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
    16           username: root
    17           password: root
    18           #配置初始化大小/最小/最大
    19           initial-size: 1
    20           min-idle: 1
    21           max-active: 20
    22           #获取连接等待超时时间
    23           max-wait: 60000
    24           #间隔多久进行一次检测,检测需要关闭的空闲连接
    25           time-between-eviction-runs-millis: 60000
    26           #一个连接在池中最小生存的时间
    27           min-evictable-idle-time-millis: 300000
    28           validation-query: SELECT 'x'
    29           test-while-idle: true
    30           test-on-borrow: false
    31           test-on-return: false
    32           #打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
    33           pool-prepared-statements: false
    34           max-pool-prepared-statement-per-connection-size: 20
    35 
    36 mybatis:
    37   # 映射文件所在路径
    38   mapper-locations: classpath:mappers/*.xml
    39   # pojo类所在包路径
    40   type-aliases-package: com.cx.user.model

    5 )  在 pom.xml 文件上右键 Run As >> Maven builed  在弹出的窗口 Goals 处输入  mybatis-generator:generate  然后Run 等待文件生成

      此时项目结构如下, 图中红圈位置是自己写的 , 不会生成。

      PS:注意要在mapper接口上加上@Mapper注解不然运行会抛异常

     =====================================整合结束 , 测试没毛病=====================================

  • 相关阅读:
    Chapter 6 GUI and OOD
    Chapter 5 : Control Structures 2 : Repetition
    Chapter 4 : Control Structures 1 : Selection
    Chapter 3 Introduction to Objects and Input/Output
    为什么很多应用都安装在/usr/local目录下?
    Chapter 2 Basic Elements of JAVA
    Chapter 1 An Overview of Computers and Programming Languages
    ZooKeeper Getting Started Guide
    The Apache HBase™ Reference Guide
    垃圾回收算法记录
  • 原文地址:https://www.cnblogs.com/cx987514451/p/9355299.html
Copyright © 2020-2023  润新知