Mybatis之逆向工程

mybatis逆向工程能做什么?

  • 首先逆向工程的作用就是帮我们把 已经存在的数据表 生成他们的pojo对象。

接下来,我们开始逆向工程吧~

  1. 建立一个数据库,名为codepay,运行如下sql,生成数据表
/*
Navicat MySQL Data Transfer

Source Server         : 本地
Source Server Version : 80013
Source Host           : 127.0.0.1:3306
Source Database       : codepay

Target Server Type    : MYSQL
Target Server Version : 80013
File Encoding         : 65001

Date: 2019-03-22 18:19:55
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for t_code
-- ----------------------------
DROP TABLE IF EXISTS `t_code`;
CREATE TABLE `t_code` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `xsdcode` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `used` varchar(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `subject` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `xsdCode` (`xsdcode`)
) ENGINE=InnoDB AUTO_INCREMENT=234 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Table structure for t_pay
-- ----------------------------
DROP TABLE IF EXISTS `t_pay`;
CREATE TABLE `t_pay` (
  `id` varchar(255) NOT NULL,
  `payname` varchar(255) NOT NULL,
  `payamout` double(255,0) NOT NULL,
  `paytime` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  `paystaus` varchar(255) NOT NULL,
  `mail` varchar(255) NOT NULL,
  `auditpass` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  `xsdcode` int(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `xsdCode` (`xsdcode`),
  CONSTRAINT `t_pay_ibfk_1` FOREIGN KEY (`xsdcode`) REFERENCES `t_code` (`id`) ON DELETE SET NULL ON UPDATE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

数据库信息如下:

Mybatis之逆向工程_第1张图片

  1. 首先创建一个maven工程(为什么用maven工程呢?因为jar包管理起来不方便,没有学过maven的同学,可以先了解一下maven)
    pom依赖如下:
 <dependencies>
  	<dependency>
	    <groupId>org.mybatis.generatorgroupId>
	    <artifactId>mybatis-generator-coreartifactId>
	    <version>1.3.7version>
	dependency>
	<dependency>
		<groupId>mysqlgroupId>
		<artifactId>mysql-connector-javaartifactId>
		<version>8.0.11 version>
	dependency>
	<dependency>
		 <groupId>org.mybatisgroupId>
         <artifactId>mybatisartifactId>
		 <version>3.4.5version>
	dependency>

  dependencies>
  1. 配置 逆向工程的xml文件



<generatorConfiguration>


  <context id="DB2Tables" targetRuntime="MyBatis3">
    <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/codepay?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8"
        userId="root"
        password="123456">
    jdbcConnection>

    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    javaTypeResolver>

    <javaModelGenerator targetPackage="com.raven.pojo" targetProject=".\src">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    javaModelGenerator>

    <sqlMapGenerator targetPackage="config"  targetProject=".\resources">
      <property name="enableSubPackages" value="true" />
    sqlMapGenerator>

    <javaClientGenerator type="XMLMAPPER" targetPackage="com.raven.dao"  targetProject=".\src">
      <property name="enableSubPackages" value="true" />
    javaClientGenerator>

    
    <table tableName="t_code" domainObjectName="Code">table>
    <table tableName="t_pay" domainObjectName="Pay">table>

  context>
generatorConfiguration>

介绍一下里面主要标签的作用

  • jdbcConnection配置自己的数据源信息

  • targetPackage指定自动生成的pojo的包名
    targetProject指定路径

  • targetPackage指定自动生成的mapper.xml文件的包名
    targetProject指定路径

  • targetPackage指定自动生成的dao层接口的包名
    targetProject指定路径

  • tableName表示数据表的名字
    domainObjectName表示POJO的名字
    4.运行代码,生成 pojo 实体类
    public class MybatisGener {
    	public static void main(String[] args) throws Exception {
    		   List<String> warnings = new ArrayList<String>();
    		   boolean overwrite = true;
    		   // 逆向工程的配置文件
    		   File configFile = new File("resources/generatorConfig.xml");
    		   System.out.println(configFile.exists());
    		   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);
    	}
    }
    

    生成之后的工程目录如下:
    Mybatis之逆向工程_第2张图片
    接下来,我们玩一玩mybatis为我们生成的接口,真的很强大。尤其是生成的 *Example,查询起来真的很好玩。

    public class GenerTest {
    		public static void main(String[] args) throws IOException {
    			
    			InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
    			SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);
    			SqlSession openSession = build.openSession();
    			PayMapper payMapper = openSession.getMapper(PayMapper.class);
    			PayExample payExample = new PayExample();
    			Criteria createCriteria = payExample.createCriteria();
    			createCriteria.andIdIsNotNull();
    			List<Pay> list = payMapper.selectByExample(payExample);
    			list.forEach((e)->{
    				System.out.println(e.toString());
    			});
    		
    		}
    }
    
    

    你可能感兴趣的:(Mybatis)