您的位置:首页 > 其它

Mybatis关联查询

2017-09-04 12:56 155 查看
实体类

public class Detail {
private Integer id;//订单详情id
private Orders order;//订单
private Goods goods;//商品
private Integer gnum;//数量


public class Orders {
private Integer id;
private Users user;//用户
private String serial;
private Date createtime;
private String note;
private List<Detail> list;//订单详情


public class Goods {
private Integer id;
private String gname;
private Double price;
private String detail;
private String pic;
private Date createtime;


public class Users {
private Integer id;
private String username;
private Timestamp birthday;
private String sex;
private String address;


config配置文件

<configuration>
<!-- 1)引入 jdbc.properties 文件 ,包含数据库链接信息-->
<properties resource="jdbc.properties" />

<!-- 实体类别名 -->
<typeAliases>
<!-- 包 -->
<package name="com.csl.entity" />
</typeAliases>
<!--2) 环境配置 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />  <!-- 事务 -->
<dataSource type="POOLED">  <!-- 数据源 -->
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>

<!--3) 映射的配置文件 -->
<mappers>
<mapper resource="com/csl/dao/UsersMapper.xml" />
<mapper resource="com/csl/dao/DetailMapper.xml"/>
<mapper resource="com/csl/dao/OrdersMapper.xml"/>
</mappers>

</configuration>


mapper配置文件

Detail类
<mapper namespace="com.csl.dao.DetailMapper">

<select id="findById" resultMap="map_1" parameterType="int">
select * from detail d,goods g,orders o
where d.gid = g.id and d.oid = o.id and d.id = #{id}
</select>

<resultMap type="Detail" id="map_1">
<id property="id" column="id"/>
<result property="gnum" column="gnum"/>
<association property="order" resultMap="map_2"/>
<association property="goods" resultMap="map_3"/>
</resultMap>

<resultMap type="Orders" id="map_2">
<id property="id" column="id"/>
<!--    <associations property="user" javaType="Users"/> -->
<result property="serial" column="serial"/>
<result property="createtime" column="createtime"/>
<result property="note" column="note"/>
</resultMap>

<resultMap type="Goods" id="map_3">
<id property="id" column="id"/>
<result property="gname" column="gname"/>
<result property="price" column="price"/>
<result property="detail" column="detail"/>
<result property="pic" column="pic"/>
<result property="createtime" column="createtime"/>
</resultMap>

</mapper>


Detail接口

public interface DetailMapper {

/**
* 根据id获取对象信息
* @param id
* @return
*/
Detail findById(Integer id);
}


Orders类
<mapper namespace="com.csl.dao.OrdersMapper">

<select id="findById" parameterType="int" resultMap="map_1">
select * from orders o ,users u ,detail d where o.user_id = u.id and o.id = #{id}
</select>

<resultMap id="map_1" type="Orders">
<id property="id" column="id"/>
<result property="serial" column="serial"/>
<result property="createtime" column="createtime"/>
<result property="note" column="note"/>
<association property="user" resultMap="userMap"/>
<collection property="list" column="id" select="detailSelect"/>
</resultMap>

<resultMap id="userMap" type="Users">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="sex" column="sex"/>
<result property="birthday" column="birthday"/>
<result property="address" column="address"/>
</resultMap>

<select id="detailSelect" parameterType="int" resultType="Detail">
select * from detail where oid = #{id}
</select>
</mapper>


Orders接口

public interface OrdersMapper {
Orders findById(Integer id);
}


测试类

public class Tests_2 {
/**
* 会话和用户映射器
*/
private SqlSession session = MybatisUtil.getSession(true);
private DetailMapper dm = session.getMapper(DetailMapper.class);

/**
* 根据id查找
*/
@Test
public void show(){

Detail d = dm.findById(1);
System.out.println(d);
}
}


public class Tests_3 {
/**
* 会话和用户映射器
*/
private SqlSession session = MybatisUtil.getSession(true);
private OrdersMapper  om = session.getMapper(OrdersMapper.class);

@Test
public void show(){
Orders o = om.findById(1);
System.out.println(o.getList());
System.out.println(o.getUser());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mybatis 关联查询