标签:对象存储 conf custom config on() display work stc ret
SELECT orders.*, user.username, user.address FROM orders, user WHERE orders.user_id = user.id
package com.lhx.mybatis.po; import java.util.Date; public class Order { private Integer id; private String user_id; private Double number; private String note; private Date createtime; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUser_id() { return user_id; } public void setUser_id(String user_id) { this.user_id = user_id; } public Double getNumber() { return number; } public void setNumber(Double number) { this.number = number; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } public Date getCreatetime() { return createtime; } public void setCreatetime(Date createtime) { this.createtime = createtime; } }
package com.lhx.mybatis.po; public class OrdersCustom extends Order { private String username; private String address; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "OrdersCustom [username=" + username + ", address=" + address + ", getId()=" + getId() + ", getUser_id()=" + getUser_id() + ", getNumber()=" + getNumber() + ", getNote()=" + getNote() + ", getCreatetime()=" + getCreatetime() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]"; } }
OrdersCustom类继承Orders类后OrdersCustom类包括了Orders类的所有字段,只需要定义用户的信息字段即可。
<mapper namespace="com.lhx.mybatis.mapperproxy.OrderMapper"> <!-- 查询所有订单信息 --> <select id="findOrdersList" resultType="com.lhx.mybatis.po.OrdersCustom"> SELECT orders.*, user.username, user.address FROM orders, user WHERE orders.user_id = user.id </select> </mapper>
public List<OrdersCustom> findOrdersList() throws Exception;
package com.lhx.mybatis.mapperproxy; import java.io.InputStream; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.lhx.mybatis.po.OrdersCustom; import junit.framework.TestCase; public class OrderMapperTest extends TestCase { private SqlSessionFactory sqlSessionFactory; protected void setUp() throws Exception { // mybatis配置文件 String resource = "SqlMapConfig-MapperProxy.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); // 使用SqlSessionFactoryBuilder创建sessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } public void testfindOrdersList() throws Exception { // 获取session SqlSession session = sqlSessionFactory.openSession(); // 获限mapper接口实例 OrderMapper orderMapper = session.getMapper(OrderMapper.class); // 查询订单信息 List<OrdersCustom> list = orderMapper.findOrdersList(); System.out.println(list); for(int i=0,len = list.size();i<len;i++){ System.out.println(list.get(i)); } // 关闭session session.close(); } }
<select id="findOrdersListResultMap" resultMap="userordermap"> SELECT orders.*, user.username, user.address FROM orders, user WHERE orders.user_id = user.id </select>
这里resultMap指定userordermap。
<!-- 订单信息resultmap --> <resultMap type="com.lhx.mybatis.po.Order" id="userordermap"> <!-- 这里的id,是mybatis在进行一对一查询时将user字段映射为user对象时要使用,必须写 --> <id property="id" column="id" /> <result property="user_id" column="user_id" /> <result property="number" column="number" /> <association property="user" javaType="com.lhx.mybatis.po.User"> <!-- 这里的id为user的id,如果写上表示给user的id属性赋值 --> <id property="id" column="user_id" /> <result property="username" column="username" /> <result property="address" column="address" /> </association> </resultMap>
association:表示进行关联查询单条记录
List<Order> findOrdersListResultMap() throws Exception;
public void testfindOrdersListResultMap() throws Exception { // 获取session SqlSession session = sqlSessionFactory.openSession(); // 获限mapper接口实例 OrderMapper orderMapper = session.getMapper(OrderMapper.class); // 查询订单信息 List<Order> list = orderMapper.findOrdersListResultMap(); System.out.println(list); for (int i = 0, len = list.size(); i < len; i++) { System.out.println(list.get(i)); } // 关闭session session.close(); }
java-mybaits-00502-案例-映射分析-一对一、一对多、多对多
标签:对象存储 conf custom config on() display work stc ret
原文地址:http://www.cnblogs.com/bjlhx/p/6847667.html