前面有篇文章介绍了eclipse插件自动生成表的实体类,优点多多:自动生成spring的整合文件、mapper里面的栗子也非常的详细,但是最近遇到了问题:
数据库里面表的字段中带有“”_“下划线,我们知道插件默认的是将这些带有下划线的字段默认的变成“优美的驼峰式”的。表是肯定不能动的,实体类的字段也是非常多,改起来非常麻烦,所以就研究了下面这种依靠代码来实现的方式。
首先:下载Mybatis插件 链接: http://pan.baidu.com/s/1dEAWNlf 密码: b1yj
然后是正常的安装插件的方法,解压完之后将文件夹的文件对应的加到eclipse安装目录对应的文件夹下。重启eclipse即可。
下面的比较重要了。
新建完项目之后点击新建
然后这样:
生成完之后
最后是修改配置文件:
<?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>
<classPathEntry
location="E:\m2Repo\mysql\mysql-connector-java\5.1.29\mysql-connector-java-5.1.29.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://172.16.14.40:3306/zhu"
userId="zhu" password="zhu" />
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL
和 NUMERIC 类型解析为java.math.BigDecimal -->
</javaTypeResolver>
<javaModelGenerator targetPackage="com.tt.domain"
targetProject="MybatisT/src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.tt.domain"
targetProject="MybatisT/src/main/resources">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<table tableName="zlpg_value" enableSelectByExample="false"
enableDeleteByExample="false" enableCountByExample="false"
selectByExampleQueryId="true" enableUpdateByExample="false">
<property name="useActualColumnNames" value="true"/>
<!-- <generatedKey column="ID" sqlStatement="oracle" identity="true" /> -->
</table>
</context>
</generatorConfiguration>
上面只是最基本的配置,详细的功能参照:
http://blog.csdn.net/zt_fucker/article/details/53782502
其中最主要的代码是下面这段
<property name="useActualColumnNames" value="true"/>
<!-- 如果设置为true,生成的model类会直接使用column本身的名字,而不会再使用驼峰命名方法,比如BORN_DATE,生成的属性名字就是BORN_DATE,而不会是bornDate -->
保存之后再点击,最后生成代码:
OK