🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
一个用户对应多个订单 **在User.java这个类中添加这个** **一个User对应多个Order** ~~~ // 附加对象List private List<Orders> ordersList; public List<Orders> getOrdersList() { return ordersList; } public void setOrdersList(List<Orders> ordersList) { this.ordersList = ordersList; } ~~~ 配置文件中写 ~~~ <!-- // 一对多关联 public List<User> selectUserList(); --> <resultMap type="pojo.User" id="user"> <id column="user_id" property="id" /> <result column="username" property="username" /> <!-- 一对多 --> <collection property="ordersList" ofType="pojo.Orders"> <id column="id" property="id" /> <result column="number" property="number" /> </collection> </resultMap> <select id="selectUserList" resultMap="user"> select o.id,o.user_id,o.number,o.createtime,u.username from user u left join orders o on o.user_id = u.id </select> ~~~ ~~~ <!-- collection是用于建立一对多中集合属性的对应关系 ofType 用于指定集合元素的数据类型 --> ~~~ 测试 ~~~ // 加载核心配置文件 String resource = "SqlMapConfig.xml"; InputStream in = Resources.getResourceAsStream(resource); // 创建SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); // 创建SqlSession SqlSession sqlSession = sqlSessionFactory.openSession(); // SqlSession帮我生成一个实现类 OrderMapper mapper = sqlSession.getMapper(OrderMapper.class); List<User> selectOrderList = mapper.selectUserList(); for (User users : selectOrderList) { System.out.println(users); System.out.println(users.getOrdersList()); } ~~~