使用mybatis-generator生成dao层操作

使用mybatis-generator生成dao层操作

一 pom文件的配置添加

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

<version>${mybatis.version}</version>

</dependency>

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId>

<version>${mybatis.spring.version}</version>

</dependency>

<dependency>

<groupId>org.mybatis.generator</groupId>

<artifactId>mybatis-generator-core</artifactId>

<version>1.3.2</version>

</dependency>

<plugin>

<groupId>org.mybatis.generator</groupId>

<artifactId>mybatis-generator-maven-plugin</artifactId>

<version>1.3.2</version>

<configuration>

<verbose>true</verbose>

<overwrite>true</overwrite>

<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>

</configuration>

<dependencies>

<dependency>

<!--写依赖的版本jar-->

</dependency>

</dependencies>

</plugin>

二 在resources中增加配置文件

/src/main/resources/datasource.properties

#数据库驱动配置

db.driverLocation=/Users/apple/.m2/repository/mysql/mysql-connector-java/5.1.38/mysql-connector-java-5.1.38.jar

#驱动类的包名

db.driverClassName=com.mysql.jdbc.Driver

#连接数据库的url

db.url=jdbc:mysql://127.0.0.1:3306/test

#用户名

db.username=user1

#密码

db.password=password1

src/main/resources/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>

<!--导入属性配置-->

<properties resource="datasource.properties"/>

<!--指定特定数据库的jdbc驱动jar包的位置-->

<classPathEntry location="${db.driverLocation}"/>

<!-- 选择运行的mybatis版本 -->

<context id="context" defaultModelType="flat" targetRuntime="MyBatis3">

<property name="javaFileEncoding" value="UTF-8"/>

<!-- <plugin type="com.use.mybatisgenerator.SelectByEntityPlugin"></plugin>-->

<!-- optional,旨在创建class时,对注释进行控制 -->

<commentGenerator type="com.use.mybatisgenerator.MyCommentGenerator">

<property name="suppressDate" value="true"/>

<property name="suppressAllComments" value="true"/>

<property name="commentEncoding" value="UTF-8"/>

</commentGenerator>

<!--jdbc的数据库连接 -->

<jdbcConnection

driverClass="${db.driverClassName}"

connectionURL="${db.url}"

userId="${db.username}"

password="${db.password}">

</jdbcConnection>

<!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->

<javaTypeResolver>

<property name="forceBigDecimals" value="false"/>

</javaTypeResolver>

<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类

targetPackage 指定生成的model生成所在的包名

targetProject 指定在该项目下所在的路径

-->

<javaModelGenerator targetPackage="com.use.model" targetProject="./src/main/java">

<!-- 是否允许子包,即targetPackage.schemaName.tableName -->

<property name="enableSubPackages" value="true"/>

<!-- 是否对model添加 构造函数 -->

<property name="constructorBased" value="true"/>

<!-- 是否对类CHAR类型的列的数据进行trim操作 -->

<property name="trimStrings" value="true"/>

<!-- 建立的Model对象是否不可改变 即生成的Model对象不会有 setter方法,只有构造方法 -->

<property name="immutable" value="false"/>

</javaModelGenerator>

<!--mapper xml映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->

<sqlMapGenerator targetPackage="com.use.mapper" targetProject="./src/main/resources">

<property name="enableSubPackages" value="true"/>

</sqlMapGenerator>

<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码

type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象

type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象

type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口

-->

<!-- targetPackage:dao层mapper接口生成的位置 -->

<javaClientGenerator type="XMLMAPPER" targetPackage="com.use.mapper" targetProject="./src/main/java">

<!-- enableSubPackages:是否让schema作为包的后缀 -->

<property name="enableSubPackages" value="true" />

</javaClientGenerator>

<!--<table tableName="core_lend_request" enableCountByExample="false"-->

<!--enableDeleteByPrimaryKey="false" enableDeleteByExample="false"-->

<!--enableSelectByExample="false" enableUpdateByExample="false"-->

<!--selectByExampleQueryId="false">-->

