使用Mybatis生成工具自动生成实体类和对应的mapper映射文件以及接口文件
2017-06-18 19:49
1041 查看
1.相关的jar包
Mybatis-Generator的下载可以到这个地址:https://github.com/mybatis/generator/releases
使用官方网站的mapper自动生成工具mybatis-generator-core-1.3.2来生成po类和mapper映射文件。
作用:mybatis官方提供逆向工程,可以使用它通过数据库中的表来自动生成Mapper接口和映射文件(单表增删改查)和Po类.
主要使用以下这几个jar包:
2.添加数据库中的表
CREATE TABLE `orders` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL COMMENT '下单用户id', `number` varchar(32) NOT NULL COMMENT '订单号', `createtime` datetime NOT NULL COMMENT '创建订单时间', `note` varchar(100) DEFAULT NULL COMMENT '备注', PRIMARY KEY (`id`), KEY `FK_orders_1` (`user_id`), CONSTRAINT `FK_orders_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; ------------------------------- CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(32) NOT NULL COMMENT '用户名称', `birthday` date DEFAULT NULL COMMENT '生日', `sex` char(1) DEFAULT NULL COMMENT '性别', `address` varchar(256) DEFAULT NULL COMMENT '地址', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8;
3.mapper生成配置文件:
在generatorConfig.xml中配置mapper生成的详细信息,注意以下几点:1、添加要生成的数据库表
2、po文件所在包路径
3、mapper文件所在包路径
<?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> <context id="testTables" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--数据库连接的信息:驱动类、连接地址、用户名、密码 mysql数据库连接--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password="123456"> </jdbcConnection> <!--oracle数据库连接--> <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver" connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg" userId="yycg" password="yycg"> </jdbcConnection> --> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:生成PO类的位置 --> <javaModelGenerator targetPackage="com.itheima.po" targetProject=".\src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetProject:mapper映射文件生成的位置 --> <sqlMapGenerator targetPackage="com.itheima.mapper" targetProject=".\src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- targetPackage:mapper接口生成的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.itheima.mapper" targetProject=".\src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 指定数据库表 --> <!-- <table tableName="items"></table> --> <table tableName="orders"></table> <!-- <table tableName="orderdetail"></table> --> <table tableName="user"></table> <!-- <table schema="" tableName="sys_user"></table> <table schema="" tableName="sys_role"></table> <table schema="" tableName="sys_permission"></table> <table schema="" tableName="sys_user_role"></table> <table schema="" tableName="sys_role_permission"></table> --> <!-- 有些表的字段需要指定java类型 <table schema="" tableName=""> <columnOverride column="" javaType="" /> </table> --> </context> </generatorConfiguration>
4.使用java类生成对应的po类和mapper文件:
package generate; import java.io.File; import java.util.ArrayList; import java.util.List; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; public class Generate { public void generator() throws Exception{ List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File("config/generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } public static void main(String[] args) throws Exception { try { Generate generatorSqlmap = new Generate(); generatorSqlmap.generator(); } catch (Exception e) { e.printStackTrace(); } } }
5.Mapper文件内容不覆盖而是追加
XXXMapper.xml文件已经存在时,如果进行重新生成则mapper.xml文件内容不被覆盖而是进行内容追加,结果导致mybatis解析失败。解决方法:删除原来已经生成的mapper xml文件再进行生成。
Mybatis自动生成的po及mapper.java文件不是内容而是直接覆盖没有此问题。
相关文章推荐
- 使用Mybatis-Generator自动生成entity实体、dao接口以及mapper映射文件
- 使用mybatis-generator自动生成实体类,接口实现类和Mapper映射配置文件
- mybatis逆向工程自动生成实体类、接口以及映射Mapper.xml配置文件
- 简单三步快速学会使用Mybatis-Generator自动生成entity实体、dao接口以及mapper映射文件(postgre使用实例)
- Mybatis自动生成实体类,映射文件,以及dao层接口。
- 使用mybatis自带工具,自动生成表对应domain、mapper.xml以及dao
- 使用mybatis自带工具,自动生成表对应domain、mapper.xml以及dao
- 使用mybatis自带工具,自动生成表对应domain、mapper.xml以及dao
- 使用mybatis自动生成工具,生成对应的类,配置文件,接口
- 使用mybatis自带工具,自动生成表对应domain、mapper.xml以及dao
- mybatis如何自动生成实体类,映射文件以及对应的dao层
- 如何用MyBatis-Generator自动创建代码(映射生成实体类、DAO接口和Mapping映射文件)
- 使用mybatis的generator自动生成po及mapper映射文件
- 利用MyBatis生成器自动生成实体类、DAO接口和Mapping映射文件
- 整合maven+mybatis+generator生成java自定义model实体类,dao接口和mapper映射文件
- Mybatis--使用mybatis generator插件映射数据库,自动生成pojo对象,dao接口,mapper.xml文件的方法
- MyBatis自动生成实体类、DAO接口和Mapping映射文件的代码(逆向工程)
- 在eclipse中使用MyBatis Geneator自动生成出对应数据库的实体与映射文件
- 使用Mybatis-Generator逆向生成po,映射文件和mapper接口
- SSM框架搭建(三) 数据库创建和MyBatis生成器自动生成实体类、DAO接口和Mapping映射文件