Mybatis(十)之逆向工程

逆向工程:使用官方网站的Mapper自动生成工具mybatis-generator-core-1.3.2来生成po类和Mapper映射文件

  1. 导入逆向工程
    在这里插入图片描述
  2. 在generatorConfig.xml中配置以下几点:
  • 数据库连接的信息:驱动类、连接地址、用户名、密码
  • pojo文件所在包路径
  • 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>
		<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"
			password="root">
		</jdbcConnection>
		<!-- <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.lin.nixiang.pojo"
			targetProject=".\src">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
			<!-- 从数据库返回的值被清理前后的空格 -->
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
		<sqlMapGenerator targetPackage="com.lin.nixiang.mapper" 
			targetProject=".\src">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
		</sqlMapGenerator>
		<!-- targetPackage:mapper接口生成的位置 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.lin.nixiang.mapper" 
			targetProject=".\src">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
		</javaClientGenerator>
		<!-- 指定数据库表 -->
		<table schema="" tableName="user"></table>
		<table schema="" tableName="orders"></table>
		
		
		<!-- 有些表的字段需要指定java类型
		 <table schema="" tableName="">
			<columnOverride column="" javaType="" />
		</table> -->
	</context>
</generatorConfiguration>

  1. 执行main方法 得到相应的接口、映射文件和pojo
    在这里插入图片描述

测试逆向工程所得文件

  1. 把生成的文件放入spring-maybatis集成环境
  2. 修改applicationContext.xml
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
   		<!-- basePackage:配置映射包装扫描,多个包时用","或";"分隔 -->
<!-- <property name="basePackage" value="com.lin.mapper" /> -->
<property name="basePackage" value="com.lin.nixiang.mapper"></property>
   </bean>
  1. 测试类
package com.lin.test;

import static org.junit.Assert.fail;

import java.util.Date;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lin.nixiang.mapper.UserMapper;
import com.lin.nixiang.pojo.User;
import com.lin.nixiang.pojo.UserExample;
import com.lin.nixiang.pojo.UserExample.Criteria;


/*
*@author linone
*/
public class UserMappernixiangTest {

private ApplicationContext applicationContext;
	
	@Before
	public void init() {
		applicationContext =  new ClassPathXmlApplicationContext("applicationContext.xml");
	}

	@Test
	public void testInsertSelective() {
		UserMapper userMapper = applicationContext.getBean(UserMapper.class);
		User record =new User();
		record.setUsername("linone");
		record.setBirthday(new Date());
		userMapper.insertSelective(record);
	}

	@Test
	//测试模糊查询
	public void testSelectByExample() {
		UserMapper userMapper = applicationContext.getBean(UserMapper.class);
	    UserExample example = new UserExample();
	    Criteria createCriteria = example.createCriteria();
	    createCriteria.andUsernameLike("%lin%");
	    createCriteria.andSexEqualTo("1");
	    //创建User对象扩展类,再创建Criteria 用来设置用户查询条件
		List<User> list = userMapper.selectByExample(example);
		for (User user : list) {
			System.out.println(user);
		}
	}

	@Test
	//通过主键查询
	public void testSelectByPrimaryKey() {
		UserMapper mapper = applicationContext.getBean(UserMapper.class);
		User user = mapper.selectByPrimaryKey(20);
		System.out.println(user);
		
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值