<!--<generatedKey column="id" sqlStatement="JDBC" />-->

<!--<columnOverride column="status"-->

<!--javaType="com.puhui.core.common.CoreLendRequestStatus" />-->

<!--<columnOverride column="pay_source"-->

<!--javaType="com.puhui.core.common.PaySource" />-->

<!--<columnOverride column="coop_mode"-->

<!--javaType="com.puhui.core.common.CooperationMode" />-->

<!--<columnOverride column="reduce_service_version"-->

<!--javaType="com.puhui.core.enums.ReduceServiceVersionEnum" />-->

<!--</table>-->

<table tableName="core_lend_request_deb_transfer"

enableCountByExample="false" enableDeleteByPrimaryKey="false"

enableDeleteByExample="false" enableSelectByExample="false"

enableUpdateByExample="false" selectByExampleQueryId="false">

<generatedKey column="id" sqlStatement="JDBC" />

</table>

</context>

</generatorConfiguration>

三 重写注释生成的类

MyCommentGenerator.java

public class MyCommentGenerator implements CommentGenerator {

private static final Logger logger = LoggerFactory.getLogger(MyCommentGenerator.class);

private Properties properties;

/**

* 是否忽略注释

*/

private boolean suppressAllComments;

/**

* 生成注释编码格式

*/

private String commentEncoding;

public MyCommentGenerator() {

super();

properties = new Properties();

this.commentEncoding = "UTF-8";

this.suppressAllComments = false;

}

@Override public void addConfigurationProperties(Properties properties) {

this.properties.putAll(properties);

this.commentEncoding = this.properties.getProperty("commentEncoding");

}

@Override public void addFieldComment(Field field, IntrospectedTable introspectedTable,

IntrospectedColumn introspectedColumn) {

field.addJavaDocLine("/**");

String remarks = introspectedColumn.getRemarks();

if (null != remarks && !"".equals(remarks)) {

try {

byte[] remarksBytes = remarks.getBytes(commentEncoding);

String remarksTemp = new String(remarksBytes);

field.addJavaDocLine(" * " + remarksTemp);

} catch (UnsupportedEncodingException e) {

logger.error("异常:{}", e.getMessage(), e);

throw new RuntimeException("commentEncoding 值不支持" + e.getMessage());

}

field.addJavaDocLine(" * " + MergeConstants.NEW_ELEMENT_TAG);

field.addJavaDocLine(" */");

}

}

@Override public void addFieldComment(Field field, IntrospectedTable introspectedTable) {

}

@Override public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {

}

@Override public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean b) {

}

@Override public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {

}

@Override public void addGetterComment(Method method, IntrospectedTable introspectedTable,

IntrospectedColumn introspectedColumn) {

addJavadocTag(method);

}

@Override public void addSetterComment(Method method, IntrospectedTable introspectedTable,

IntrospectedColumn introspectedColumn) {

addJavadocTag(method);

}

@Override public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {

}

@Override public void addJavaFileComment(CompilationUnit compilationUnit) {

compilationUnit.addFileCommentLine("/**");

compilationUnit

.addFileCommentLine("* Copyright(c) 2018-" + Calendar.getInstance().get(Calendar.YEAR) + "by kxd inc");

compilationUnit.addFileCommentLine("* all rights reserved");

compilationUnit.addFileCommentLine("*/");

}

@Override public void addComment(XmlElement xmlElement) {

if (suppressAllComments) {

return;

}

xmlElement.addElement(new TextElement("<!-- " + MergeConstants.NEW_ELEMENT_TAG + "-->"));

}

@Override public void addRootComment(XmlElement xmlElement) {

}

public void addJavadocTag(JavaElement javaElement) {

if (suppressAllComments) {

return;

}

javaElement.addJavaDocLine("/**");

javaElement.addJavaDocLine("* " + MergeConstants.NEW_ELEMENT_TAG);

javaElement.addJavaDocLine("*/");

}

}

通过插件运行,生成mapper,xml,dto等文件

猜你喜欢

转载自my.oschina.net/iioschina/blog/1818666