mybatis关系映射之一对多和多对一

2025-05-29 0 87

本实例使用用户和订单的例子做说明:一个用户可以有多个订单, 一个订单只对应一个用户。(其中应用到注释)

1.代码的结构

mybatis关系映射之一对多和多对一

2. 建表语句:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24
create database test;

use test;

create table person(

personid varchar(36) primary key,

personname varchar(64),

personaddress varchar(128),

persontel varchar(11)

);

create table orders(

orderid varchar(36) primary key,

ordernumber varchar(20),

orderprice int,

pid varchar(36)

);

insert into person values('1', '木子', '湖北', '110');

insert into person values('2', '木子大大', '武汉', '120');

insert into person values('1', '木子苗苗', '天门', '119');

insert into orders values('1', '001', 100, '1');

insert into orders values('2', '002', 200, '1');

insert into orders values('3', '003', 300, '2');

insert into orders values('4', '004', 400, '2');

insert into orders values('5', '005', 500, '3');

select p.*, o.* from person p join orders o on (p.personid=o.pid) where p.personid = '1' ;

*指显示所有字段

3. 用户实体:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18
package com.mybatis.domain;

import java.util.list;

import lombok.data;

@data//注释(person为单方)

public class person {

private string personid;

private string personname;

private string personaddress;

private string persontel;

//这个代表多方里面的内容(orders)

private list<orders> orders;

@override

public string tostring() {

return "person [personid=" + personid + ", personname=" + personname

+ ", personaddress=" + personaddress + ", persontel="

+ persontel + ", orders=" + orders + "]";

}

}

4. 订单实体:

?

1

2

3

4

5

6

7

8

9

10
package com.mybatis.domain;

import lombok.data;

@data//(orders为多方)

public class orders {

private string orderid;

private string ordernumber;

private integer orderprice;

//对象(单方person)与外键进行关联

private person person;

}

5.写personmapper.java的接口

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
package com.mybatis.dao.mapper;

import com.mybatis.domain.orders;

import com.mybatis.domain.person;

import java.util.list;

public interface personmapper {

int deletebyprimarykey(string personid);

int insert(person record);

person selectbyprimarykey(string personid);

list<person> selectall();

int updatebyprimarykey(person record);

//一对多查询(根据id查询)

public list<orders> findpersonandorders(string pid);

//一对多查询返回一个对象

public person selectpersonbyid(string id);

}

6. 一对多实体配置: personmapper.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
<?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.mybatis.dao.mapper.personmapper" >

<resultmap id="personresultmap" type="com.mybatis.domain.person" >

<id column="personid" property="personid" jdbctype="varchar" />

<result column="personname" property="personname" jdbctype="varchar" />

<result column="personaddress" property="personaddress" jdbctype="varchar" />

<result column="persontel" property="persontel" jdbctype="varchar" />

<!-- 一对多的关系(这个是关联集合)这个是orders里面的多方 -->

<!-- property: 指的是集合属性的名, oftype:指的是集合中元素的类型的路径 (实现类)-->

<collection property="orders" oftype="com.mybatis.domain.orders">

<!-- id有一个单独标签 -->

<id column="orderid" property="orderid"/>

<!--column指sql中字段的名字 property指java中对应sql中属性的名 -->

<result column="ordernumber" property="ordernumber"/>

<result column="orderprice" property="orderprice"/>

</collection>

</resultmap>

<!-- 根据id查询person, 关联将orders查询出来(注意放置的位置) -->

<select id="findpersonandorders" parametertype="string" resultmap="personresultmap">

select p.*,o.* from person o,orders b where o.personid=#{pid};

</select>

<select id="selectpersonbyid" parametertype="string" resultmap="personresultmap">

select p.*, o.* from person p, orders o where p.personid = o.pid and p.personid = #{id}

</select>

</mapper>

7.写ordersmapper.java的接口

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
package com.mybatis.dao.mapper;

import com.mybatis.domain.orders;

import java.util.list;

public interface ordersmapper {

int deletebyprimarykey(string orderid);

int insert(orders record);

orders selectbyprimarykey(string orderid);

list<orders> selectall();

int updatebyprimarykey(orders record);

//多查一 根据id

public orders selectorderbyid(string oid);

//多查一 根据ordernumber

public orders selectordernumber(string number);

}

8.多对一实体配置:ordersmapper.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
<?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.mybatis.dao.mapper.ordersmapper" >

<resultmap id="ordersresultmap" type="com.mybatis.domain.orders" >

<id column="orderid" property="orderid" jdbctype="varchar" />

<result column="ordernumber" property="ordernumber" jdbctype="varchar" />

<result column="orderprice" property="orderprice" jdbctype="integer" />

<!--

<result column="pid" property="pid" jdbctype="varchar" />

-->

<!-- 多对一的关系 这个是person里面的单方 -->

<!-- property: 指的是属性的值, javatype:指的是属性的类型的路径 (实现类)-->

