Day41——MyBatis逆向工程

一. 知识储备

在这里插入图片描述

二. 例子

导入jar 包:mybatis-generator-core-1.3.2.jar

在工程项目的目录下创建mbg.xml文件

<?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:
  	 	targetRuntime: 
  				MyBatis3:生成带QBC风格的CRUD
  				MyBatis3Simple: 生成基本的CRUD
   -->
  <context id="DB2Tables" targetRuntime="MyBatis3">
    <!-- 数据库的连接 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/bookstore_0416"
        userId="root"
        password="123456">
    </jdbcConnection>

	<!-- javaBean的生成策略 -->
    <javaModelGenerator targetPackage="com.atguigu.mybatis.beans" targetProject=".\src">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>
	
	<!-- SQL映射文件的生成策略 -->
    <sqlMapGenerator targetPackage="com.atguigu.mybatis.dao"  targetProject=".\conf">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
	
	<!-- Mapper接口的生成策略  -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.mybatis.dao"  targetProject=".\src">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
	
	<!-- 逆向分析的表 -->
    <table tableName="tbl_dept"  domainObjectName="Department"></table>
    <table tableName="tbl_employee" domainObjectName="Employee"></table>

  </context>
</generatorConfiguration>

TestMyBatis.java

//测试
@Test
	public void testMyBatis3() throws Exception{
		SqlSessionFactory ssf = getSqlSessionFactory();
		SqlSession session = ssf.openSession();
		
		try {
				EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
				
				//QBC: Query By Criteria
				
				//查询:查询名字中带有“老师”并且  性别是男的 或者 邮箱中带有'b'
				EmployeeExample example = new EmployeeExample();
				//封装条件 Criteria
				Criteria c1 = example.createCriteria();
				c1.andLastNameLike("%老师%");
				c1.andGenderEqualTo("1");
				
				//对于or的条件需要重新封装到Criteria中
				Criteria c2 = example.createCriteria();
				c2.andEmailLike("%b%");
				
				//封装or条件的Criteria需要or到example中
				example.or(c2);
				
				
				List<Employee> emps = mapper.selectByExample(example);
				System.out.println(emps);
			
			
		} finally {
			// TODO: handle finally clause
			session.close();
		}
		
	}


//运行即可创建逆向工程
@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);
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值