激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

香港云服务器
服務(wù)器之家 - 編程語言 - Java教程 - MyBatis逆向工程的創(chuàng)建和使用

MyBatis逆向工程的創(chuàng)建和使用

2020-12-15 14:58LuckyBao Java教程

這篇文章主要介紹了MyBatis逆向工程的創(chuàng)建和使用,需要的朋友可以參考下

1.什么是逆向工程

mybaits需要程序員自己編寫sql語句,mybatis官方提供逆向工程 可以針對單表自動(dòng)生成mybatis執(zhí)行所需要的代碼(mapper.java,mapper.xml、po..)

企業(yè)實(shí)際開發(fā)中,常用的逆向工程方式:

由于數(shù)據(jù)庫的表生成java代碼。

2.下載逆向工程

mybatis-generator-core-1.3.2-bundle.zip

3.使用方法(會用)

3.1運(yùn)行逆向工程

官方文檔中提供的運(yùn)行逆向工程的幾種方法

running mybatis generator

mybatis generator (mbg) can be run in the following ways:

(1)from the command prompt with an xml configuration

(2)as an ant task with an xml configuration

(3)as a maven plugin

(4)from another java program with an xml configuration

(5)from another java program with a java based configuration

(6)還可以通過eclipse的插件生成代碼

建議使用java程序方式(from another java program with an xml configuration),不依賴開發(fā)工具。

下面創(chuàng)建一個(gè)生成逆向文件的工程,將自動(dòng)生成的文件再拷貝到原工程中去(這么做是為了放止直接在源文件中生成會覆蓋掉同名文件)

導(dǎo)入的jar包和工程結(jié)構(gòu)截圖如下:

如圖

MyBatis逆向工程的創(chuàng)建和使用

3.2生成代碼配置文件

generatorconfig.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?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="testtables" targetruntime="mybatis3">
 <commentgenerator>
  <!-- 是否去除自動(dòng)生成的注釋 true:是 : false:否 -->
  <property name="suppressallcomments" value="true" />
 </commentgenerator>
 <!--數(shù)據(jù)庫連接的信息:驅(qū)動(dòng)類、連接地址、用戶名、密碼 -->
 <jdbcconnection driverclass="com.mysql.jdbc.driver"
  connectionurl="jdbc:mysql://localhost:3306/mybatis" userid="root"
  password="1234">
 </jdbcconnection>
 <!-- <jdbcconnection driverclass="oracle.jdbc.oracledriver"
  connectionurl="jdbc:oracle:thin:@127.0.0.1:1521:yycg"
  userid="yycg"
  password="yycg">
 </jdbcconnection> -->
 <!-- 默認(rèn)false,把jdbc decimal 和 numeric 類型解析為 integer,為 true時(shí)把jdbc decimal 和
  numeric 類型解析為java.math.bigdecimal -->
 <javatyperesolver>
  <property name="forcebigdecimals" value="false" />
 </javatyperesolver>
 <!-- targetproject:生成po類的位置 -->
 <javamodelgenerator targetpackage="cn.edu.hpu.ssm.po"
  targetproject=".\src">
  <!-- enablesubpackages:是否讓schema作為包的后綴 -->
  <property name="enablesubpackages" value="false" />
  <!-- 從數(shù)據(jù)庫返回的值被清理前后的空格 -->
  <property name="trimstrings" value="true" />
 </javamodelgenerator>
 <!-- targetproject:mapper映射文件生成的位置 -->
 <sqlmapgenerator targetpackage="cn.edu.hpu.ssm.mapper"
  targetproject=".\src">
  <!-- enablesubpackages:是否讓schema作為包的后綴 -->
  <property name="enablesubpackages" value="false" />
 </sqlmapgenerator>
 <!-- targetpackage:mapper接口生成的位置 -->
 <javaclientgenerator type="xmlmapper"
  targetpackage="cn.edu.hpu.ssm.mapper"
  targetproject=".\src">
  <!-- enablesubpackages:是否讓schema作為包的后綴 -->
  <property name="enablesubpackages" value="false" />
 </javaclientgenerator>
 <!-- 指定數(shù)據(jù)庫表 -->
 <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執(zhí)行生成程序

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
generatorsqlmap.java:
import java.io.file;
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.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();
 }
 }
}

生成后的代碼:

如圖

