当前位置: 首页 > 编程日记 > 正文

Spring+SpringMVC+MyBatis深入学习及搭建(十)——MyBatis逆向工程

转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6973266.html

前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(九)——MyBatis和Spring整合

使用官方网站的mapper自动生成工具mybatis-generator-core-1.3.2来生成po类和mapper映射文件。

1.什么是逆向工程

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

企业实际开发中,常用的逆向工程方式:

由数据库的表生成java代码。

2.下载逆向工程

3.使用方法(要求会用)

为了防止后期数据库表修改、扩展,需求修改等原因,更新自动生成的po,mapper覆盖有误。我们新建专门逆向生成的项目generatorSqlmapCustom,再按需求将自动生成的po,mapper等拷贝到项目中去。

3.1运行逆向工程

建议使用java程序方式,不依赖开发工具。

3.2mapper生成配置文件

在gengeratorConfig.xml中配置mapper生成的详细信息,注意改下几点:

(1)添加要生成的数据库表;

(2)po文件所在包路径;

(3)mapper文件所在包路径。

配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfigurationPUBLIC "-//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/mybatisdemo" userId="root"password=""></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="joanna.yan.po"targetProject=".\src"><!-- enableSubPackages:是否让schema作为包的后缀 --><property name="enableSubPackages" value="false" /><!-- 从数据库返回的值被清理前后的空格 --><property name="trimStrings" value="true" /></javaModelGenerator><!-- targetProject:mapper映射文件生成的位置 --><sqlMapGenerator targetPackage="joanna.yan.mapper" targetProject=".\src"><!-- enableSubPackages:是否让schema作为包的后缀 --><property name="enableSubPackages" value="false" /></sqlMapGenerator><!-- targetPackage:mapper接口生成的位置 --><javaClientGenerator type="XMLMAPPER"targetPackage="joanna.yan.mapper" targetProject=".\src"><!-- enableSubPackages:是否让schema作为包的后缀 --><p    roperty name="enableSubPackages" value="false" /></javaClientGenerator><!-- 指定数据库表 --><table tableName="items"></table><table tableName="orders"></table><table tableName="orderdetail"></table><table tableName="user"></table><!-- <table schema="" tableName="sys_user"></table><table schema="" tableName="sys_role"></table><table schema="" tableName="sys_permission"></table><table schema="" tableName="sys_user_role"></table><table schema="" tableName="sys_role_permission"></table> --><!-- 有些表的字段需要指定java类型<table schema="" tableName=""><columnOverride column="" javaType="" /></table> --></context>
</generatorConfiguration>

3.3使用Java类生成mapper文件

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();}}}

生成后的代码:

3.4拷贝生成的mapper文件到工程中指定的目录中

3.4.1mapper.xml

Mapper.xml文件拷贝至mapper目录内

3.4.2mapper.java

Mapper.xml文件拷贝至mapper目录内

注意:mapper.xml文件和mapper.java文件在一个目录内且文件名相同。

3.4.3mapper接口测试

