详解MyBatis逆向工程

2025-05-27 0 18

1.什么是mybatis逆向工程

在使用mybatis时需要程序员自己编写sql语句,针对单表的sql语句量是很大的,mybatis官方提供了一种根据数据库表生成mybatis执行代码的工具,这个工具就是一个逆向工程。
逆向工程:针对数据库单表—->生成代码(mapper.xml、mapper.java、pojo。。)

mybatis-generator-core-1.3.2.jar—逆向工程运行所需要的jar核心 包

2.配置逆向工程的配置文件

详解MyBatis逆向工程

配置文件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>

<!-- 是否去除自动生成的注释 true:是 : false:否 -->

<property name="suppressallcomments" value="true" />

</commentgenerator>

<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->

<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> -->

<!-- 默认false,把jdbc decimal 和 numeric 类型解析为 integer,为 true时把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" />

<!-- 从数据库返回的值被清理前后的空格 -->

<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>

<!-- 指定数据库表 -->

<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.执行逆向工程生成代码

详解MyBatis逆向工程

执行java类方法:

详解MyBatis逆向工程

生成的代码如下:

详解MyBatis逆向工程

4.将生成的代码拷贝到业务系统工程中测试

?

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() {

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:http://blog.csdn.net/happy_meng/article/details/79058351

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 详解MyBatis逆向工程 https://www.kuaiidc.com/76443.html

相关文章

发表评论
暂无评论