一、逆向工程

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入门(七)---逆向工程的更多相关文章

  1. <MyBatis>入门七 缓存机制

    缓存机制 MyBatis包含强大的查询缓存特性,它可以非常方便的定制和配置.缓存可以极大的提升查询效率. MyBatis默认定义了两级缓存:一级缓存和二级缓存 1.默认情况下,只有一级缓存(sqlSe ...

  2. mybatis入门基础(九)----逆向工程

    一.什么是逆向工程 mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml.po. ...

  3. mybatis入门(七)

    mybatis入门--添加一个用户 //添加用户 @Test public void insertUserTest() throws IOException { // 通过工厂得到SqlSession ...

  4. MyBatis入门(五)---延时加载、缓存

    一.创建数据库 1.1.建立数据库 /* SQLyog Enterprise v12.09 (64 bit) MySQL - 5.7.9-log : Database - mybatis ****** ...

  5. MyBatis入门(二)---一对一,一对多

    一.创建数据库表 1.1.创建数据表同时插入数据 /* SQLyog Enterprise v12.09 (64 bit) MySQL - 5.6.27-log : Database - mybati ...

  6. Mybatis(一) mybatis入门

    学习了hibernate这个持久层框架之后,在来学习Mybatis简直是无压力,因为Mybatis入门门栏很低,如果学习过了hibernate的话,对于Mybatis的学习很简单了,如果没学习过hib ...

  7. Mybatis学习(一)—————mybatis入门

    学习了hibernate这个持久层框架之后,在来学习Mybatis简直是无压力,因为Mybatis入门门栏很低,如果学习过了hibernate的话,对于Mybatis的学习很简单了,如果没学习过hib ...

  8. MyBatis学习七:spring和MyBatis整合

    <\mybatis\day02\16mybatis和spring整合-sqlSessionFactory配置.avi;> MyBatis学习七:spring和MyBatis整合.逆向工程 ...

  9. mybatis入门--初识mybatis

    初识mybatis 今天,一起来说说mybits这个框架吧.这是一个持久层的框架.之前叫做ibatis.所以,在它的代码中出现ibatis这个词的时候,不要感到惊讶.不是写错了,它确实就是这个样子的. ...

  10. Mybatis系列(一):Mybatis入门

    一.Mybatis是什么 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改 ...

随机推荐

  1. 使用ECMAscript5中的forEach函数遍历数组

    1 var a = [1,2,3]; 2 a.forEach(function(value,index,arr){ 3 arr[index] = value + index; 4 }) 5 conso ...

  2. C#生成不重复的随机数(转)

    我们在做能自动生成试卷的考试系统时,常常需要随机生成一组不重复随机数的题目,在.net Framework中提供了一个专门用来产生随机数的类System.Random. 对于随机数,大家都知道,计算机 ...

  3. chrome 插件 vimium 像操作vim一样的操作浏览器

    感谢潘德龙同学推荐的插件非常好用整理出来一些常用快捷键记下,顺便分享! x  关闭当前页 GW 跳出浏览器 J 展示左边页签 K展示右边页签 j向下滚动 k向上滚动 /搜索 ?打开帮助 r 刷新当前页 ...

  4. Android 定时器

    Andorid定时器封装类 public class TimerUtil { private static final String TAG = "TimerUtil"; priv ...

  5. Http客户端识别的方法

      Http用户识别的机制方法分为如下几种: 1).承载用户身份的HTTP首部 2).客户端IP地址跟踪,根据客户端IP地址进行识别 3).用户登录,用认证方式设别用户 4).使用胖URL,一种在UR ...

  6. .Net反射机制分析和使用

    1..NET反射的概述 .NET反射是审查元数据并动态收集关于它的类型信息的能力. 应用程序结构分为应用程序域—程序集—模块—类型—成员几个层次,公共语言运行库加载器管理应用程序域.这些域在拥有相同应 ...

  7. Winform开发框架的业务对象统一调用方式

    在这个纷繁的社会里面,统一性的特点能够带来很多高效的产出.牢固的记忆,这种特征无论对于企业.个人的开发工作,知识的传承都有着非常重要的作用,Winfrom框架本身就是基于这个理念而生,从统一的数据库设 ...

  8. Gridview的RowDataBound事件(添加删除提示,改变背景颜色)

    protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e) { //如果是绑定数据行 if (e.Row.Row ...

  9. Redis 3.2.100 Windows 32位下载

    因为公司的老服务器用的是Windows 2008 32位,不得不安装Redis32位.可在微软的Github上有64位的MSI安装包,前天开始在不同的群里寻找32位的安装包,一直没找到,索性自己下载源 ...

  10. WPF_View中控件使用单例ViewModel

    一个View里面单独的一个控件需要一个ViewModel   这个ViewModel类 可以做成单例 public class VMTest:Ad.Core.ViewModel.ViewModel { ...