使用Mybatis框架逆向工程

来源:互联网 发布:淘宝买家数据 编辑:程序博客网 时间:2024/05/21 12:03

什么是是逆向工程

mybatis官方提供的一种根据数据库表生成mybatis执行代码的工具,会自动生成mapper.xml映射文件,mapper.java实体类和pojo接口,减少了简单代码的书写

准备工作

  • 下载mybatis核心包和逆向工程包mybatis下载地址
  • 下载要连接数据库的驱动包
  • 创建一个数据库,并且在里面建立好需要的表,我的使用的数据库如下:
/*SQLyog v10.2 MySQL - 5.1.72-community : Database - mybatis**********************************************************************//*!40101 SET NAMES utf8 */;/*!40101 SET SQL_MODE=''*/;/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;/*Table structure for table `items` */CREATE TABLE `items` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `name` varchar(32) NOT NULL COMMENT '商品名称',  `price` float(10,1) NOT NULL COMMENT '商品定价',  `detail` text COMMENT '商品描述',  `pic` varchar(64) DEFAULT NULL COMMENT '商品图片',  `createtime` datetime NOT NULL COMMENT '生产日期',  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;/*Table structure for table `orderdetail` */CREATE TABLE `orderdetail` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `orders_id` int(11) NOT NULL COMMENT '订单id',  `items_id` int(11) NOT NULL COMMENT '商品id',  `items_num` int(11) DEFAULT NULL COMMENT '商品购买数量',  PRIMARY KEY (`id`),  KEY `FK_orderdetail_1` (`orders_id`),  KEY `FK_orderdetail_2` (`items_id`),  CONSTRAINT `FK_orderdetail_1` FOREIGN KEY (`orders_id`) REFERENCES `orders` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,  CONSTRAINT `FK_orderdetail_2` FOREIGN KEY (`items_id`) REFERENCES `items` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;/*Table structure for table `orders` */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;/*Table structure for table `user` */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=27 DEFAULT CHARSET=utf8;/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
  • 创建一个java工程,导入上述三个包,并且建好自己的包用于存放逆向工程生成的文件
  • 书写配置文件generatorCongig.xml,可以在说明文档下面的位置找到配置文件的模板:
Alt text
<?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="DB2Tables" targetRuntime="MyBatis3">  <!-- 连接数据库 -->      <!-- 指定驱动,数据库地址,用户名和登录密码 -->    <jdbcConnection driverClass="com.mysql.jdbc.Driver"          connectionURL="jdbc:mysql://localhost:3306/mybatis"          userId="root"          password="">      </jdbcConnection>      <javaTypeResolver >      <property name="forceBigDecimals" value="false" />    </javaTypeResolver>    <!-- 生成po实体类包结构 -->    <javaModelGenerator targetPackage="cn.wangyequn.model" targetProject="\MBGTestProject\src">      <property name="enableSubPackages" value="true" />      <property name="trimStrings" value="true" />    </javaModelGenerator>    <!-- 生成mapper.xml的位置 -->    <sqlMapGenerator targetPackage="test.xml"  targetProject="\MBGTestProject\src">      <property name="enableSubPackages" value="true" />    </sqlMapGenerator>    <!-- 生成mapper.java接口文件包结构 -->    <javaClientGenerator type="XMLMAPPER" targetPackage="cn.wangyequn.dao"  targetProject="\MBGTestProject\src">      <property name="enableSubPackages" value="true" />    </javaClientGenerator>    <!-- 要参与逆向工程的表 -->    <table tableName="items" </table>    <table tableName="orderdetail" </table>    <table tableName="orders" </table>    <table tableName="user" </table>  </context></generatorConfiguration>

执行并生成代码

在文档中找到下面的位置Alt text新建一个类,搞个main方法出来,把代码考进去,运行
package cn.wangyequn.utils;import java.io.File;import java.io.IOException;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.exception.XMLParserException;import org.mybatis.generator.internal.DefaultShellCallback;public class In {    public static void main(String[] args) throws Exception {           List<String> warnings = new ArrayList<String>();           boolean overwrite = true;           File configFile = new File("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);    }}
然后你就会发现在之前自己的包里自动生成了想要的代码!

 

原创粉丝点击