本文参考:http://www.mybatis.org/generator/index.html
利用mybatis自动生成工具来生成基本的JavaBean 以及 mapper文件以及dao
需要jar包:
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>
<!-- 数据库驱动 -->
<classPathEntry
location="D:/sharepackage/mysql-connector-java-5.1.41-bin.jar" />
<!-- 一个数据库一个context targetRuntime是指定生成代码的运行时目标 -->
<!-- MyBatis3是默认值 使用该值,MBG将生成与MyBatis 3.0及更高版本以及JSE 5.0及更高版本兼容的对象-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 定义注释生成器的属性 -->
<commentGenerator>
<!-- 是否生成带注释的时间戳 -->
<property name="suppressDate" value="true" />
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test" userId="liujd"
password="1234">
</jdbcConnection>
<!-- 用于定义java类型解析器的属性 用于从数据库列信息计算java类型 -->
<javaTypeResolver>
<!-- 是否强制对decimal和number字段使用java.math.BigDecimal -->
<property name="forceBigDecimals" value="true" />
</javaTypeResolver>
<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="com.model"
targetProject="arithmetic">
<!-- 是否在当前路径下新加一层schema -->
<property name="enableSubPackages" value="true" />
<!-- 是否去除从数据库查出的数据两端的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成映射文件的包名和位置 -->
<sqlMapGenerator targetPackage="com.mapper"
targetProject="arithmetic">
<!-- 是否在当前路径下新加一层schema -->
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.dao" targetProject="arithmetic">
<!-- 是否在当前路径下新加一层schema -->
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成哪些表 -->
<table tableName="student" domainObjectName="student"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<generatedKey column="id" sqlStatement="JDBC" identity="true" />
<!-- domainObjectName将生成的对象名 其他表示是否生成一个Example类 -->
</table>
</context>
</generatorConfiguration>
利用eclipse运行:
右击配置文件 然后点击如下选项就可