users表
+–+–+–+
| user_id | user_name | user_sex |
+–+–+–+
| 1 | 李雯静 | 女 |
| 3 | 刘诗诗 | 女 |
+–+–+–+
phones表
+–+–+–+
| phone_id | phone | user_id |
+–+–+–+
| 1 | 13629715184 | 1 |
| 3 | 15340532900 | 3 |
+–+–+–+
测试正常:phone_id=1而不是0,下面会一直为0,好烦。
+–+–+–+–+–+
| phone_id | phone | user_id | user_name | user_sex |
+–+–+–+–+–+
| 1 | 13629715184 | 1 | 李雯静 | 女 |
+–+–+–+–+–+
+–+–+–+
| user_id | user_name | user_sex |
+–+–+–+
| 1 | 李雯静 | 女 |
| 3 | 刘诗诗 | 女 |
+–+–+–+
phones表
+–+–+–+
| phone_id | phone | user_id |
+–+–+–+
| 1 | 13629715184 | 1 |
| 3 | 15340532900 | 3 |
+–+–+–+
测试正常:phone_id=1而不是0,下面会一直为0,好烦。
+–+–+–+–+–+
| phone_id | phone | user_id | user_name | user_sex |
+–+–+–+–+–+
| 1 | 13629715184 | 1 | 李雯静 | 女 |
+–+–+–+–+–+
/* * 一个电话号码对应一个User,但一个Use可以r对应多个电话号码。这里只考虑一对一单向关联。 */ public class Phone { private int phone_id; private String phone; private User user; public int getPhone_id() { return phone_id; } public void setPhone_id(int phone_id) { this.phone_id = phone_id; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String toString(){ return "{phone : phone_id="+this.phone_id+",phone="+this.phone+", [id="+this.user.getId()+",username="+this.user.getUsername()+",sex="+this.user.getSex()+"]}"; } }
<!-- PhoneMapper.xml --> <mapper namespace="com.dk.mybatis.mapper.PhoneMapper"> <resultMap type="Phone" id="phoneResultMap"> <association property="user" javaType="User"> <!-- Phone中的确关联了一个user属性 --> <id property="id" column="user_id" /> <result property="username" column="user_name"/> <result property="sex" column="user_sex"/> </association> </resultMap> <select id="getPhone" parameterType="int" resultMap="phoneResultMap"> select p.phone_id, p.phone, u.user_id, u.user_name, u.user_sex from phones as p, users as u where p.user_id=u.user_id and p.phone_id=#{phone_id} </select> </mapper>
//单元测试 public class Test4 { @Test public void testQuery() { SqlSession session=MyBatisUtils.getSqlSession(); String statement = "com.dk.mybatis.mapper.PhoneMapper.getPhone"; Phone phone = session.selectOne(statement, 1); Phone phone2 = session.selectOne(statement, 3); session.close(); System.out.println(phone); //{phone : phone_id=0,phone=null, [id=1,username=刘诗诗,sex=女]} System.out.println(phone2); //{phone : phone_id=0,phone=null, [id=3,username=李雯静,sex=女]} /* * 为什么查询结果中phone_id总是等于0啊啊啊啊啊啊啊······ * */ } }
解决方案
10
有意思, resultMapper里面P
hone的栏位都没加上你还想得到它。换成这样试一次吧。
<!-- PhoneMapper.xml --> <mapper namespace="com.dk.mybatis.mapper.PhoneMapper"> <id property="id" column="phone_id" /> <result property="phone" column="phone"/> <resultMap type="Phone" id="phoneResultMap"> <association property="user" javaType="User"> <!-- Phone中的确关联了一个user属性 --> <id property="id" column="user_id" /> <result property="username" column="user_name"/> <result property="sex" column="user_sex"/> </association> </resultMap> <select id="getPhone" parameterType="int" resultMap="phoneResultMap"> select p.phone_id, p.phone, u.user_id, u.user_name, u.user_sex from phones as p, users as u where p.user_id=u.user_id and p.phone_id=#{phone_id} </select> </mapper>
10