标签:
数据库设计,部分源文件请参照resulttype那篇随笔,不再次累赘。
创建Orders.java把User.java作为属性加入
import java.util.Date; import cn.zqq.bean.User; public class Orders{ private String number; private Date craetetime; private String note; private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public Date getCraetetime() { return craetetime; } public void setCraetetime(Date craetetime) { this.craetetime = craetetime; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } @Override public String toString() { return "Orders [number=" + number + ", craetetime=" + craetetime + ", note=" + note + ", user=" + user + "]"; } }
mapper.java文件如下
import java.util.List; public interface UserCustomOrderMap { public List<Orders> findAllInfoUserOrdersMap()throws Exception; }
对应的UserCustomOrderMa.xml
<?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="cn.zqq.one2one.resultmap.UserCustomOrderMap"> <resultMap type="cn.zqq.one2one.resultmap.Orders" id="ordersMap"> <result column="number" property="number"/> <result column="createtime" property="craetetime"/> <result column="note" property="note"/> <!-- 复杂类型的属性,引入相关类型并配置 --> <association javaType="cn.zqq.bean.User" property="user"> <id column="id" property="id"/> <result column="username" property="username"/> <result column="birthday" property="birthday"/> <result column="sex" property="sex"/> <result column="address" property="address"/> </association> </resultMap> <select id="findAllInfoUserOrdersMap" resultMap="ordersMap"> SELECT * FROM USER,orders WHERE user.id=orders.user_id; </select> </mapper>
测试代码
SqlSession sqlSession = null; @Before public void beforesourse() throws IOException { String resource = "SqlMapConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); sqlSession = sqlSessionFactory.openSession(); }
@Test public void findAllInfoUserOrders() throws Exception { UserCustomOrderMap order = sqlSession.getMapper(UserCustomOrderMap.class); List<Orders> userOrders = order.findAllInfoUserOrdersMap(); for (Orders userCustom : userOrders) { System.out.println(userCustom); } }
对于SqlMapConfig.xml添加对应的包的扫描器即可
标签:
原文地址:http://www.cnblogs.com/zqq1234/p/5249212.html