兩張表:user、t_orderjava
CREATE TABLE `user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主鍵', `name` varchar(30) DEFAULT NULL, `sex` int(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8;
CREATE TABLE `t_order` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主鍵', `user_id` bigint(20) NOT NULL, `order_money` decimal(20,3) NOT NULL COMMENT '金額', `order_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
須要查詢用戶的信息以及最近2筆訂單:sql
public class UserOrdersDTO { private Long id; private String userName; private List<Order> orderList; }
1)代碼流程式:首先查用戶信息、而後查該用戶最近2筆訂單mybatis
public UserOrdersDTO qryUserOrders(Long userId){ UserOrdersDTO userOrdersDTO = new UserOrdersDTO(); //步驟一:查詢用戶信息 User user = userMapper.selectByPrimaryKey(userId); if(user == null){ return userOrdersDTO; } userOrdersDTO.setId(userId); userOrdersDTO.setUserName(user.getName()); //步驟二:查詢用戶最近2個訂單 PageHelper.startPage(1, 2); Condition condition = new Condition(Order.class); condition.createCriteria().andEqualTo("userId",userId); condition.setOrderByClause("order_time desc"); List<Order> orderList = orderMapper.selectByCondition(condition); userOrdersDTO.setOrderList(orderList); return userOrdersDTO; }
針對單表操做,使用公共Mapper方法便可完成,無需再寫DAO代碼。app
2)註解式:dom
public interface UserMapper extends Mapper<User> { @Select("select id ,name from user where id = #{userId}") @Results({ @Result(column = "id",property = "id"), @Result(column = "name",property = "userName"), @Result(column = "id",property = "orderList", many = @Many(select = "com.ss.quickStart.dao.OrderMapper.qryOrdersByUserIdLastTwo")) }) UserOrdersDTO qryUserOrders2(@Param("userId") Long userId); }
public interface OrderMapper extends Mapper<Order> { @Select("SELECT id,user_id,order_money,order_time FROM t_order WHERE user_id = #{userId} ORDER BY order_time DESC limit 2") @ResultMap("BaseResultMap") List<Order> qryOrdersByUserIdLastTwo(@Param("userId") String userId); }
更多註解方式使用ui
3)xml式:我的以爲若是是動態查詢建議使用該方式lua
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.ss.quickStart.dao.UserMapper"> <resultMap id="BaseResultMap" type="com.ss.quickStart.domain.User"> <!-- WARNING - @mbg.generated --> <id column="id" jdbcType="BIGINT" property="id" /> <result column="name" jdbcType="VARCHAR" property="name" /> <result column="sex" jdbcType="INTEGER" property="sex" /> </resultMap> <resultMap id="userOrdersMap" type="com.ss.quickStart.domain.dto.UserOrdersDTO"> <id column="id" property="id" /> <result column="userName" property="userName" /> <collection property="orderList" column="id" resultMap="orderMap" /> </resultMap> <resultMap id="orderMap" type="com.ss.quickStart.domain.Order"> <result column="orderId" property="id" /> <result column="userId" property="userId" /> <result column="orderMoney" property="orderMoney" /> <result column="orderTime" property="orderTime" /> </resultMap> <select id="qryUserOrders3" resultMap="userOrdersMap"> select t.id, name userName, t1.id orderId, t1.user_id userId, t1.order_money orderMoney, t1.order_time orderTime from user t left join t_order t1 on t.id=t1.user_id where t.id=#{userId} order by order_time desc limit 2 </select> </mapper>