1.什么是mybatis逆向工程
在使用mybatis時(shí)需要程序員自己編寫sql語句,針對(duì)單表的sql語句量是很大的,mybatis官方提供了一種根據(jù)數(shù)據(jù)庫表生成mybatis執(zhí)行代碼的工具,這個(gè)工具就是一個(gè)逆向工程。
逆向工程:針對(duì)數(shù)據(jù)庫單表—->生成代碼(mapper.xml、mapper.java、pojo。。)
mybatis-generator-core-1.3.2.jar—逆向工程運(yùn)行所需要的jar核心 包
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
63
64
65
66
|
<?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= "123" > </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.zm.mybatis.po" targetproject= ".\src" > <!-- enablesubpackages:是否讓schema作為包的后綴 --> <property name= "enablesubpackages" value= "false" /> <!-- 從數(shù)據(jù)庫返回的值被清理前后的空格 --> <property name= "trimstrings" value= "true" /> </javamodelgenerator> <!-- targetproject:mapper映射文件生成的位置 --> <sqlmapgenerator targetpackage= "cn.zm.mybatis.mapper" targetproject= ".\src" > <!-- enablesubpackages:是否讓schema作為包的后綴 --> <property name= "enablesubpackages" value= "false" /> </sqlmapgenerator> <!-- targetpackage:mapper接口生成的位置 --> <javaclientgenerator type= "xmlmapper" targetpackage= "cn.zm.mybatis.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.執(zhí)行逆向工程生成代碼
執(zhí)行java類方法:
生成的代碼如下:
4.將生成的代碼拷貝到業(yè)務(wù)系統(tǒng)工程中測(cè)試
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
|
public class itemsmappertest { private applicationcontext applicationcontext; private itemsmapper itemsmapper; @before public void setup() throws exception { applicationcontext = new classpathxmlapplicationcontext( "classpath:applicationcontext.xml" ); itemsmapper = (itemsmapper) applicationcontext.getbean( "itemsmapper" ); } //根本主鍵刪除 @test public void deletebyprimarykey() { itemsmapper.deletebyprimarykey( 4 ); } @test public void insert() { } @test public void selectbyexample() { itemsexample itemsexample = new itemsexample(); itemsexample.criteria criteria = itemsexample.createcriteria(); //使用criteria自定義查詢條件 criteria.andnameequalto( "水杯" ); criteria.andidequalto( 1 ); list<items> list = itemsmapper.selectbyexample(itemsexample); system.out.println(list); } @test public void selectbyprimarykey() { items items = itemsmapper.selectbyprimarykey( 1 ); system.out.println(items); } @test public void updatebyprimarykey() { } } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/happy_meng/article/details/79058351