springboot2.1.7整合mybatis

dongxurr 2019-12-17

1.先创建springboot项目,然后会有如下依赖(aop是自己加上去的)

<dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-web</artifactId></dependency><dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-test</artifactId>   <scope>test</scope></dependency><dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-aop</artifactId></dependency>
2.然后添加mysql驱动包
<dependency>   <groupId>mysql</groupId>   <artifactId>mysql-connector-java</artifactId>   <version>5.1.46</version></dependency>3.还要添加druid数据连接池(其他连接池也行,建议使用druid)
<dependency>   <groupId>com.alibaba</groupId>   <artifactId>druid</artifactId>   <version>1.0.9</version></dependency>4.在application.properties配置文件中添加配置
#(默认端口号,可不配置)
server.port=8080#连接mysql和连接池
spring.datasource.type=com.alibaba.druid.pool.DruidDataSourcespring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTCspring.datasource.username=rootspring.datasource.password=root#配置mysql数据库字段和实体属性映射
mybatis.configuration.map-underscore-to-camel-case=true#添加sql日志(debug级别-level后面为你mapper接口的路径)logging.level.com.example.mybatis.mapper=debug
#如果mapper.xml文件在resources下则需要此配置(classpath后面的mapper为存放xml文件的包)
mybatis.mapper-locations=classpath:mapper/*.xml#如果Mapper的xml映射文件在src/main/java包下时,需要此配置
<resources>   <resource>      <directory>src/main/java</directory>      <includes>         <include>**/*.xml</include>      </includes>   </resource>   <resource>      <directory>src/main/resources</directory>   </resource></resources>5.配置完成之后,我们再添加一个mybatis-generator插件用来生成代码,我添加在了如下位置

springboot2.1.7整合mybatis

打开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>    <!--指定maven仓库中jdbc驱动jar包的位置-->    <classPathEntry  location="E:\apache-maven-3.2.5\repository\mysql\mysql-connector-java\8.0.13\mysql-connector-java-8.0.13.jar"/>    <!-- 一个数据库一个context -->    <context id="DB2Tables"  targetRuntime="MyBatis3">        <commentGenerator>            <property name="suppressDate" value="true"/><!-- 是否生成注释代时间戳 -->            <property name="suppressAllComments" value="true"/><!-- 是否去除自动生成的注释 true是 false:否 -->        </commentGenerator>        <!--数据库链接URL,用户名、密码 -->        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/test?serverTimezone=GMT" userId="root" password="root">            <property name="nullCatalogMeansCurrent" value="true" />        </jdbcConnection>        <!-- 类型转换 -->        <javaTypeResolver>        <!-- 默认false,把JDBC DECIMAL和NUMERIC类型解析为 Integer;为true时则解析为java.math.BigDecimal -->            <property name="forceBigDecimals" value="false"/>        </javaTypeResolver>        <!-- 生成model的包名和位置-->        <javaModelGenerator targetPackage="com.example.mybatis.model" targetProject="src/main/java">            <property name="enableSubPackages" value="true"/>            <property name="trimStrings" value="true"/>        </javaModelGenerator>        <!-- 生成Mapper映射XML文件位置-->        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">            <property name="enableSubPackages" value="true"/>        </sqlMapGenerator>        <!-- 生成Mapper接口文件位置-->        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mybatis.mapper" targetProject="src/main/java">            <property name="enableSubPackages" value="true"/>        </javaClientGenerator>        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->        <table tableName="sys_permission" domainObjectName="Permission"               enableCountByExample="true" enableUpdateByExample="true"               enableDeleteByExample="true" enableSelectByExample="true"               selectByExampleQueryId="true">        </table>    </context></generatorConfiguration>6.在pom的plugins标签下添加插件依赖
<plugin>   <groupId>org.mybatis.generator</groupId>   <artifactId>mybatis-generator-maven-plugin</artifactId>  #版本   <version>1.3.2</version>   <configuration>    此位置是上面截图的位置(你放在哪里就写在哪里)      <configurationFile>D:\mybatis\src\main\resources\generate\generatorConfig.xml</configurationFile>      <verbose>true</verbose>      <overwrite>true</overwrite>   </configuration>   <executions>      <execution>         <id>Generate MyBatis Artifacts</id>         <!--maven生成jar包时 避免运行generator插件(这个必须要加上,否则mapper接口和xml会在重新生成)-->         <phase>deploy</phase>         <goals>            <goal>generate</goal>         </goals>      </execution>   </executions>   <dependencies>      <dependency>         <groupId>org.mybatis.generator</groupId>         <artifactId>mybatis-generator-core</artifactId>         <version>1.3.2</version>      </dependency>   </dependencies></plugin>
 

相关推荐