在Java中,优秀的框架一般都离不开反射的使用,有时候自己在为了实现或者简化某些功能或者操作时,自己也可以开发一些小型框架。在学习Mybatis框架时,会接触到很多关于反射的东西,当然如果你有兴趣打开源码浏览的话。今天就是想无聊,谈谈Mybatis的逆向工程。
说起Mybatis这个框架,我们基本会用到的几个东西,用一张草图来稍微描述一下:
对于这张图大家一定都不陌生,而且我们知道,这四个东西联系都非常紧密。比如通过数据库表我们能知道pojo类相应的属性,通过mapper接口,我们能知道mapper配置文件中的SQL语句以及其输入输出参数的定义等等。 而对于mapper配置文件,则可以看做是其他三者的桥梁。所以Mybatis设计了逆向工程,利用逆向工程,我们可以利用其中的某一个来生成我们所需要的其他全部文件或者配置。
接下来,实现一个简单的逆向工程,利用一个数据库表,生成所需的(配置)文件。
关于数据库和数据库表,我本机为了测试已经建立好了,如下图:
接下来就是开始逆向工程,需要准备的jar包除了我们日常使用mybatis所用到的之外,还需要一个mybatis-generator-core-1.3.5.jar,可以到maven库里面去下载。配置好jar包后,我们需要开始配置逆向工程,首先建立逆向工程配置文件:generator.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 id="charlesTests" targetRuntime="MyBatis3">
<commentGenerator>
<!--
suppressAllComments属性值:
true:自动生成实体类、SQL映射文件时没有注释
true:自动生成实体类、SQL映射文件,并附有注释
-->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/student"
userId="root" password="charles">
</jdbcConnection>
<!--
forceBigDecimals属性值:
true:把数据表中的DECIMAL和NUMERIC类型,
解析为JAVA代码中的java.math.BigDecimal类型
false(默认):把数据表中的DECIMAL和NUMERIC类型,
解析为解析为JAVA代码中的Integer类型
-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--
targetProject属性值:实体类的生成位置
targetPackage属性值:实体类所在包的路径
-->
<javaModelGenerator targetPackage="com.charles.entity"
targetProject=".\src">
<!-- trimStrings属性值:
true:对数据库的查询结果进行trim操作
false(默认):不进行trim操作
-->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--
targetProject属性值:SQL映射文件的生成位置
targetPackage属性值:SQL映射文件所在包的路径
-->
<sqlMapGenerator targetPackage="com.charles.mapper"
targetProject=".\src">
</sqlMapGenerator>
<!-- 生成动态代理的接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.charles.mapper" targetProject=".\src">
</javaClientGenerator>
<!-- 指定数据库表 -->
<table tableName="info"> </table>
</context>
</generatorConfiguration>
关于配置文件中的配置属性,这里就不再细说,代码里已有注释。
接下来,我们就可以建立测试类来完成逆向工程的开发,新建Java源文件Test.java,代码如下:
package com.charles.tests;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
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.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
public class Test {
public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException
{
// 本四行代码为填参数而生
List<String> warnings = new ArrayList<>();
ConfigurationParser cp=new ConfigurationParser(warnings);
Configuration config=cp.parseConfiguration(new File("src/generator.xml"));
DefaultShellCallback callBack = new DefaultShellCallback(true);
// 逆向工程的核心类
MyBatisGenerator generator=new MyBatisGenerator(config, callBack , warnings);
generator.generate(null);
}
}
测试类建立完毕,这里稍稍提一下,其实关键代码知识最后两句,前面四行代码只是为了给倒数第二行代码提供参数,对于第一行,就是把相关的警告信息放在该集合中;第二行是为了第三行config对象的产生而新建的对象。后面两者就不说了,按照相应的提示就能完成。至此,我们所需的准备工作已经做完,此时的项目结构如下:
我们运行该程序,然后刷新项目目录,发现:
新增了两个包,以及包里面有相关的类,点击查看:info.java
package com.charles.entity;
public class Info {
private String name;
private Integer id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
典型的pojo,与我们数据库的字段一一对应。再观察infoMapper.java
package com.charles.mapper;
import com.charles.entity.Info;
import com.charles.entity.InfoExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface InfoMapper {
long countByExample(InfoExample example);
int deleteByExample(InfoExample example);
int insert(Info record);
int insertSelective(Info record);
List<Info> selectByExample(InfoExample example);
int updateByExampleSelective(@Param("record") Info record, @Param("example") InfoExample example);
int updateByExample(@Param("record") Info record, @Param("example") InfoExample example);
}
与之对应的infoMapper.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.charles.mapper.InfoMapper">
<resultMap id="BaseResultMap" type="com.charles.entity.Info">
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="id" jdbcType="INTEGER" property="id" />
</resultMap>
<sql id="Example_Where_Clause">
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause">
<where>
<foreach collection="example.oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List">
name, id
</sql>
<select id="selectByExample" parameterType="com.charles.entity.InfoExample" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from info
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<delete id="deleteByExample" parameterType="com.charles.entity.InfoExample">
delete from info
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="com.charles.entity.Info">
insert into info (name, id)
values (#{name,jdbcType=VARCHAR}, #{id,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.charles.entity.Info">
insert into info
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="name != null">
name,
</if>
<if test="id != null">
id,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="com.charles.entity.InfoExample" resultType="java.lang.Long">
select count(*) from info
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map">
update info
<set>
<if test="record.name != null">
name = #{record.name,jdbcType=VARCHAR},
</if>
<if test="record.id != null">
id = #{record.id,jdbcType=INTEGER},
</if>
</set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map">
update info
set name = #{record.name,jdbcType=VARCHAR},
id = #{record.id,jdbcType=INTEGER}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<resultMap id="BaseResultMap" type="com.charles.entity.Info">
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="id" jdbcType="INTEGER" property="id" />
</resultMap>
<sql id="Example_Where_Clause">
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause">
<where>
<foreach collection="example.oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List">
name, id
</sql>
<select id="selectByExample" parameterType="com.charles.entity.InfoExample" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from info
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<delete id="deleteByExample" parameterType="com.charles.entity.InfoExample">
delete from info
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="com.charles.entity.Info">
insert into info (name, id)
values (#{name,jdbcType=VARCHAR}, #{id,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.charles.entity.Info">
insert into info
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="name != null">
name,
</if>
<if test="id != null">
id,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="com.charles.entity.InfoExample" resultType="java.lang.Long">
select count(*) from info
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map">
update info
<set>
<if test="record.name != null">
name = #{record.name,jdbcType=VARCHAR},
</if>
<if test="record.id != null">
id = #{record.id,jdbcType=INTEGER},
</if>
</set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map">
update info
set name = #{record.name,jdbcType=VARCHAR},
id = #{record.id,jdbcType=INTEGER}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
</mapper>
一套完整而又官方增删改查的mapper配置文件来了;
至此,已经基本完成了一个逆向工程的例子,但是在实际的开发中,这种关于逆向开发的应用是不常见的,其原因之一就是生成的文件很规整而又官方,对于不同风格的程序员来说,可能会有困扰;当然,这个项目中还有两个以example结束的文件,这个是关于一些使用的说明,用不上,可以删除不用。
文章浏览阅读1k次。通过使用ajax方法跨域请求是浏览器所不允许的,浏览器出于安全考虑是禁止的。警告信息如下:不过jQuery对跨域问题也有解决方案,使用jsonp的方式解决,方法如下:$.ajax({ async:false, url: 'http://www.mysite.com/demo.do', // 跨域URL ty..._nginx不停的xhr
文章浏览阅读2k次。关于在 Oracle 中配置 extproc 以访问 ST_Geometry,也就是我们所说的 使用空间SQL 的方法,官方文档链接如下。http://desktop.arcgis.com/zh-cn/arcmap/latest/manage-data/gdbs-in-oracle/configure-oracle-extproc.htm其实简单总结一下,主要就分为以下几个步骤。..._extproc
文章浏览阅读1.5w次。linux下没有上面的两个函数,需要使用函数 mbstowcs和wcstombsmbstowcs将多字节编码转换为宽字节编码wcstombs将宽字节编码转换为多字节编码这两个函数,转换过程中受到系统编码类型的影响,需要通过设置来设定转换前和转换后的编码类型。通过函数setlocale进行系统编码的设置。linux下输入命名locale -a查看系统支持的编码_linux c++ gbk->utf8
文章浏览阅读750次。今天准备从生产库向测试库进行数据导入,结果在imp导入的时候遇到“ IMP-00009:导出文件异常结束” 错误,google一下,发现可能有如下原因导致imp的数据太大,没有写buffer和commit两个数据库字符集不同从低版本exp的dmp文件,向高版本imp导出的dmp文件出错传输dmp文件时,文件损坏解决办法:imp时指定..._imp-00009导出文件异常结束
文章浏览阅读143次。当下是一个大数据的时代,各个行业都离不开数据的支持。因此,网络爬虫就应运而生。网络爬虫当下最为火热的是Python,Python开发爬虫相对简单,而且功能库相当完善,力压众多开发语言。本次教程我们爬取前程无忧的招聘信息来分析Python程序员需要掌握那些编程技术。首先在谷歌浏览器打开前程无忧的首页,按F12打开浏览器的开发者工具。浏览器开发者工具是用于捕捉网站的请求信息,通过分析请求信息可以了解请..._初级python程序员能力要求
文章浏览阅读7.6k次,点赞2次,收藏6次。@Service标注的bean,类名:ABDemoService查看源码后发现,原来是经过一个特殊处理:当类的名字是以两个或以上的大写字母开头的话,bean的名字会与类名保持一致public class AnnotationBeanNameGenerator implements BeanNameGenerator { private static final String C..._@service beanname
文章浏览阅读6.9w次,点赞73次,收藏463次。1.前序创建#include<stdio.h>#include<string.h>#include<stdlib.h>#include<malloc.h>#include<iostream>#include<stack>#include<queue>using namespace std;typed_二叉树的建立
文章浏览阅读7.1k次。在Asp.net上使用Excel导出功能,如果文件名出现中文,便会以乱码视之。 解决方法: fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);_asp.net utf8 导出中文字符乱码
文章浏览阅读2.1k次,点赞4次,收藏23次。第一次实验 词法分析实验报告设计思想词法分析的主要任务是根据文法的词汇表以及对应约定的编码进行一定的识别,找出文件中所有的合法的单词,并给出一定的信息作为最后的结果,用于后续语法分析程序的使用;本实验针对 PL/0 语言 的文法、词汇表编写一个词法分析程序,对于每个单词根据词汇表输出: (单词种类, 单词的值) 二元对。词汇表:种别编码单词符号助记符0beginb..._对pl/0作以下修改扩充。增加单词
文章浏览阅读773次。我在使用adb.exe时遇到了麻烦.我想使用与bash相同的adb.exe shell提示符,所以我决定更改默认的bash二进制文件(当然二进制文件是交叉编译的,一切都很完美)更改bash二进制文件遵循以下顺序> adb remount> adb push bash / system / bin /> adb shell> cd / system / bin> chm..._adb shell mv 权限
文章浏览阅读6.8k次,点赞12次,收藏125次。1. 单目相机标定引言相机标定已经研究多年,标定的算法可以分为基于摄影测量的标定和自标定。其中,应用最为广泛的还是张正友标定法。这是一种简单灵活、高鲁棒性、低成本的相机标定算法。仅需要一台相机和一块平面标定板构建相机标定系统,在标定过程中,相机拍摄多个角度下(至少两个角度,推荐10~20个角度)的标定板图像(相机和标定板都可以移动),即可对相机的内外参数进行标定。下面介绍张氏标定法(以下也这么称呼)的原理。原理相机模型和单应矩阵相机标定,就是对相机的内外参数进行计算的过程,从而得到物体到图像的投影_相机-投影仪标定
文章浏览阅读2.2k次。文章目录Wayland 架构Wayland 渲染Wayland的 硬件支持简 述: 翻译一篇关于和 wayland 有关的技术文章, 其英文标题为Wayland Architecture .Wayland 架构若是想要更好的理解 Wayland 架构及其与 X (X11 or X Window System) 结构;一种很好的方法是将事件从输入设备就开始跟踪, 查看期间所有的屏幕上出现的变化。这就是我们现在对 X 的理解。 内核是从一个输入设备中获取一个事件,并通过 evdev 输入_wayland