MyBatis逆向工程的創(chuàng)建和使用

3.4使用生成的代碼

需要將生成工程中所生成的代碼拷貝到自己的工程中。我們這里吧itemsmapper.java和itemsmapper.xml、items、itemsexample類拷入我們的原工程。

測試itemsmapper中的方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package cn.edu.hpu.ssm.test;
 import static org.junit.assert.fail;
import java.util.date;
import java.util.list;
import org.junit.before;
import org.junit.test;
import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;
import cn.edu.hpu.ssm.mapper.itemsmapper;
import cn.edu.hpu.ssm.po.items;
import cn.edu.hpu.ssm.po.itemsexample;
public class itemsmappertest {
 private applicationcontext applicationcontext;
 private itemsmapper itemsmapper;
 //注解before是在執(zhí)行本類所有測試方法之前先調(diào)用這個(gè)方法
 @before
 public void setup() throws exception{
 applicationcontext=new classpathxmlapplicationcontext("classpath:spring/applicationcontext.xml");
 itemsmapper=(itemsmapper)applicationcontext.getbean("itemsmapper");
 }
 //根據(jù)主鍵刪除
 @test
 public void testdeletebyprimarykey() {
 fail("not yet implemented");
 }
 //插入
 @test
 public void testinsert() {
 items items=new items();
 items.setname("iphone-5s");
 items.setprice(3999f);
 items.setdetail("正品行貨");
 items.setpic("sdasd.jpg");
 items.setcreatetime(new date());
 itemsmapper.insert(items);
 }
 //自定義條件來查詢
 @test
 public void testselectbyexample() {
 itemsexample itemsexample=new itemsexample();
 //通過criteria構(gòu)造查詢條件
 itemsexample.criteria criteria=itemsexample.createcriteria();
 criteria.andnameequalto("電視機(jī)");
 //可能返回多條記錄
 list<items> list=itemsmapper.selectbyexample(itemsexample);
 for (int i = 0; i < list.size(); i++) {
  items it=list.get(i);
  system.out.println(it.getid()+":"+it.getname());
 }
 }
 //根據(jù)主鍵來查詢
 @test
 public void testselectbyprimarykey() {
 items items=itemsmapper.selectbyprimarykey(1);
 system.out.println(items.getname());
 }
 //更新數(shù)據(jù)
 @test
 public void testupdatebyprimarykey() {
 //對所有字段進(jìn)行更新,需要先查詢出來再更新
 items items = itemsmapper.selectbyprimarykey(1);
 items.setname("iphone");
 itemsmapper.updatebyprimarykey(items);
 //如果傳入字段不空為才更新,在批量更新中使用此方法,不需要先查詢再更新
 //itemsmapper.updatebyprimarykeyselective(record);
 }
}

總結(jié)

以上所述是小編給大家介紹的mybatis逆向工程的創(chuàng)建和使用,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!

原文鏈接:http://www.cnblogs.com/luckypo/archive/2017/08/14/7356249.html

延伸 · 閱讀

精彩推薦
657
主站蜘蛛池模板: 一区免费| 成人免费av在线播放 | 久久一区三区 | 香蕉久久久 | 色综合欧美| 欧美高清在线精品一区二区不卡 | 万圣街在线观看免费完整版 | 二区三区偷拍浴室洗澡视频 | 精国品产一区二区三区有限公司 | 欧美日韩免费一区 | 国内成人自拍视频 | 国产成人羞羞视频在线 | 亚洲一级电影在线观看 | 99精品电影 | 久久恋 | 美国一级毛片片aa久久综合 | 国产一级在线观看视频 | 色淫视频 | 久久久久电影网站 | 国产又白又嫩又紧又爽18p | 黄色伊人网站 | 日本a级一区 | 操穴视频 | 2019亚洲日韩新视频 | 国产孕妇孕交大片孕 | 在线中文字幕播放 | 久久av一区二区 | 国产亚洲精品成人 | 亚洲第一页中文字幕 | 日本不卡一区二区三区在线 | 天天色综合2 | 国产高清毛片 | 男女一边摸一边做羞羞视频免费 | 久久精品一区视频 | 秋霞a级毛片在线看 | 一级片999 | 亚欧在线免费观看 | 久久无毛 | 91网站永久免费看 | 在线天堂中文在线资源网 | 成年人高清视频在线观看 |