MyBatis入门(七)---逆向工程
一、逆向工程
1.1、概述
mybatis需要程序号自己编写的SQL。
mybatis官方提供了逆向工程,可以针对单表自动生成mybatis执行所需要的代码
(mapper,java,maper.xml,po...)
一般都是由数据库到java代码, 的生成过程
二、导入jar包
2.1、mybatis-generator

三、配置xml
3.1、generatorConfig.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>
<!--指定数据库jar包 -->
<classPathEntry location="G:/jar/mysql-connector-java-5.1.37-bin.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator> <!--JDBC连接配置,driver,url,user,password -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis?character=utf8"
userId="root" password="root">
</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.mybatis.po"
targetProject=".\src">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.pb.mybatis.mapping"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.pb.mybatis.mapper" targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!--指定要生成的表 -->
<table tableName="author"></table>
<table tableName="blog"></table>
<table tableName="posts"></table> </context> </generatorConfiguration>
四、运行java程序生成
4.1、java程序
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 GeneratorSqlmap { public void generator() 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); }
public static void main(String[] args) throws Exception {
try {
GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
} } }

五、测试
5.1、测试类
package com.pb.ssm.mapper; import static org.junit.Assert.fail; import java.util.Date;
import java.util.List; import javax.crypto.Cipher; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.pb.ssm.po.Author;
import com.pb.ssm.po.AuthorExample;
import com.pb.ssm.po.AuthorExample.Criteria; public class AuthorMapperTest {
private ApplicationContext applicationContext;
private AuthorMapper authorMapper; @Before
public void setUp() throws Exception {
applicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");
authorMapper=(AuthorMapper) applicationContext.getBean("authorMapper");
} //根据条件查询记录数
@Test
public void testCountByExample() {
AuthorExample example=new AuthorExample();
//可以加条件,不加条件默认查询全部
Criteria criteria=example.createCriteria();
//加条件,介绍不是空的
criteria.andAuthorBioIsNotNull();
int num=authorMapper.countByExample(example);
System.out.println("num="+num);
} //根据条件删除
@Test
public void testDeleteByExample() {
AuthorExample example=new AuthorExample();
//可以加条件,不加条件默认查询全部
Criteria criteria=example.createCriteria();
criteria.andAuthorUsernameEqualTo("程序员");
int num=authorMapper.deleteByExample(example);
System.out.println("num="+num); } //根据主键ID删除
@Test
public void testDeleteByPrimaryKey() {
int num=authorMapper.deleteByPrimaryKey(18);
System.out.println("num="+num);
}
//插入
@Test
public void testInsert() { Author author=new Author();
author.setAuthorUsername("再测试一下");
author.setAuthorPassword("admin123");
author.setAuthorEmail("admin1234@qq.com"); int num=authorMapper.insert(author);
System.out.println("num="+num);
//这个方法插入,默认不会把数据库自增加ID返回,如果需要,可以手动增加
System.out.println("插入后的ID"+author.getAuthorId()); }
//插入
@Test
public void testInsertSelective() {
Author author=new Author();
author.setAuthorUsername("再测试一下");
author.setAuthorPassword("admin123");
author.setAuthorEmail("admin1234@qq.com");
author.setRegisterTime(new Date()); int num=authorMapper.insert(author);
System.out.println("num="+num);
//这个方法插入,默认不会把数据库自增加ID返回,如果需要,可以手动增加
System.out.println("插入后的ID"+author.getAuthorId()); }
//自定义 条件查询
@Test
public void testSelectByExample() {
//声明一个对象
AuthorExample authorExample=new AuthorExample();
//创建criteria对象添加条件 and 连接
Criteria criteria=authorExample.createCriteria();
//需要手动加%
criteria.andAuthorUsernameLike("%张三%"); List<Author> list=authorMapper.selectByExample(authorExample);
System.out.println(list.size());
} //根据主键ID查询
@Test
public void testSelectByPrimaryKey() {
Author author= authorMapper.selectByPrimaryKey(6);
System.out.println(author.getAuthorUsername()+"..."+author.getAuthorBio());
} @Test
public void testUpdateByExampleSelective() {
fail("Not yet implemented");
} @Test
public void testUpdateByExample() {
fail("Not yet implemented");
} @Test
public void testUpdateByPrimaryKeySelective() {
fail("Not yet implemented");
} @Test
public void testUpdateByPrimaryKey() {
fail("Not yet implemented");
} }
MyBatis入门(七)---逆向工程的更多相关文章
- <MyBatis>入门七 缓存机制
缓存机制 MyBatis包含强大的查询缓存特性,它可以非常方便的定制和配置.缓存可以极大的提升查询效率. MyBatis默认定义了两级缓存:一级缓存和二级缓存 1.默认情况下,只有一级缓存(sqlSe ...
- mybatis入门基础(九)----逆向工程
一.什么是逆向工程 mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml.po. ...
- mybatis入门(七)
mybatis入门--添加一个用户 //添加用户 @Test public void insertUserTest() throws IOException { // 通过工厂得到SqlSession ...
- MyBatis入门(五)---延时加载、缓存
一.创建数据库 1.1.建立数据库 /* SQLyog Enterprise v12.09 (64 bit) MySQL - 5.7.9-log : Database - mybatis ****** ...
- MyBatis入门(二)---一对一,一对多
一.创建数据库表 1.1.创建数据表同时插入数据 /* SQLyog Enterprise v12.09 (64 bit) MySQL - 5.6.27-log : Database - mybati ...
- Mybatis(一) mybatis入门
学习了hibernate这个持久层框架之后,在来学习Mybatis简直是无压力,因为Mybatis入门门栏很低,如果学习过了hibernate的话,对于Mybatis的学习很简单了,如果没学习过hib ...
- Mybatis学习(一)—————mybatis入门
学习了hibernate这个持久层框架之后,在来学习Mybatis简直是无压力,因为Mybatis入门门栏很低,如果学习过了hibernate的话,对于Mybatis的学习很简单了,如果没学习过hib ...
- MyBatis学习七:spring和MyBatis整合
<\mybatis\day02\16mybatis和spring整合-sqlSessionFactory配置.avi;> MyBatis学习七:spring和MyBatis整合.逆向工程 ...
- mybatis入门--初识mybatis
初识mybatis 今天,一起来说说mybits这个框架吧.这是一个持久层的框架.之前叫做ibatis.所以,在它的代码中出现ibatis这个词的时候,不要感到惊讶.不是写错了,它确实就是这个样子的. ...
- Mybatis系列(一):Mybatis入门
一.Mybatis是什么 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改 ...
随机推荐
- CSS3伪类和伪元素的特性和区别
前端er们大都或多或少地接触过CSS伪类和伪元素,比如最常见的:focus,:hover以及<a>标签的:link.visited等,伪元素较常见的比如:before.:after等. 其 ...
- 免费下载:用于原型设计的 iOS 7 线框图
André Revin 使用 Illustrator 创建 iOS7 iPhone 5 的样机原型.这是一个像素完美的线框样机,可以帮助超级轻松的打造你的原型.你可以免费下载源文件,并在工作中使用. ...
- LeetCode-334. Increasing Triplet Subsequence
Description: Given an unsorted array return whether an increasing subsequence of length 3 exists or ...
- sed用例
文件空行处理 1. 在文件中的每一行后面添加一个空行. sed 'G' test.txt 解释: Get命令是将保留空间的内容取出,并添加到当前模式空间的内容之后(添加一行).当保留空间为空时,效果为 ...
- CentOS6.5菜鸟之旅:安装输入法(小呀小企鹅)
一.前言 假如在登录系统的时候语言选择了中文,那么是系统会自带ibus的中文输入法.但由于我打算用英文版,于是就被小企鹅输入法(FCITX)折腾了两个晚上. 二.检查系统编码 在bash中输入 loc ...
- IOS开发UI基础UIImagePickerController的属性
UIImagePickerController 1.+(BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType ...
- Tesseract-OCR引擎 入门
OCR(Optical Character Recognition):光学字符识别,是指对图片文件中的文字进行分析识别,获取的过程. Tesseract:开源的OCR识别引擎,初期Tesseract引 ...
- windows的host文件的位置和作用
在Window系统中有个Hosts文件(没有后缀名),在Windows98系统下该文件在Windows目录,在Windows2000/XP系统中位于C:\Winnt\System32\Drivers\ ...
- 根据IP地址获取地址所在城市帮助类(IPHelper)
很多类库都是需要在长时间的编写过程中进行积累的,进入软件编程行业已经是第五个年头了,从2011年写下第一行代码到现在不知道已经写了多少行代码了,时间也过得挺快的.最近事情比较多,也很少写博客了,最近项 ...
- csharp:Compare two DataTables to rows in one but not the other
/// <summary> /// 账面数据 Accounting /// </summary> /// <returns></returns> Dat ...