Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize mybatis Mapping

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to achieve mybatis mapping". In daily operation, I believe many people have doubts about how to achieve mybatis mapping. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "how to achieve mybatis mapping". Next, please follow the editor to study!

Implementation of Mybatis 1-to-1 Association

Through the resultType way

By cascading attributes [resultType and resultMap]

By means of association association

Through the step-by-step query of association

Through the double association association of the wrapper class

Through the nesting definition of association

Case: query the user information corresponding to the order

Sql construction table statement

User table:

-- Table structure for `user`

DROP TABLE IF EXISTS `user`

CREATE TABLE `user` (

`id`int (11) NOT NULL AUTO_INCREMENT

`username`varchar (32) NOT NULL COMMENT 'user name'

`birthday day`date DEFAULT NULL COMMENT 'birthday'

`sex` char (1) DEFAULT NULL COMMENT 'gender'

`address` varchar (256) DEFAULT NULL COMMENT 'address'

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8

-- Records of user

INSERT INTO `user`VALUES ('1Qing,' Wang Wu', '2019-08-30th,' 2Qing, 'Hangzhou')

INSERT INTO `user`VALUES ('1011', 'Zhang San', '2014-07-10', '1Qing,' Beijing')

INSERT INTO `user`VALUES ('1600,' Zhang Xiaoming', '2019-08-15,' 1Qing, 'Zhengzhou, Henan')

INSERT INTO `user`VALUES ('22Qing,' Chen Xiaoming', '2019-08-08,' 1Qing, 'Zhengzhou, Henan')

INSERT INTO `user`VALUES ('249,' Zhang Sanfeng', '2019-08-15,' 1Qing, 'Changsha')

INSERT INTO `user`VALUES ('259,' Wu Xiaoming', '2019-08-08,' 1Qing, 'Zhengzhou, Henan')

INSERT INTO `user`VALUES ('26qu,' Wang Wu', '2019-08-1400,' 2Qing, 'Wuhan')

Order form:

-- Table structure for `orders`

DROP TABLE IF EXISTS `orders`

CREATE TABLE `orders` (

`id`int (11) NOT NULL AUTO_INCREMENT

`user_ id`int (11) NOT NULL COMMENT 'place order user id'

`number`varchar (32) NOT NULL COMMENT 'order number'

`createtime`datetime NOT NULL COMMENT 'create order time'

`note`varchar (100) DEFAULT NULL COMMENT 'remarks'

PRIMARY KEY (`id`)

KEY `FK_orders_ 1` (`user_ id`)

CONSTRAINT `FK_orders_ id` FOREIGN KEY (`user_ id`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION

) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8

-- Records of orders

INSERT INTO `orders` VALUES ('3mm,' 1mm, '1000010mm,' 2015-02-04 1315 null)

INSERT INTO `orders` VALUES ('418,' 1, '1000011,' 2015-02-03 13 22 null)

INSERT INTO `orders` VALUES ('515,' 10, '1000012,' 2015-02-12, 16-13-13-15-23, null)

INSERT INTO `orders` VALUES ('6mm,' 10mm, '1000013th,' 2015-08-30 10V 11 null)

INSERT INTO `orders` VALUES ('7,'16, '100014,' 2019-08-16, 12-14, null)

Entity class:

User: User

Public class User {

Private Integer id

Private String name

Private Date birthday

Private String sex

Private String address

/ / getter.. Setter.. ToString..

}

Order: Orders

Public class Orders {

Private Integer id

Private Integer userId

Private String number

Private Date createtime

Private String note

/ / getter.. Setter.. ToString..

}

Method 1: resultType mode

One order corresponds to one user.

Extend the order entity class: [add only the corresponding fields; for example, the user name in the case]

Create a new extension class for OrdersExtByColumn, inherit Orders, and add corresponding fields

Public class OrdersExtByColumn extends Orders {

Private String username

/ / getter.. Setter.. ToString..

}

In the OrdersMapper mapping file, write the corresponding sql statement

SELECT o.id,number,createtime,note,user_id userId,username

FROM orders o, user u

WHERE o.id = u.id and o.id = # {id}

Write a test program:

Public class TestAssociation {

Private SqlSession sqlSession

@ Before

Public void initSqlSession () {

/ / 1. Read the core configuration file SqlMapConfig.xml

InputStream resourceAsStream = TestMain.class.getClassLoader () .getResourceAsStream ("SqlMapConfig.xml")

/ / 2. Generate SqlSessionFactory

SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder () .build (resourceAsStream)

/ / 3. Generate SqlSession

SqlSession = sqlSessionFactory.openSession ()

}

@ Test

Public void testQryName () {

/ / find the corresponding sqlID

String sqlID = "orders.queryOrdersByType"

/ / sqlSession performs the corresponding database operation

OrdersExtByColumn orderInfo = sqlSession.selectOne (sqlID, 3)

/ / A pair of query results are processed

System.out.println (orderInfo)

}

@ After

Public void closeResource () {

SqlSession.close ()

}

}

Method 2: the way of cascading attributes

In the orders class, add the cascading attribute User user

ResultType mode:

ResultMap mode:

Method 3: the way of association association

Method 4: step-by-step query of association

Step-by-step query: that is, multiple sql queries calculate the results

In this way, a delayed cache can be generated in combination with the configuration item of mybatis core configuration file mybatisCfg.xml.

MybatisCfg.xml delay loading related settings

Method 5: the association of the double association of the wrapper class

Create a new wrapper class:

OrdersAndUser

Public class OrdersAndUser {

Private Orders orders

Private User user

/ / getter.. Setter..

}

Method 6: embedded association cascade

This embedded association is associated with three tables.

The requirement is simply changed as follows: query the order and the corresponding user information from the details of an order

Add a list of order details

-- Table structure for `orderdetail`

DROP TABLE IF EXISTS `orderdetail`

CREATE TABLE `orderdetail` (

`id`int (11) NOT NULL AUTO_INCREMENT

`orders_ id`int (11) NOT NULL COMMENT 'order id'

`items_ id` int (11) NOT NULL COMMENT 'Commodity id'

`items_ Num`int (11) DEFAULT NULL COMMENT 'quantity purchased'

PRIMARY KEY (`id`)

KEY `FK_orderdetail_ 1` (`orders_ id`)

KEY `FK_orderdetail_ 2` (`items_ id`)

CONSTRAINT `FK_orderdetail_ 1` FOREIGN KEY (`orders_ id`) REFERENCES `orders` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION

CONSTRAINT `FK_orderdetail_ 2` FOREIGN KEY (`items_ id`) REFERENCES `items` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION

) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8

-- Records of orderdetail

INSERT INTO `orderdetail` VALUES ('1th,' 3pm, '1th,' 1')

INSERT INTO `orderdetail` VALUES ('2percent,' 3percent, '2percent,' 3')

INSERT INTO `orderdetail` VALUES ('3percent,' 4percent, '3percent,' 4')

INSERT INTO `orderdetail` VALUES ('4percent,' 4percent, '2percent,' 3')

New OrderDeatil class [Associated Orders attribute]

Public class OrderDetail {

Private Integer id

Private Integer orderId

Private Integer itemsId

Private Integer itemsNum

Private Orders orders

/ / setter.. Getter..

}

At this point, the study on "how to implement mybatis mapping" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report