Mybatis数据库表自动生成entity,dao口,mapper.xml
附件下载链接:下载
- 在数据库中创建表
- 修改generator.xml文件
。数据库驱动包路径:替换成所使用数据库对应的驱动所放置的路径
。数据库链接URL、用户名、密码:替换成自己项目所使用的数据库
。生成实体类的包名和位置:是实体类的包名即目录,不是实体类名称,建议与项目的一致,这样生成的文件可以直接拷贝到项目中使用
。生成的mapper.xml包名和位置:同上
。生成DAO接口的包名和位置:同上
。要生成的数据库表(更改tableName和domainObjectName就可以):替换成自己要生成文件的数据库表。
PS:domainObjectName为实体类名称,生成的这三个文件都会以domainObjectName定义文件名
<?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>
<!-- 数据库驱动包位置 -->
<!-- 下列注释则为mysql数据库 -->
<!--<classPathEntry location="D:\generator\mysql-connector-java-5.1.34.jar" /> -->
<!-- 本例使用oracle数据库 -->
<classPathEntry location="C:\lib\ojdbc14.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库链接URL、用户名、密码 -->
<!--<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhots:3306/my_db?characterEncoding=utf8" userId="root" password="123456"> -->
<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@192.168.12.87:1521:orcl" userId="trustdis" password="trustdis"></jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成实体类的包名和位置 -->
<javaModelGenerator targetPackage="com.demo.entity" targetProject="d:\generator\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成的mapper.xml包名和位置 -->
<sqlMapGenerator targetPackage="com.demo.mapper" targetProject="d:\generator\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO接口的包名和位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.demo.dao" targetProject="d:\generator\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成的数据库表(更改tableName和domainObjectName就可以,domainObjectName为实体类名称,生成的这三个文件都会以domainObjectName定义文件名) -->
<table tableName="TRUSTDIS_SETTING" domainObjectName="KbCity" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<!-- <table tableName="course_info" domainObjectName="CourseInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="course_user_info" domainObjectName="CourseUserInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> -->
</context>
</generatorConfiguration>
- 打开电脑的命令提示符(win+r),进入generator.xml文件和mybatis-generator-core-1.3.2.jar所在的的位置,或者在瓷盘中直接找到该位置,然后在窗口的路径行输入cmd并回车,如图:
- 运行命令:java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite,出现MyBatis Generator finished successfully.则成功生成