1、这个是配置文件
<resultMap type="User" id="userResultMap">
<!-- 属性名和数据库列名映射 -->
<id property="id" column="id" />
<result property="userName" column="userName" />
<result property="userPwd" column="userPwd" />
</resultMap>
<resultMap type="Article" id="articleResultMap" >
<id property="id" column="id" />
<result property="title" column="title" />
<result property="content" column="content" />
<!-- 将article的user属性映射到userResultMap -->
<association property="user" resultMap="userResultMap" />
</resultMap>
<!--查询-->
<select id="getUserArticles" parameterType="int" resultMap="articleResultMap" >
select user.id,user.userName,user.userPwd,article.title,article.content
from user,article where user.id = article.userId and user.id = #{id}
</select>
2、select user.id,user.userName,user.userPwd,article.title,article.content
from user,article where user.id = article.userId and user.id = 1 这条sql语句在mysql中查询是有5条记录的,但是在代码中就只有一条记录
3、public List getUserArticles(int id);
这个是接口
你的id是user表里面的主键吧,怎么查也不会有五条记录吧
这个是因为你返回的user表只有一条记录,在这条记录里有个articleResultMap中有个属性字段来保article表的数据看你写的是user属性(user.user)这样的情况下,他才是5条件记录
我写的例子:我刚才试了一下,和数据库查询一样的。
在UserMapper.xml中
<!-- resultMap
sql语句同resultType实现的sql
使用resultMap将查询结果中的订单信息映射到Orders对象中,
在orders类中添加User属性,将关联查询出来的用户信息映射到orders对象中的user属性中。
定义resultMap
订单关联查询关联用户的resultMap
将整个查询的结果映射到Orders类中
-->
<resultMap type="orders" id="ordersUserResultMap">
<!-- 配置映射的订单信息 -->
<!-- id:指定查询列中的唯一标识,订单信息的中的唯一标识,如果有多个列组成唯一标识,配置多个id
column:订单信息的唯一标识 列
property:订单信息的唯一标识 列 所映射到Orders中的哪个属性 -->
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="createtime" property="createtime"/>
<result column="note" property="note"/>
<!-- 配置映射的关联的用户信息 -->
<!-- association:用于映射关联查询单个对象的信息
property:要将关联查询的用户信息映射到Orders中哪个属性 -->
<association property="user" javaType="user">
<!-- id:关联查询用户的唯一标识
column:指定唯一标识用户信息的列
javaType:映射到user的哪个属性-->
<id column="user_id" property="id" />
<result column="username" property="username" />
<result column="sex" property="sex"/>
<result column="address" property="address"/>
</association>
</resultMap>
<!-- 测试一对多查询 -->
<!-- 查询订单关联查询用户信息,使用resultMap -->
<select id="findOrdersUserResultMapById" parameterType="int" resultMap="ordersUserResultMap">
select
orders.*,
user.username,
user.sex,
user.address
from
orders,
user
where orders.user_id = user.id
and user.id=#{id}
</select>
在UserMapper接口中添加
//测试用
public List<Orders> findOrdersUserResultMapById(int id) throws Exception;
执行单元测试
@Test
public void findOrdersUserResultMapById() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
userMapper = sqlSession.getMapper(UserMapper.class);
//调用userMapper的方法
List<Orders> list = userMapper.findOrdersUserResultMapById(1);
System.out.println(list);
}
控台打印
DEBUG [main] - ==> Preparing: select orders.*, user.username, user.sex, user.address from orders, user where orders.user_id = user.id and user.id=?
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <== Total: 2
[Orders [id=3, userId=1, number=null, createtime=Wed Feb 04 13:22:35 CST 2015, note=null, user=User [id=1, username=王五, sex=2, birthday=null, address=null, orders=[]], orderdetails=[]], Orders [id=4, userId=1, number=null, createtime=Tue Feb 03 13:22:41 CST 2015, note=null, user=User [id=1, username=王五, sex=2, birthday=null, address=null, orders=[]], orderdetails=[]]]
去数据库执行
select orders.*, user.username, user.sex, user.address from orders, user where orders.user_id = user.id and user.id=1

<!-- 将article的user属性映射到userResultMap -->
//articleList这个要加到user实体类中当一个字段,不过最好写在扩展类中
这样的结果集中就有你想要的article的数据articleList了

http://blog.csdn.net/evankaka/article/details/45674101
以user为主表只有1条记录
select user.id,user.userName,user.userPwd,article.title,article.content
from user,article where user.id = article.userId and user.id = #{id}
这个查询里面没有查 article.id
改成这样select article.id,article.title,article.content, user.id,user.userName,user.userAddress from user,article where user.id=article.userid and user.id=#{id}
其实就是把article表的字段写在前面,这样就article为主了