您的位置:首页 > 编程语言

使用Mybatis generator生成代码

2017-12-28 11:30 309 查看

环境信息

构建工具为Maven

数据库为MySQL

准备pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>my.test</groupId>
<artifactId>mybatis</artifactId>
<version>1.0.0</version>

<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>

<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
</dependencies>

<configuration>
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
</configuration>
</plugin>
</plugins>
</build>
</project>


configurationFile可以不指定,默认就是${basedir}/src/main/resources/generatorConfig.xml

官方网址:http://www.mybatis.org/generator/running/runningWithMaven.html

准备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>
<context id="servicesupport" targetRuntime="MyBatis3">
<property name="javaFileEncoding" value="UTF-8" />

<jdbcConnection connectionURL="jdbc:mysql://10.23.0.79:3306/servicesupport"
driverClass="com.mysql.jdbc.Driver" password="location" userId="location" />

<javaModelGenerator targetPackage="my.test.mybatis.model" targetProject="MAVEN" />

<sqlMapGenerator targetPackage="mybatis" targetProject="MAVEN" />

<javaClientGenerator targetPackage="my.test.mybatis.persist" targetProject="MAVEN" type="XMLMAPPER" />

<table tableName="t_safe_apkrightlist_bak" domainObjectName="ApkRightlistBak">
<generatedKey column="id" sqlStatement="JDBC" identity="true" />
</table>

</context>
</generatorConfiguration>


官方网址:http://www.mybatis.org/generator/configreference/xmlconfig.html

在Maven工程中将targetProject设置为MAVEN有特殊含义,表示生成的代码会放在MAVEN的target目录中,且会自动生成targetPackage指定的目录结构

执行mvn mybatis-generator:generate

│  pom.xml
│
├─src
│  └─main
│      └─resources
│              generatorConfig.xml
│
└─target
└─generated-sources
└─mybatis-generator
├─my
│  └─test
│      └─mybatis
│          ├─model
│          │      ApkRightlistBak.java
│          │      ApkRightlistBakExample.java
│          │      ApkRightlistBakWithBLOBs.java
│          │
│          └─persist
│                  ApkRightlistBakMapper.java
│
└─mybatis
ApkRightlistBakMapper.xml


业务代码

ApkRightlistBakMapper apkRightlistBakMapper = sqlTemplate.getMapper(ApkRightlistBakMapper.class);

ApkRightlistBakExample apkRightlistBakExample = new ApkRightlistBakExample();
apkRightlistBakExample.or().andApknameEqualTo("xxx").andTypeEqualTo(1);
apkRightlistBakExample.or().andApknameEqualTo("yyy").andTypeEqualTo(2);
apkRightlistBakExample.setOrderByClause("id asc");

List<ApkRightlistBakWithBLOBs> apkRightlistBakWithBLOBsList = apkRightlistBakMapper.selectByExampleWithBLOBs(apkRightlistBakExample);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mybatis-generator