<association property="person" javatype="com.mybatis.domain.person">

<!--注意:在此column和property的值要一样都为person的属性 -->

<id column="personid" property="personid"/>

<result column="personname" property="personname"/>

<result column="personaddress" property="personaddress"/>

<result column="persontel" property="persontel"/>

</association>

</resultmap>

<!-- 根据id查询order, 关联将person查询出来 -->

<select id="selectorderbyid" parametertype="string" resultmap="ordersresultmap">

select p.*, o.* from person p, orders o where p.personid = o.pid and o.orderid = #{oid}

</select>

<!-- 根据ordernumber查询order, 关联将person查询出来 -->

<select id="selectordernumber" parametertype="string" resultmap="ordersresultmap">

select p.*, o.* from person p, orders o where p.personid = o.pid and o.orderid = #{number}

</select>

</mapper>

9.其他配置

?

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

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118
db.properties配置(sql语句的基本链接)

db.driver=com.mysql.jdbc.driver

db.url=jdbc:mysql://localhost:3306/wang1?useunicode=true&characterencoding=utf8

db.username=root

db.password=123456

log4j.properties配置(注释)

# global logging configuration

log4j.rootlogger=debug, stdout

# console output...

log4j.appender.stdout=org.apache.log4j.consoleappender

log4j.appender.stdout.layout=org.apache.log4j.patternlayout

log4j.appender.stdout.layout.conversionpattern=%5p [%t] - %m%n

mybatis.xml(逆向生成domain、dao层)

<?xml version="1.0" encoding="utf-8" ?>

<!doctype configuration

public "-//mybatis.org//dtd config 3.0//en"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

<!-- mybatis核心配置文件 -->

<!-- 加载java的配置文件或者声明属性信息 -->

<properties resource="db.properties">

</properties>

<!-- alias别名 -->

<typealiases> <!--这里需要修改 domain层的路径-->

<typealias type="com.mybatis.domain.person" alias="person" />

<typealias type="com.mybatis.domain.orders" alias="orders" />

</typealiases>

<!-- 配置mybatis的环境信息,与spring整合,该信息由spring来管理

如果说我们需要连接数据库,那么必须在mybatis中配置环境 运行环境

-->

<environments default="development">

<environment id="development">

<!-- 配置jdbc事务控制,由mybatis进行管理 -->

<transactionmanager type="jdbc"></transactionmanager>

<!-- 配置数据源,采用mybatis连接池 -->

<datasource type="pooled">

<property name="driver" value="${db.driver}" />

<property name="url" value="${db.url}" />

<property name="username" value="${db.username}" />

<property name="password" value="${db.password}" />

</datasource>

</environment>

</environments>

<!-- 加载映射文件(注意反\\)-->

<mappers> <!--这里需要修改 dao层的路径-->

<mapper resource="com\\mybatis\\dao\\mapper\\personmapper.xml"/>

<mapper resource="com\\mybatis\\dao\\mapper\\ordersmapper.xml"/>

</mappers>

</configuration>

generatorconfig.xml配置(对mysql进行操作)下面标红部分根据自己建立的进行修改

<?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> d盘中要有此包mysql-connector-java-5.1.7-bin.jar

<classpathentry location="d:\\mysql-connector-java-5.1.7-bin.jar" />

<context id="mysql" defaultmodeltype="hierarchical" targetruntime="mybatis3simple">

<!-- 自动识别数据库关键字,默认false,如果设置为true,根据sqlreservedwords中定义的关键字列表;

一般保留默认值,遇到数据库关键字(java关键字),使用columnoverride覆盖 -->

<property name="autodelimitkeywords" value="false" />

<!-- 生成的java文件的编码 -->

<property name="javafileencoding" value="utf-8" />

<!-- beginningdelimiter和endingdelimiter:指明数据库的用于标记数据库对象名的符号,比如oracle就是双引号,mysql默认是`反引号; -->

<property name="beginningdelimiter" value="`" />

<property name="endingdelimiter" value="`" />

<!-- 注释生成器 -->

<commentgenerator>

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

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

</commentgenerator>

<!-- 必须要有的,使用这个配置链接数据库 @todo:是否可以扩展 -->

<jdbcconnection driverclass="com.mysql.jdbc.driver"

connectionurl="jdbc:mysql://localhost:3306/wang1" userid="root" password="123456">

<!-- 这里面可以设置property属性,每一个property属性都设置到配置的driver上 -->

</jdbcconnection>

<!-- java模型创建器,是必须要的元素 负责:1,key类(见context的defaultmodeltype);2,java类;3,查询类

targetpackage:生成的类要放的包,真实的包受enablesubpackages属性控制;

targetproject:目标项目,指定一个存在的目录下,生成的内容会放到指定目录中,如果目录不存在,mbg不会自动建目录 -->

<javamodelgenerator targetpackage="com.mybatis.domain" targetproject="mybatis03/src">

