年前,公司准备做招标系统,该系统中包含了项目管理模块、支付模块、公众号模块等等,这个准备工作的时间有点长,一直拖到现在才开始,LZ都等得有点不耐烦了。好了,吐槽完毕,回归正事:在该项目中,我主要负责的是支付模块的开发工作,LZ也从来没有接触相关的支付模块开发,只能一点一滴的做下去,不懂就问。 项目开始搭建也有两三天的时间了,从建库到springboot搭建,再到统一异常处理等等,今天就来说下使用MyBatis逆向工程生成dao、model和mapper。
一、建表
自行建表,不懂直接度娘,本项目中采用Oracle数据库。
二、maven中添加mybatis的相关依赖(以下代码只添加mybatis的依赖,其余springboot相关依赖自行添加)
注意:在resources目录下创建lib包,并将ojdbc6.jar(下载连接:https://www.jb51.net/softs/566175.html)拷贝进去,xml配置文件中添加oracle驱动包需要指定该包路径
<properties>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
</properties>
<build>
<plugins>
<!--添加mybatis generator maven插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!--generatorConfig.xml位置-->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
<!--此处必须添加oracle驱动包-->
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/resources/lib/ojdbc6.jar</systemPath>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
三、在resource目录下创建配置文件generatorConfig.xml。
Oracle配置(需要用到ojdbc6.jar包):
<?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>
<!-- 配置绝对路径指向ojdbc6.jar包 -->
<classPathEntry
location="E:\lib\ojdbc6.jar"/>
<context id="sqlserverTables" targetRuntime="MyBatis3">
<!-- 生成的pojo,将implements Serializable-->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 数据库链接URL、用户名、密码 -->
<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
connectionURL="jdbc:oracle:thin:@localhost:1521/orcl"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<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.gpdi.ebidding.pay.dao" targetProject="./src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 列出要生成代码的所有表 -->
<table tableName="PAY_BANK_ACCOUNT" domainObjectName="PayBankAccount"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="false"/>
</table>
<table tableName="PAY_BANK_TRANSACTIONS" domainObjectName="PayBankTransactions"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="false"/>
</table>
<table tableName="PAY_PAYMENT_LOG" domainObjectName="PayPaymentLog"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="false"/>
</table>
<table tableName="PAYMENT_ORDER_LOG_INFO" domainObjectName="PaymentOrderLogInfo"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="false"/>
</table>
<table tableName="PAY_PAYMENT_ORDER_INFO" domainObjectName="PayPaymentOrderInfo"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="false"/>
</table>
<table tableName="PAY_THIRD_PARTY_PAYMENT_INFO" domainObjectName="PayThirdPartyPaymentInfo"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="false"/>
</table>
</context>
</generatorConfiguration>
MySql配置主要将配置文件generatorConfig.xml中的classPathEntry(包下载连接:https://download.csdn.net/download/softtechnology/10013669)、jdbcConnection换成mysql相关的信息即可。
注意:如果数据库(Oracle或Mysql)表中存在字段是clob或text等类型,在mybatis逆向工程中,会生成*WithBLOBs.java代码,并继承entity实体类。
看个人是否需要不生成*WithBLOBs.java类,如果不需要生成,有两种办法:
第一种:修改数据库字段的类型(不用clob或text等类型)即可。
第二种:将*WithBLOBs.java中的属性字段添加到配置文件中(会存在失败的情况,如失败,请用第一种方法),如下所示:
<table tableName="PAYMENT_ORDER_LOG_INFO" domainObjectName="PaymentOrderLogInfo"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="false"/>
<!-- 添加属性,使不生成*WithBLOBs.java类 -->
<columnOverride column="businessRespJson" javaType="java.lang.String" jdbcType="VARCHAR"/>
<columnOverride column="businessReqJson" javaType="java.lang.String" jdbcType="VARCHAR"/>
</table>
四、点击Maven Projects下的mybatis-generator:generate即可生成dao、model和mapper
最终生成的结果图如下:
五、单元测试,即可添加数据到数据库
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class})
public class PayBankAccountMapperTest {
@Autowired(required = true)
PayBankAccountMapper payBankAccountMapper;
@Test
public void paymentOrderLogInfo() {
PayBankAccount payBankAccount = PayBankAccount.builder()
.accountId("1")
.accountName("root")
.depositBankName("123")
.bankAccountNo("2")
.parentAccountId("1")
.accountType("1")
.accountProp("1")
.bankSysCode("1")
.remark("1")
.state("1")
.createTime(new Date())
.updateTime(new Date()).build();
payBankAccountMapper.insert(payBankAccount);
}
}