自动生成mybatis代码

由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以可以使用mybatis-gennerator插件帮我们自动生成mybatis所需要的dao、bean、mapper xml文件。

一、新建Maven项目

二、修改pom.xml文件

在pom.xml文件中添加如下内容:

 1 <build>
 2     <plugins>
 3         <plugin>
 4             <groupId>org.mybatis.generator</groupId>
 5             <artifactId>mybatis-generator-maven-plugin</artifactId>
 6             <version>1.3.2</version>
 7             <dependencies>
 8                 <dependency>
 9                     <groupId>mysql</groupId>
10                         <artifactId>mysql-connector-java</artifactId>
11                         <version>5.1.35</version>
12                     </dependency>
13             </dependencies>
14             <configuration>  
15                 <verbose>true</verbose>  
16         <overwrite>true</overwrite>  
17         </configuration>
18         </plugin>
19     </plugins> 
20 </build>    

 

三、修改generator配置

在 /src/main/resources 创建 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     <context id="MavenMybatisDemo" targetRuntime="MyBatis3">
 7         <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>  
 8         <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> 
 9          <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin> 
10          
11         <commentGenerator>
12             <!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示包含 -->
13             <!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true -->
14             <property name="suppressDate" value="true" />
15             <!-- 是否去除自动生成的注释 true:是 : false:否 -->
16             <property name="suppressAllComments" value="false" />
17         </commentGenerator>
18         
19         <!--数据库链接URL,用户名、密码 -->
20         <jdbcConnection driverClass="com.mysql.jdbc.Driver"
21             connectionURL="jdbc:mysql://127.0.0.1:3306/demo" userId="root" password="">
22         </jdbcConnection>
23         
24         <javaTypeResolver>
25             <!-- 把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal, 默认false -->
26             <property name="forceBigDecimals" value="false" />
27         </javaTypeResolver>
28         
29         <!-- targetProject:自动生成代码的位置 -->
30         <!-- 生成模型的包名和位置 -->
31         <javaModelGenerator targetPackage="org.waxdl.model" targetProject="src/main/java">
32             <!-- enableSubPackages:是否让schema作为包的后缀 --> 
33             <property name="enableSubPackages" value="true" />
34             <!-- 从数据库返回的值被清理前后的空格  -->
35             <property name="trimStrings" value="true" />
36         </javaModelGenerator>
37         
38         <!-- 生成映射文件的包名和位置 -->
39         <sqlMapGenerator targetPackage="org.waxdl.dao" targetProject="src/main/java">
40             <property name="enableSubPackages" value="true" />
41         </sqlMapGenerator>
42         
43         <!-- 生成DAO的包名和位置 -->
44         <javaClientGenerator type="XMLMAPPER"
45             targetPackage="org.waxdl.dao" implementationPackage="org.waxdl.dao.impl"  targetProject="src/main/java">
46             <property name="enableSubPackages" value="true" />
47         </javaClientGenerator>
48         
49         <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->
50         <table tableName="user" domainObjectName="User"
51             enableCountByExample="false" enableUpdateByExample="false"
52             enableDeleteByExample="false" enableSelectByExample="false"
53             selectByExampleQueryId="false"></table>
54     </context>
55 </generatorConfiguration>

 

四、生成代码

方法一:mvn mybatis-generator:generate
 
方法二:选中pom.xml运行Run As - Maven build ,在 Goals里填写:mybatis-generator:generate,然后 run,如图:
 
 

五、生成后的项目结构

 
posted @ 2018-05-09 17:30  waxdl  阅读(151)  评论(0)    收藏  举报