<!-- for mybatis3/mybatis3simple 自动为每一个生成的类创建一个构造方法,构造方法包含了所有的field;而不是使用setter; -->

<property name="constructorbased" value="false" />

<!-- for mybatis3 / mybatis3simple 是否创建一个不可变的类,如果为true, 那么mbg会创建一个没有setter方法的类,

取而代之的是类似constructorbased的类 -->

<property name="immutable" value="false" />

</javamodelgenerator>

<!-- 生成sql map的xml文件生成器, 注意,在mybatis3之后,我们可以使用mapper.xml文件+mapper接口(或者不用mapper接口),

或者只使用mapper接口+annotation,所以,如果 javaclientgenerator配置中配置了需要生成xml的话,这个元素就必须配置

targetpackage/targetproject:同javamodelgenerator -->

<sqlmapgenerator targetpackage="com.mybatis.dao.mapper" targetproject="mybatis03/src">

<!-- 在targetpackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false -->

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

</sqlmapgenerator>

<!-- 对于mybatis来说,即生成mapper接口,注意,如果没有配置该元素,那么默认不会生成mapper接口 targetpackage/targetproject:同javamodelgenerator

type:选择怎么生成mapper接口(在mybatis3/mybatis3simple下):

1,annotatedmapper:会生成使用mapper接口+annotation的方式创建(sql生成在annotation中),不会生成对应的xml;

2,mixedmapper:使用混合配置,会生成mapper接口,并适当添加合适的annotation,但是xml会生成在xml中;

3,xmlmapper:会生成mapper接口,接口完全依赖xml;

注意,如果context是mybatis3simple:只支持annotatedmapper和xmlmapper -->

<javaclientgenerator targetpackage="com.mybatis.dao.mapper" type="xmlmapper" targetproject="mybatis03/src">

<!-- 在targetpackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false -->

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

<!-- 可以为所有生成的接口添加一个父接口,但是mbg只负责生成,不负责检查 <property name="rootinterface"

value=""/> -->

</javaclientgenerator>

<!--逆向生成的文件-->

<table tablename="person" delimitidentifiers="true">

<!-- 参考 javamodelgenerator 的 constructorbased属性 -->

<property name="constructorbased" value="false" />

<generatedkey column="id" sqlstatement="jdbc"/>

</table>

<table tablename="orders" delimitidentifiers="true">

<!-- 参考 javamodelgenerator 的 constructorbased属性 -->

<property name="constructorbased" value="false" />

<generatedkey column="id" sqlstatement="jdbc"/>

</table>

</context>

</generatorconfiguration>

10.测试文件

?

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
package com.mybatis.test;

import java.io.inputstream;

import java.text.parseexception;

import java.text.simpledateformat;

import java.util.date;

import java.util.iterator;

import java.util.list;

import lombok.data;

import org.apache.ibatis.io.resources;

import org.apache.ibatis.session.sqlsession;

import org.apache.ibatis.session.sqlsessionfactory;

import org.apache.ibatis.session.sqlsessionfactorybuilder;

import org.junit.before;

import org.junit.test;

import com.mybatis.dao.mapper.ordersmapper;

import com.mybatis.dao.mapper.personmapper;

import com.mybatis.domain.orders;

import com.mybatis.domain.person;

public class teststudentmapper {

sqlsessionfactory sessionfactory = null;

// 这方法之前

@before

public void setup() throws exception {

string resource = "mybatis.xml";

// 这个是加载配置文件

inputstream inputstream = resources.getresourceasstream(resource);

// 得到会话工厂

sessionfactory = new sqlsessionfactorybuilder().build(inputstream);

}

//查询一对多 根据这个person里面的id号就能查询出这个用户有多少个订单记录

// @test

public void testselectpersonbyid(){

sqlsession sq = sessionfactory.opensession();

// 得到dao层的实现类

personmapper u = sq.getmapper(personmapper.class);

person person = u.selectpersonbyid("2");

system.out.println(person);

}

//多对一 根据多对一id进行查询

// @test//多对一关联查询

public void testselectorderbyid(){

sqlsession sq = sessionfactory.opensession();

// 得到dao层的实现类

ordersmapper u = sq.getmapper(ordersmapper.class);

orders od = u.selectorderbyid( "2");

system.out.println(od.getperson().getpersonname());

system.out.println(od.getperson().getpersonaddress());

}

@test//多对一关联查询

public void testselectordernumber(){

sqlsession sq = sessionfactory.opensession();

// 得到dao层的实现类

ordersmapper u = sq.getmapper(ordersmapper.class);

orders od = u.selectordernumber("001");

system.out.println(od.getperson().getpersonname());

system.out.println(od.getperson().getpersonaddress());

}

}

如有问题请多多指教!希望给您带来帮助!祝您生活愉快。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对快网idc的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/muzidigbig/article/details/78551132

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 mybatis关系映射之一对多和多对一 https://www.kuaiidc.com/110121.html

相关文章

发表评论
暂无评论