一、逆向工程

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;@Beforepublic void setUp() throws Exception {applicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");authorMapper=(AuthorMapper) applicationContext.getBean("authorMapper");}//根据条件查询记录数
    @Testpublic void testCountByExample() {AuthorExample example=new AuthorExample();//可以加条件,不加条件默认查询全部Criteria criteria=example.createCriteria();//加条件,介绍不是空的
        criteria.andAuthorBioIsNotNull();int num=authorMapper.countByExample(example);System.out.println("num="+num);}//根据条件删除
    @Testpublic void testDeleteByExample() {AuthorExample example=new AuthorExample();//可以加条件,不加条件默认查询全部Criteria criteria=example.createCriteria();criteria.andAuthorUsernameEqualTo("程序员");int num=authorMapper.deleteByExample(example);System.out.println("num="+num);}//根据主键ID删除
    @Testpublic void testDeleteByPrimaryKey() {int num=authorMapper.deleteByPrimaryKey(18);System.out.println("num="+num);}//插入
    @Testpublic 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());}//插入
    @Testpublic 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());}//自定义 条件查询
    @Testpublic 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查询
    @Testpublic void testSelectByPrimaryKey() {Author author= authorMapper.selectByPrimaryKey(6);System.out.println(author.getAuthorUsername()+"..."+author.getAuthorBio());}@Testpublic void testUpdateByExampleSelective() {fail("Not yet implemented");}@Testpublic void testUpdateByExample() {fail("Not yet implemented");}@Testpublic void testUpdateByPrimaryKeySelective() {fail("Not yet implemented");}@Testpublic void testUpdateByPrimaryKey() {fail("Not yet implemented");}}

MyBatis入门(七)---逆向工程相关推荐

  1. mybatis入门(七)之日志

    转载自    mybatis入门(七)之日志 Mybatis 的内置日志工厂提供日志功能,内置日志工厂将日志交给以下其中一种工具作代理: SLF4J Apache Commons Logging Lo ...

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

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

  3. 【Mybatis入门20221024】

    Mybatis入门20221024 一. 搭建环境 1.1 配置Mysql环境 1.2 配置Maven环境 二. 创建包`utils`的工具类`MybatisUtils` 三. 创建文件`resour ...

  4. Mybatis入门之动态sql

    Mybatis入门之动态sql 通过mybatis提供的各种标签方法实现动态拼接sql. 1.if.where.sql.include标签(条件.sql片段) <sql id="sel ...

  5. MyBatis1:MyBatis入门

    MyBatis是什么 MyBatis是什么,MyBatis的jar包中有它的官方文档,文档是这么描述MyBatis的: MyBatis is a first class persistence fra ...

  6. MyBatis(1):MyBatis入门

    MyBatis是什么 MyBatis是什么,MyBatis的jar包中有它的官方文档,文档是这么描述MyBatis的: MyBatis is a first class persistence fra ...

  7. MyBatis-学习笔记02【02.Mybatis入门案例】

    Java后端 学习路线 笔记汇总表[黑马程序员] MyBatis-学习笔记01[01.Mybatis课程介绍及环境搭建][day01] MyBatis-学习笔记02[02.Mybatis入门案例] M ...

  8. mybatis入门(一)之基础安装

    转载自  mybatis入门 安装 要使用 MyBatis, 只需将 mybatis-x.x.x.jar 文件置于 classpath 中即可. 如果使用 Maven 来构建项目,则需将下面的 dep ...

  9. Mybatis入门程序增删改查操作

    学习目标 了解Mybatis的基本知识 熟悉Mybatis的工作原理 掌握Mybatis入门程序的编写 文章目录 1.初始Mybatis 2.Mybatis入门程序 3.Mybatis操作总结 1.初 ...

  10. Mybatis入门 使用注解

    使用XML方式地址为Mybatis入门 使用XML 1.目录结构 2.需要修改的地方 1.mybatis的配置文件 <?xml version="1.0" encoding= ...

最新文章

  1. java session验证码_利用session实现一次性验证码
  2. scala 操作hdfs
  3. mysql所有知识点总结_MySQL知识点总结
  4. springboot学习笔记(六)
  5. 官宣!张小龙史上最长演讲 4小时3万字完整版回应微信的一切
  6. Linux三剑客grep、sed、awk
  7. 手动配置 iis php环境,iis上手动配置php
  8. 【JS第1期】深拷贝实现原理
  9. Linux命令解释之sed
  10. Java编程必备软件
  11. rtl驱动 ubuntu 禁用_Ubuntu如何安装rtl8822be驱动
  12. 现代信息检索——布尔检索
  13. 用Python的turtle画一个正方形圆形五角星
  14. 小黑fastNLP实战:实体识别1
  15. 从技术角度告诉你,区块链到底有哪些特点和运作机制
  16. dcmtk读取dcm文件中Tag值
  17. 数据集下载(OTB2015、VOT2018、UAV123、DET、VID、COCO、Youtube-BB、LaSOT、GOT-10k)
  18. 免费发外链论坛有哪些?
  19. RStudio入门教程(二)RStudio数据处理
  20. mac os ERROR! The server quit without updating PID file

热门文章

  1. CocosCreator上的游戏(调试)发布到微信小程序
  2. 用Git向gitHub上传项目
  3. C#量转换为汉字表达
  4. WebView加载HTML时禁止超链接跳转
  5. MB51升级后输入框的名字显示数据字段名
  6. 精致3D图片切换效果,最适合企业产品展示
  7. 【玩转Ubuntu】01. Ubuntu上配置JDK
  8. 最大连续子序列乘积(DP)
  9. js null ,null没有typeof返回值为undefine 即 null没有返回类型的
  10. Solaris 操作