企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
一个用户对应多个订单 在User.java这个类中添加这个 ~~~ // 附加对象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> ~~~ 测试 ~~~ // 加载核心配置文件 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()); } ~~~