In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to define and use resultMap in mybatis". In daily operation, I believe many people have doubts about how to define and use resultMap in mybatis. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to define and use resultMap in mybatis". Next, please follow the editor to study!
We know that there is a pojo object mapping in the mybatis framework, which directly encapsulates the query results into objects and returns them to us, but if the columns in the database are inconsistent with the class property names in java, or if the objects we actually return need to be associated with other objects (that is, objects of other classes are used as member variables of our class), then it is definitely not possible to use resultType at this time.
Here we need to define resultMap to complete our requirements.
The process of defining resultMap is to describe how to load objects from the database result set
ResultMap is mostly used for mapping relationships between multi-table queries, such as:
Let's take departments and employees as an example. There are multiple employees in a department, and one employee belongs to one department.
Establish department table and employee table
CREATE TABLE dept (--Department table id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR (10)) CREATE TABLE employee (--employee form id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR (10), age INT, deptId INT)
Create Dept (department) class and Employee (employee) class in java
Public class Employee {/ / employee class private Integer id; private String name; private Integer age; / / an employee is associated with a department private Dept dept; public Integer getId () {return id;} public void setId (Integer id) {this.id = id;} public String getName () {return name } public void setName (String name) {this.name = name;} public Integer getAge () {return age;} public void setAge (Integer age) {this.age = age;} public Dept getDept () {return dept;} public void setDept (Dept dept) {this.dept = dept } @ Override public String toString () {return "Employee {" + "id=" + id + ", name='" + name +'\'+ ", age=" + age + ", dept=" + dept +'}';}} public class Dept {private Integer id; private String name / / multiple employees in a department, use the List collection to store private List list; public Integer getId () {return id;} public void setId (Integer id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name;} public List getList () {return list } public void setList (List list) {this.list = list;} @ Override public String toString () {return "Dept {" + "id=" + id + ", name='" + name +'\'+ ", list=" + list +'}';}}
First, we query the employees and define the dao layer interface:
Public interface EmployeeDao {/ / query all employees List selectAllEmployee ();}
Because we associate other objects in the object, it is no longer a normal mapping, here we define resultMap
SELECT e.idjie. Name ename,e.age,d.name dname FROM employee e LEFT JOIN dept d ON e.deptId = d.id
The id in resultMap is the unique identity, which is equivalent to the name, and the type type is the type we want to return.
Use resultMap in, and pass in the newly defined id
So we can get the mapping format we want in the java code.
Inquiry department:
Public interface DeptDao {/ / query department List selectDept ();}
Define and use resultMap
SELECT d.idjold d.name dname,e.name ename FROM dept d LEFT JOIN employee e ON d.id = e.deptId
Here JavaType we choose list, because we use the list collection to store multiple employee information. OfType is the actual object name contained in the list collection, and here is the employee Employee.
Through resultMap, we can get the mapping relationship we want.
At this point, the study on "how to define and use resultMap in mybatis" 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.