29.MyBatis_逆向工程

MyBatisGenerator(MBG)是为MyBatis框架设计的代码生成工具,能自动生成JavaBean、Mapper接口及XML映射文件。配置文件中包括数据库连接、Bean生成策略等设置。通过配置,MBG能够根据数据库表生成对应的代码,支持基本的CRUD操作和QBC查询。在运行生成器后,可以进行条件查询等操作。
摘要由CSDN通过智能技术生成
MyBatis Generator简介

MyBatis Generator简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类。支持基本的增删改查,以及QBC风格的条件查询。但是表连接、存储过程等这些复杂sql的定义需要我们手工编写。

官方文档地址

http://www.mybatis.org/generator/

MyBatis Generator使用步骤
  1. 编写MyBatis Generator的配置文件(重要几处配置)
    1)jdbcConnection配置数据库连接信息
    2)javaModelGenerator配置javaBean的生成策略
    3)sqlMapGenerator 配置sql映射文件生成策略
    4)javaClientGenerator配置Mapper接口的生成策略
    5)table 配置要逆向解析的数据表
    tableName:表名
    domainObjectName:对应的javaBean名

MBG配置文件:

<?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:指定如何连接到目标数据库 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true"
        userId="root"
        password="root">
    </jdbcConnection>

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

	<!-- 
		javaModelGenerator:指定javaBean生成策略
		targetPackage:指定目标包名;
		targetProject:指定目标工程	
	 -->
    <javaModelGenerator targetPackage="com.fzl.mybatis.bean" targetProject=".\src">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>

	<!-- 
		sqlMapGenerator:sql映射生成策略
	 -->
    <sqlMapGenerator targetPackage="com.fzl.mybatis.dao"  targetProject=".\conf">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>

	<!-- 
		javaClientGenerator:指定mapper接口的位置
	 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.fzl.mybatis.dao"  targetProject=".\src">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>

	<!-- table:要逆向生成哪些表 -->
    <table tableName="tbl_dept" domainObjectName="Department"></table>
    <table tableName="tbl_employee" domainObjectName="Employee"></table>

  </context>
</generatorConfiguration>
  1. 运行代码生成器生成代码

生成器代码:

@Test
	public void testMbg() throws Exception {
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		File configFile = new File("mbg.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);
	}
    

从官方文档给出的示例中,只需要将File configFile = new File(“mbg.xml”);你面的xml文件改成你项目下的MyBatis Generator的配置文件即可。

注意:
Context标签
targetRuntime=“MyBatis3“可以生成带条件的增删改查。
targetRuntime=“MyBatis3Simple“可以生成基本的增删改查。
如果再次生成,建议将之前生成的数据删除,避免xml向后追加内容出现的问题。

  1. 测试查询
 @Test
    public void testMbg3() throws IOException {
    	SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
    	SqlSession openSession = sqlSessionFactory.openSession();
    	try {
			EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
			//xxxExample就是封装查询条件的
			//1.查询所有员工信息
			List<Employee> emps = mapper.selectByExample(null);
			//2.查询姓名中有字母a的,gender为0的员工信息
			EmployeeExample example = new EmployeeExample();
			//创建一个Criteria,这个Criteria就是拼装查询条件
			com.fzl.mybatis.bean.EmployeeExample.Criteria criteria = example.createCriteria();
			criteria.andLastNameLike("%a%");
			criteria.andGenderEqualTo("2");
			com.fzl.mybatis.bean.EmployeeExample.Criteria criteria2 = example.createCriteria();
			criteria2.andEmailLike("%2%");
			example.or(criteria2);
			List<Employee> employees = mapper.selectByExample(example);
			for(Employee e:employees) {
				System.out.println(e.getLastName());
			}
			openSession.commit();
		} finally {
			openSession.close();
		}
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值