设计数据库字段后,可直接利用mybatis工具生成Pojo类和CRUD操作的xml。节省开发时间~ 但是有的代码并不是很好用,可以自行添加或修改。
1、准备:
IDEA安装mybatis plugin插件。
<img src="http://upload-images.jianshu.io/upload_images/3116838-202ed057f1e523bb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
然后替换一下目录中的batis插件的jar包(多金的可以直接付费使用,支持正版)。
<img src="http://upload-images.jianshu.io/upload_images/3116838-42e96964fcbd6504.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
破解jar包分享地址:http://pan.baidu.com/s/1jHDDSqE 密码:7gdc
2、数据库SQL,创建一张简单的用户表:
-- DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`userName` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`createTime` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`lastUpdateTime` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`userId` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`userId`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;
3、pom.xml文件配置(依赖部分)
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.22</version>
<scope>runtime</scope>
</dependency>
<!-- mybatis generator 自动生成代码插件 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
</project>
4、创建mybatisGenerator.xml配置文件。
安装插件后可以在resource->new中看到 'mybatis-generator-config',生成即可。
![](http://upload-images.jianshu.io/upload_images/3116838-b406cf78e8d50c61.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240 "width=50% height=50%)
(这里如果没有默认起名为mybatisGenerator.xml,启动maven时会报错)。生成后修改文件,具体内容如下。
<?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>
<!-- !!!! Driver Class Path !!!! -->
<!-- mysql驱动jar包路径 -->
<classPathEntry location=":/Files/repository/mysql/mysql-connector-java/5.1.38/mysql-connector-java-5.1.38.jar"/>
<context id="context" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="false"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<!-- !!!! Database Configurations !!!! -->
<!-- 数据库驱动和连接配置 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/mystudy" userId="root" password="123456"/>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- !!!! Model Configurations !!!! -->
<javaModelGenerator targetPackage="generator.model" targetProject="src/main/java/mybatis">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- !!!! Mapper XML Configurations !!!! -->
<sqlMapGenerator targetPackage="generator.mapping" targetProject="src/main/java/mybatis">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- !!!! Mapper Interface Configurations !!!! -->
<javaClientGenerator targetPackage="generator.dao" targetProject="src/main/java/mybatis" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- !!!! Table Configurations !!!! -->
<!-- 设置数据库表名 -->
<table tableName="user" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false"/>
</context>
</generatorConfiguration>
5、配置maven启动项
mybatis-generator:generate -e
<img src="http://upload-images.jianshu.io/upload_images/3116838-9c111752cdcc95cd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
自动生成前的目录结构
<img src="http://upload-images.jianshu.io/upload_images/3116838-f64c234ee5a09775.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
自动生成后的目录结构
<img src="http://upload-images.jianshu.io/upload_images/3116838-8f770f717e396c5d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
So easy,and 强大的不行~
以后如果增加数据库表的字段,重新自动生成一下代码就好了。但是如果在表中改字段名,或是有其他关联操作,就不好说了。所以在工程中最好把自动生成的代码单独拎出一份,其他业务需求的实现放在其他文件中,尽量互不影响。