Mybatis关联查询
2017-09-04 12:56
155 查看
实体类
config配置文件
mapper配置文件
Detail接口
Orders接口
测试类
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关联查询
- mybatis的表关联查询,多个参数关联查询
- mybatis 关联查询 含有集合的嵌套查询 并传多个参数
- MyBatis关联查询
- MyBatis与mysql内接关联查询
- Mybatis关联查询(嵌套查询)
- 关联查询---Mybatis学习笔记(九)
- mybatis关联查询问题(一对多、多对一)
- mybatis 关联查询 一对一 一对多
- 【SSM-MyBatis框架】关联查询--多对多查询
- Mybatis的关联查询
- MyBatis 实践 -动态SQL/关联查询
- Mybatis下的数据库表自关联查询
- Spring+SpringMVC+MyBatis深入学习及搭建(六)——MyBatis关联查询
- SSM框架day02-MyBatis——042——关联查询-one2many-多表连接查询
- MyBatis:关联查询——多对一查询(详解association)
- mybatis 关联查询 Collection定义关联集合封装规则
- mybatis基于注解的关联查询
- MyBatis关联查询 (association) 时遇到的某些问题/mybatis映射
- mybatis 使用接口增删改查和两表一对一关联查询