二、SpringBoot集成Myabtis
2.1. pom 依赖
<!--版本控制-><properties><java.version>1.8</java.version><oracle.version>11.2.0.3</oracle.version><mysql.version>8.0.20</mysql.version></properties><!--Mybatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.2</version></dependency><!--Mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency><!--oracle--><dependency><groupId>com.oracle</groupId><artifactId>ojdbc6</artifactId><version>${oracle.version}</version></dependency>
注:数据库任选其一即可
2.2. yml配置
mybatis:configuration:map-underscore-to-camel-case: true# 控制台日志配置log-impl: org.apache.ibatis.logging.stdout.StdOutImplmapper-locations: classpath:mappers/*.xml
2.3. 启动类+扫描mapper包注解
package com.gblfy;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan(basePackages = "com.gblfy.mapper")
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}
三、集成Mybatis Generator
适配Mysql和Oracle
3.1. 插件集成
官网链接:https://mybatis.org/generator/running/runningWithMaven.html
<build><plugins><!--逆向工程--><plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.2</version><configuration><verbose>true</verbose><!--代码生成重新覆盖--><overwrite>true</overwrite></configuration><dependencies><!--oracle--><dependency><groupId>com.oracle</groupId><artifactId>ojdbc6</artifactId><version>${oracle.version}</version></dependency><!--Mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency></dependencies></plugin></plugins></build>
3.2. 新建generatorConfig.xml文件
在resources目录下面
内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration><context id="testTables" targetRuntime="MyBatis3"><commentGenerator><!-- 是否去除自动生成的注释 true:是 : false:否 --><property name="suppressAllComments" value="true" /></commentGenerator><!--数据库连接的信息:驱动类、连接地址、用户名、密码 --><!-- <jdbcConnection driverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://localhost:3306/knowledge_base?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&nullCatalogMeansCurrent=true" userId="root"password="root"></jdbcConnection>--><!-- 数据库连接配置 --><jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"connectionURL="jdbc:oracle:thin:@192.168.0.12:1521:orcl"userId="scoptt"password="scoptt"></jdbcConnection><!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和NUMERIC 类型解析为java.math.BigDecimal --><javaTypeResolver><property name="forceBigDecimals" value="false" /></javaTypeResolver><!-- targetProject:生成PO类的位置 --><javaModelGenerator targetPackage="com.gblfy.mall.pojo"targetProject="src/main/java"><!-- enableSubPackages:是否让schema作为包的后缀 --><property name="enableSubPackages" value="false" /><!-- 从数据库返回的值被清理前后的空格 --><property name="trimStrings" value="true" /></javaModelGenerator><!-- targetProject:mapper映射文件生成的位置 --><sqlMapGenerator targetPackage="com.gblfy.mall.mapper"targetProject="src/main/java"><!-- enableSubPackages:是否让schema作为包的后缀 --><property name="enableSubPackages" value="false" /></sqlMapGenerator><!-- targetPackage:mapper接口生成的位置 --><javaClientGenerator type="XMLMAPPER"targetPackage="com.gblfy.mall.dao"targetProject="src/main/java"><!-- enableSubPackages:是否让schema作为包的后缀 --><property name="enableSubPackages" value="false" /></javaClientGenerator><!-- 指定数据库表 --><!--<table schema="" tableName="MQ_PRODUCER_LOG_TRACK"></table>--><table schema="" tableName="sys_user"></table><!-- 有些表的字段需要指定java类型<table schema="" tableName=""><columnOverride column="" javaType="" /></table> --></context>
</generatorConfiguration>
注:Mybatis Generator 配置详解
3.3.运行插件生成代码
或者载命令行执行以下命令:
mvn mybatis-generator:generate