映射分为输出映射和出入映射
-
输入映射(参数映射)
- 简单类型:String和8种基本类型
- pojo类型:我们自己封装pojo类型
- 包装的pojo类型:pojo中有pojo的类型
- map类型:HashMap类型
简单类型和pojo类型在前文中已经使用很多次了,下面着重来讲一下包装的pojo类型和map类型的参数映射。
-
包装的pojo类型:单独写一个QueryVo类来包裹上文的User类;
package cn.zw.pojo; import java.util.Date; public class User { private int id; private String username;// 用户姓名 private String sex;// 性别 private Date birthday;// 生日 private String address;// 地址 public User() { super(); // TODO Auto-generated constructor stub } public User(int id, String username, String sex, Date birthday, String address) { super(); this.id = id; this.username = username; this.sex = sex; this.birthday = birthday; this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", birthday=" + birthday + ", address=" + address + "]"; } }
package cn.zw.pojo; public class QueryVo { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
<select id="selectByQueryVo" parameterType="cn.zw.pojo.QueryVo" resultType="cn.zw.pojo.User"> select * from user where id = #{user.id} </select>
输入参数的映射的格式要求为包装类的属性名.属性
-
Map的类型(与pojo类型相同)
把参数封装成map传给sql语句,占位表达式里面的值就是map中的key
在mapper的接口中添加一个方法
List<User> selectByMap(Map<String, String> map);
<select id="selectByMap" parameterType="java.util.Map" resultType="cn.zw.pojo.User"> SELECT * FROM `user` WHERE username LIKE #{username} AND address = #{address}; </select>
@Test public void fun02() throws IOException { SqlSession session = sqlSessionFactory.openSession(); UserMapper mapper = session.getMapper(UserMapper.class); Map<String,String> map = new HashMap<>(); map.put("username","%蟑螂%"); map.put("address","大阪"); List<User> users = mapper.selectByMap(map); System.out.println(users); }
-
输出映射(返回值映射)
- 简单数据类型:string和八种基本类型
- pojo类型:返回一行记录(前文使用过)
- list:返回多行记录(前文使用过)
- map:返回一行记录
- list<map>:返回多行记录
-
简单数据类型的演示
- mapper接口中添加方法;
int getCount();
- 映射xml文件中添加查询语句
<select id="getCount" resultType="int"> select count(*) from user </select>
- 执行映射语句获得结果
@Test public void fun03() throws IOException { SqlSession session = sqlSessionFactory.openSession(); UserMapper mapper = session.getMapper(UserMapper.class); System.out.println(mapper.getCount()); }
-
map类型的输出映射:当返回结果没有对应的pojo类时
- 模拟情况我们没有User类
Map getUserByIdMap(int id);
- 映射xml文件同添加查询语句
<select id="getUserByIdMap" parameterType="int" resultType="map"> select * from user where id = #{id} </select>
- 执行映射语句获得结果
@Test public void fun04() throws IOException { SqlSession session = sqlSessionFactory.openSession(); Map map = session.getMapper(UserMapper.class).getUserByIdMap(4); System.out.println(map); }
-
list<map>相当于list<User>
List<Map> getUserByIdList(int id);
<select id="getUserByIdList" parameterType="int" resultType="map"> select * from user where id > #{id} </select>
@Test public void fun05() throws IOException { SqlSession session = sqlSessionFactory.openSession(); List<Map> maps = session.getMapper(UserMapper.class).getUserByIdList(1); System.out.println(maps); }
现在有一个问题,那么有一天我们的参数映射或者输出映射中的属性名称与我们的参数映射中的参数名称和输出映射中的参数名称不相同怎么办呢?
我们就要用到parameterMap和resultMap
假设把User类中的username属性改成name,那么结果就数据库中的结果中的username就不能映射到name属性上了。