使用MyBatis Generator自动创建代码(dao,mapping,poji)

时间:2024-01-03 16:47:38
连接的数据库为SQL server2008,所以需要的文件为sqljdbc4.jar

使用的lib库有:

使用MyBatis Generator自动创建代码(dao,mapping,poji)

在lib库目录下新建一个src文件夹用来存放生成的文件,然后新建generatorConfig.xml

里面代码为:

  1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE generatorConfiguration
3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
5 <generatorConfiguration>
6 <!-- 数据库驱动-->
7 <!-- sqljdbc4.jar是SQLServer数据库连接jar包,如果要连接MySQL数据库直接把sqljdbc4.jar改成mysql-connector-java-5.1.25-bin.jar -->
8 <classPathEntry location="sqljdbc4.jar"/>
9 <context id="DB2Tables" targetRuntime="MyBatis3">
10 <commentGenerator>
11 <property name="suppressDate" value="true"/>
12 <!-- 是否去除自动生成的注释 true:是 : false:否 -->
13 <property name="suppressAllComments" value="true"/>
14 </commentGenerator>
15 <!--数据库链接URL,用户名、密码 -->
16 <!--连接数据SQLServer -->
17 <jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
18
19 connectionURL="jdbc:sqlserver://127.0.0.1:1433;databaseName=dbSSMTEST" userId="sa" password="123">
20 <!--连接数据库MySQL -->
21 <!-- <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.1.10:3306/ypzlmanagement"
22
23 userId="root" password="hewei123"> -->
24 </jdbcConnection>
25 <javaTypeResolver>
26 <property name="forceBigDecimals" value="false"/>
27 </javaTypeResolver>
28 <!-- 生成模型的包名和位置-->
29 <!-- targetPackage="com.ssm.pojo"是生成代码的存放路径这里根据你的路径存放 -->
30 <javaModelGenerator targetPackage="com.ssm.pojo" targetProject="src">
31 <property name="enableSubPackages" value="true"/>
32 <property name="trimStrings" value="true"/>
33 </javaModelGenerator>
34 <!-- 生成映射文件的包名和位置-->
35 <!-- targetPackage="com.ssm.pojo"是生成代码的存放路径这里根据你的路径存放 -->
36 <sqlMapGenerator targetPackage="com.ssm.mapping" targetProject="src">
37 <property name="enableSubPackages" value="true"/>
38 </sqlMapGenerator>
39 <!-- 生成DAO的包名和位置-->
40 <!-- targetPackage="com.ssm.pojo"是生成代码的存放路径这里根据你的路径存放 -->
41 <javaClientGenerator type="XMLMAPPER" targetPackage="com.ssm.dao" targetProject="src">
42 <property name="enableSubPackages" value="true"/>
43 </javaClientGenerator>
44 <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
45 <!-- tableName是要生成数据库映射文件的表名 domainObjectName要生成代码的实体类名 根据自己需求修改 -->
46 <table tableName="student" domainObjectName="student" enableCountByExample="false" enableUpdateByExample="false"
47
48 enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
49 </context>
50 </generatorConfiguration>

最后在cmd控制台下找到lib的根目录然后执行以下语句

Java -jar mybatis-generator-core-1.3.5.jar -configfile generatorConfig.xml -overwrite

如图所示:

使用MyBatis Generator自动创建代码(dao,mapping,poji)

然后在文件夹目录下可以看见自动生成的文件

使用MyBatis Generator自动创建代码(dao,mapping,poji)