public class ItemsMapperTest {private ApplicationContext applicationContext;private ItemsMapper itemsMapper;@Beforepublic void setUp(){applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");itemsMapper=(ItemsMapper) applicationContext.getBean("itemsMapper");}@Testpublic void testInsert() {Items item=new Items();item.setName("手机");item.setPrice(999f);
     item.setCreatetime(new Date());itemsMapper.insert(item);}
//自定义条件查询 @Testpublic void testSelectByExample() {ItemsExample itemsExample=new ItemsExample();//通过criteria构造查询条件ItemsExample.Criteria criteria=itemsExample.createCriteria();criteria.andNameEqualTo("笔记本3");//可能返回多条记录List<Items> list=itemsMapper.selectByExample(itemsExample);System.out.println(list);}//根据主键查询 @Testpublic void testSelectByPrimaryKey() {Items items=itemsMapper.selectByPrimaryKey(1);System.out.println(items);}@Testpublic void testUpdateByPrimaryKey() {//对所有字段进行更新,需要先查询出来再更新Items items=itemsMapper.selectByPrimaryKey(1);items.setName("水杯");itemsMapper.updateByPrimaryKey(items);//如果传入字段不为空才更新,在批量更新中使用此方法,不需要先查询再更新 itemsMapper.updateByPrimaryKeySelective(items);}}

4.逆向工程注意事项

4.1Mapper文件内容不覆盖而是追加

XXXMapper.xml文件已经存在时,如果进行重新生成则mapper.xml文件内容不被覆盖而是进行内容追加,结果导致mybatis解析失败。

解决方法:删除原来已经生成的mapper.xml文件再进行生成。

MyBatis自动生成的po和mapper.java文件不是内容追加而是直接覆盖没有此问题。

4.2 Table schema问题

下边是针对Oracle数据库表生成代码的schema问题:

schema即数据库模式,oracle中一个用户对应一个schema,可以理解为用户就是schema。当Oracle数据库存在多个Schema可以访问相同的表名时,使用mybatis生成该表的mapper.xml将会出现mapper内容重复的问题,结果导致mybatis解析错误。

解决方法:在table中填写schema,如下:

<table schema="XXXX" tableName=" " >

XXXX即为一个schema的名称,生成后将mapper.xml的schema前缀批量去掉,如果不去掉,当Oracle用户变更了sql语句将查询失败。

快捷操作方式:mapper.xml文件中批量替换:“from XXXX.”为空。

Oracle查询对象的schema可从dba_objects中查询,如下:

select * from dba_objects

如果此文对您有帮助,微信打赏我一下吧~

转载于:https://www.cnblogs.com/Joanna-Yan/p/6973266.html

相关文章:

(一)七种AOP实现方法

在这里列表了我想到的在你的应用程序中加入AOP支持的所有方法。这里最主要的焦点是拦截&#xff0c;因为一旦有了拦截其它的事情都是细节。 Approach 方法 Advantages 优点 Disadvantages 缺点 Remoting Proxies 远程代理 Easy to implement, because of the .Net framewor…

[导入]Java线程的深入探讨

文章来源:http://blog.csdn.net/jeffreyren/archive/2001/03/29/6401.aspx 转载于:https://www.cnblogs.com/zhaoxiaoyang2/archive/2001/03/30/816654.html

CSS之布局(盒子的水平布局)

盒子的水平布局&#xff1a; <!DOCTYPE html> <html><head><meta charset"UTF-8"><title>盒子的水平布局</title><style>.outer{width: 800px;height: 200px;border: 10px red solid;}.inner{width: 200px;height: 200px…

IssueVission的命令处理

模仿了IssueVission的命令处理&#xff0c;感觉真的很好。只是在用的过程中遇到这样的一个问题&#xff1a;当我有多个MenuItem&#xff08;或其他的控件&#xff09;绑定到同一个Command时&#xff0c;如果因为某些需要删除了其中的一个控件&#xff08;Dispose了&#xff09;…

下一版本Windowsreg; CE 开发工具Smart Device Extensions for Microsoft Visual Studioreg; .NET...

初识 Smart Device Extensions Larry RoofTonked.com 2001年10月23日 上个月我曾说过我会前往 Microsoft 学院&#xff0c;了解下一版本的小型工具的情况。此行的目的是为我不久要撰写的杂志文章和已签约的书籍搜集一些背景知识。但在回来的路上&#xff0c;我改变了我的初衷。…

vue 手机键盘把底部按钮顶上去

背景&#xff1a;在写提交订单页面时候&#xff0c;底部按钮当我点击输入留言信息的时候&#xff0c;底部提交订单按钮被输入法软键盘顶上去遮挡住了。 h5 ios输入框与键盘 兼容性优化实现原理&#xff1a;当页面高度发生变化的时候改变底部button的样式&#xff0c;没点击前bu…

CSS之布局(盒子的垂直布局)

盒子的垂直布局&#xff1a; <!DOCTYPE html> <html><head><meta charset"UTF-8"><title>盒子的垂直布局</title><style>.outer{background-color: #BBFFAA;/*默认情况下父元素的高度会被内容撑开*/}.inner{width: 100px…

microsoft 为microbit.org 设计的课程

这文章来至https://www.microbit.co.uk/blocks/lessons &#xff0c;是由microsoft 为microbit.org 设计的课程 Microbit Shop 入门课程 Beautiful Image, 用LEDs&#xff0c;秀美丽的图样Lucky 7, 秀数字在 LED 屏幕上Answering Machine, 使用字符串秀文字讯息Game of Chance…

Java, Mono, or C++?

Thoughts on the future of open source desktop development文章地址: http://ometer.com/desktop-language.html转载于:https://www.cnblogs.com/dudu/archive/2004/08/25/36317.html

CSS之布局(外边距的折叠)

外边距的折叠【重叠】&#xff1a; <!DOCTYPE html> <html><head><meta charset"UTF-8"><title>外边距的折叠</title><style>.box1,.box2{width: 200px;height: 200px;}/*垂直外边距的重叠(折叠)-相邻的垂直方向外边距会…

图论--欧拉路,欧拉回路(小结)

在题目中在慢慢细说概念 1.HDU - 3018 Ant Trip 题目大意&#xff1a;又N个村庄&#xff0c;M条道路。问须要走几次才干将全部的路遍历 解题思路&#xff1a;这题问的是有关欧拉路的判定 欧拉路就是每条边仅仅能走一次&#xff0c;且要遍历全部的边&#xff0c;简单的说就是…

轻松一下,看看vs.net2002变态的智能提示,不知道算不算bug

https://images.cnblogs.com/cnblogs_com/jjstar/2750/r_joke.jpg转载于:https://www.cnblogs.com/jjstar/archive/2004/08/27/36953.html

C++构造函数(一)

本篇是介绍C的构造函数的第一篇&#xff08;共二篇&#xff09;&#xff0c;属于读书笔记&#xff0c;对C进行一个系统的复习。 构造函数的概念和作用 全局变量未初始化时为0&#xff0c;局部变量未初始化时的值却是无法预测的。这是因为&#xff0c;全局变量的初始化是再程序装…

CSS之布局(行内元素的盒模型)

行内元素的盒模型&#xff1a; <!DOCTYPE html> <html><head><meta charset"UTF-8"><title>行内元素的盒模型</title><style>.s1{background-color: yellow;/*行内元素的盒模型&#xff1a;-行内元素不支持设置宽度和高度…

es安装的时候遇到的所有的坑

不允许root用户启动。 解决办法&#xff0c;创建子用户。 在linux下需要注意。es默认不能用root用户启动。我们需要新建一个用户来启动。 groupadd es adduser es-user -g 用户组 -p 密码 #新建一个es-user用户 密码可以省略 chown -R es-user:es /usr/local/elk/ …

MD5 - Bump Mapping

使用《mathematics for 3d game programming & computer graphics》中介绍的方法计算tangent basis.需要注意的一点是&#xff0c;在计算tangent basis的时候&#xff0c;最好是采用顶点的法线而非三角形的&#xff0c;否则将会产生非常严重的不平滑过渡。没有开启Bump Map…

『03网络』 实验一:多功能浏览器的使用和个人Blog的创建和使用

实验一&#xff1a;多功能浏览器的使用和个人Blog的创建和使用<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" />一、 实验目的1、熟悉各种浏览器的使用和配置&#xff1b;2、创建个人Blog&#xff0c;并加以完善。二、 …

SQL Server 最佳实践分析器使用小结

Best Practices Analyzer Tool for Microsoft SQL Server 2000是Microsoft SQL Server开发团队开发的一个数据库管理工具&#xff0c;可以让你检测设计的数据库是否遵循SQL Server操作和管理的最佳实践准则。这些准则公认有助于提高数据库的性能和效率&#xff0c;并让应用程序…

Vue 框架-02-事件:点击, 双击事件,鼠标移上事件

Vue 框架-02-事件&#xff1a;点击, 双击事件,鼠标移上事件 1.单击事件&#xff1a;v-on:click 源码 app2.js &#xff1a; //实例化 vue 对象 new Vue({//注意代码格式//el&#xff1a;element 需要获取的元素&#xff0c;一定是 html 中的根容器元素el:"#vue-app",…

HTML5 canvas绘制雪花飘落

Canvas是HTML5新增的组件&#xff0c;它就像一块幕布&#xff0c;可以用JavaScript在上面绘制各种图表、动画等。没有Canvas的年代&#xff0c;绘图只能借助Flash插件实现&#xff0c;页面不得不用JavaScript和Flash进行交互。有了Canvas&#xff0c;我们就再也不需要Flash了&a…

CSS之布局(默认样式)

默认样式&#xff1a; <!DOCTYPE html> <html><head><meta charset"UTF-8"><title>默认样式</title><!--重置样式表&#xff1a;专门用来对浏览器的样式进行重置的reset.css 直接去除浏览器的默认样式normalize.css 对默认样…

Junit资料汇集

Junit资料汇集 提交时间: 2004-2-24 17:23:10 回复 发消息 JUnit入門http://www.dotspace.twmail.org/Test/JUnit_Primer.htm怎样使用Junit Framework进行单元测试的编写http://www.chinaunix.net/bbsjh/14/546.htmlAntJunitLog4JCVS进行XP模式开发的建立http://ejb.cn/modu…

LESS 的 operation 是 特性

LESS 的 operation 是 特性&#xff0c;其实简单的讲&#xff0c;就是对数值型的 value&#xff08;数字、颜色、变量等&#xff09;进行加减乘除四则运算。 例&#xff1a; 清单 1 . LESS 文件 12345init: #111111; transition: init*2; .switchColor { color: transition; }经…

测一测你的blog魔症有多严重

测一测你的blog魔症有多严重 在Donews.net那里看到了这个有趣的测试&#xff1a;Are You a Blogaholic? 用来测试你对Blog的迷恋程度。 下面是我的得分与评价&#xff1a;14058 people have taken this silly test so far. 3626 people have scored higher than you. 9297 pe…

CSS之布局(盒子的尺寸)

盒子的尺寸&#xff1a; <!DOCTYPE html> <html><head><meta charset"UTF-8"><title>盒子的尺寸</title><style>.box1{width: 100px;height: 100px;background-color: #BBFFAA;padding: 10px;border: 10px solid red;/*默…

自己写的一个测试函数执行效率的单元(test on Delphi 7)

运用了一点技巧来实现对函数进行效率测试使用方法:uses Profile;.......function TForm1.Func1():string;begin TFunctionTimeProfiler.ExecuteTest(ClassName, Func1); //这里会创建一个接口实例,并开始测试; 此实例会自动释放并结束测试 ....end;程序最后退出会自动生…

datatable自动增加序号

{"targets": [0],"visible": true,"render": function (data, type, full, meta) {var id full.id;if (id) {return meta.row 1 meta.settings._iDisplayStart;} else {return ;}} },此方法有点小bug,推荐用下面的方法。 var table $(#myTabl…

CSS之布局(轮廓和圆角)

轮廓和圆角&#xff1a; <!DOCTYPE html> <html><head><meta charset"UTF-8"><title>轮廓和圆角</title><style>.box1{width: 200px;height: 200px;background-color: #BBFFAA;/*box-shadow用来设置元素的的阴影效果&…

Idea项目遇到的错误整理

解决方案 1.Maven 加入新的子模块module, 重新编译报错&#xff1a;找不到类/符号/程序包 需要清空Idea缓存&#xff0c;重新编译 File -> Invalidate Cahes... 转载于:https://www.cnblogs.com/atongmumu/p/7027050.html

对不起,我爱你

在学校上传了一部“对不起&#xff0c;我爱你”&#xff0c;据说很多人都喜欢看&#xff0c;对我 而言没有时间去看了&#xff0c;不过原声大碟倒是常常放到我的“Beep-media-player”里边&#xff0c;大四了&#xff0c;也常常觉得时间的珍贵&#xff0c;许多事情仿佛也懂了许…