一、创建数据库 SQL
CREATE TABLE user (id INT, username VARCHAR(20), phone VARCHAR(20));
CREATE TABLE orderinfo (id INT, uid INT, goods VARCHAR(20), info VARCHAR(50));
二、创建与数据库表对应的实体类
public class User implements Serializable {
private int id;
private String username;
private String phone;
...
}
public class OrderInfo implements Serializable {
private int id;
private int uid;
private String goods;
private String info;
private User user;
...
}
三、持久层接口
List<OrderInfo> findAll();
四、持久层映射文件
<resultMap id="orderInfoMap" type="chu.yi.bo.domain.OrderInfo">
<id column="id" property="id"/>
<result column="uid" property="uid"/>
<result column="goods" property="goods"/>
<result column="info" property="info"/>
<association property="user" javaType="chu.yi.bo.domain.User">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="phone" property="phone"/>
</association>
</resultMap>
<select id="findAll" resultMap="orderInfoMap">
select o.*,u.* from orderinfo o,user u where o.uid = u.id;
</select>
五、测试
List<OrderInfo> orderInfoList = orderInfoDao.findAll();
for(OrderInfo orderInfo : orderInfoList) {
System.out.println(orderInfo);
}
来源:CSDN
作者:萝卜缨子
链接:https://blog.csdn.net/u010723080/article/details/103844761