The two entity classes are as follows: User class and Goods Commodity class. One user corresponds to multiple products (one to multiple) packagecom. leo. entity; importjava. util. List; publicclassUser {privateIntegerid; privateStringusername; privateIntegerage; privateStringaddress; privateListG
The two entity classes are as follows: User class and Goods Commodity class. One user corresponds to multiple items (one-to-multiple) package com. leo. entity; import java. util. list; public class User {private Integer id; private String username; private Integer age; private String address; private ListG
The two entity classes are as follows: User class and Goods Commodity class. One user corresponds to multiple products (one to multiple)
package com.leo.entity;import java.util.List;public class User {private Integer id;private String username;private Integer age;private String address;private List
goodsList;public List
getGoodsList() {return goodsList;}public void setGoodsList(List
goodsList) {this.goodsList = goodsList;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public User() {super();// TODO Auto-generated constructor stub}@Overridepublic String toString() {return "User [id=" + id + ", username=" + username + ", age=" + age+ ", address=" + address + ", goodsList=" + goodsList + "]";}}
Goods Product
package com.leo.entity;public class Goods {private Integer id;private String goodsName;private Integer goodsNumber;private Integer user_id;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getGoodsName() {return goodsName;}public void setGoodsName(String goodsName) {this.goodsName = goodsName;}public Integer getGoodsNumber() {return goodsNumber;}public void setGoodsNumber(Integer goodsNumber) {this.goodsNumber = goodsNumber;}public Integer getUser_id() {return user_id;}public void setUser_id(Integer user_id) {this.user_id = user_id;}}
Mapper ing file of the User object class: UserDao. xml
Select u. id as u_id, u. username, u. age, u. address fromuser u where u. id =$ {value };
Mapper ing file of Goods object class: GoodsDao. xml
SELECT id, goodsName, goodsNumber, user_id FROM Goods WHERE user_id =#{ value}
Environment profile mabatis-config.xml for mabatis
Tested servlet (you can also use the main function for testing)
package com.leo.servlet;import java.io.IOException;import java.io.InputStream;import java.util.List;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.ResultHandler;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import com.leo.entity.Goods;import com.leo.entity.User;import com.leo.mapper.GoodsDao;import com.leo.mapper.UserDao;/** * Servlet implementation class MybatisServlet */@WebServlet("/MybatisServlet")public class MybatisServlet extends HttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {InputStream is = Resources.getResourceAsStream("com/leo/resources/mybatis-config.xml");SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);SqlSession session = factory.openSession();//UserDao ud = session.getMapper(UserDao.class);GoodsDao gd = session.getMapper(GoodsDao.class);List
goodsList= gd.selectGoodsForUser(1);//User user = ud.getUserinfoById(1);//System.out.println(user);//List
goodsList = user.getGoodsList();for (Goods goods : goodsList) {System.out.println(goods.getId()+" "+ goods.getGoodsName()+" "+goods.getGoodsNumber()+ " "+ goods.getUser_id());}session.commit();session.close();}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}
The above is a set nested query, and another method is the set nested results. This method only requires one object class file, which is a cascade query that is automatically completed.
The following method uses the nested result of the Set:
You only need to modify UserDao. xml and use this ing file.
Select u. id as u_id, u. username, u. age, u. address, g. id as g_id,
G. goodsName, g. goodsNumber, g. user_id fromuser uinner join goods g on u. id = g. user_id where u. id =$ {value };
I hope this will help you. If you have any incorrect wording, I hope you can correct it and make progress.