mapper.xml 映射文件是 MyBatis 的核心,定义了操作数据库的 sql,每个sql 是一个statement。

  1. parameterType(输入类型),输入类型包括:基本类型、pojo对象类型、hashmap、
    a. #{} 与 ${}
    注意:#{} 中的参数名 通常和 mapper 接口的形参名称相同,也可以设置成任意值。
       ${} 与 #{} 不同,${} 是将参数值不加修饰的拼接在 sql 中,相当于 JDBC 的 statement 拼接sql,不能防止sql注入。但是有时候会使用比较方法:如下:
<!-- 根据用户名查询用户信息 -->
<select id="findUserByUsername" parameterType="string" resultType="user">
	select * from users where username like '%${value}%'
</select>
	
<!-- 综合查询用户信息 -->
<select id="findUserList" parameterType="user" resultType="user">
	select * from users where username like '%${username}%' and sex = #{sex}
</select>

注意:如果 parameterType 是基本类型,那么${变量名} 中变量名必须是value;如果是 pojo 对象类型,那么变量名是 pojo 对象的属性名。

  1. 基本类型--不做介绍了
  2. pojo对象类型:上面代码第二个<select> 中 parameterType 就是 User 对象类型,可直接调用该对象的属性
public List<User> findUserList() throws Exception {
	SqlSession sqlSession = sqlSessionFactory.openSession();
	UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
		
	User user = new User();
	user.setUsername("Anna");
	user.setSex("0");
		
	List<User> users = usermapper.findUserList(user);
	System.out.println(users.size());
	Iterator<User> iterator = users.iterator();
	while (iterator.hasNext()) {
		User user2 = iterator.next(); 
		System.out.println(user2);
	}
	return users;
}
public List<User> findUserList() throws Exception {
	SqlSession sqlSession = sqlSessionFactory.openSession();
	UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
		
	User user = new User();
	user.setUsername("Anna");
	user.setSex("0");
		
	List<User> users = usermapper.findUserList(user);
	System.out.println(users.size());
	Iterator<User> iterator = users.iterator();
	while (iterator.hasNext()) {
		User user2 = iterator.next(); 
		System.out.println(user2);
	}
	return users;
}
  1. hashmap类型:代码中 HashMap 中的key 是需要硬编码的,所以 HashMap 用的不多。
<!-- 通过 hashmap 查询 -->
<select id="findUserListByHashmap" parameterType="hashmap" resultType="user">
	select * from users where username like '%${username}%' and sex = #{sex}
</select>
public List<User> findUserListByHashmap() throws Exception {
	SqlSession sqlSession = sqlSessionFactory.openSession();
	UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
		
	HashMap<String, Object> map = new HashMap<String, Object>();
	map.put("username", "Anna");
	map.put("sex", "0");
		
	List<User> users = usermapper.findUserListByHashmap(map);
	System.out.println(users.size());
	return users;
}
public List<User> findUserListByHashmap() throws Exception {
	SqlSession sqlSession = sqlSessionFactory.openSession();
	UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
		
	HashMap<String, Object> map = new HashMap<String, Object>();
	map.put("username", "Anna");
	map.put("sex", "0");
		
	List<User> users = usermapper.findUserListByHashmap(map);
	System.out.println(users.size());
	return users;
}
  1. resultType(输出类型):基本类型、pojo对象类型(单个)、pojo对象列表
  1. 基本类型--不多介绍
  2. pojo 对象类型(单个和列表),在 mapper.xml 中 resultType="User" 是一样的,区别是在写 Mapper 接口,如下:mybatis 会根据 Mapper 接口的返回类型 来决定去调用session.selectOne() 还是 session.selectList() 方法。返回单个pojo对象要保证sql查询出来的结果集为单条,使用session.selectOne方法调用,mapper接口使用pojo对象作为方法返回值。
public User findUserById(int userId) throws Exception;	
	
public List<User> findUserByUsername(String username) throws Exception;
public User findUserById(int userId) throws Exception;	
	
public List<User> findUserByUsername(String username) throws Exception;
  1. resultMap(输出类型): resultMap 也是一种输出类型,用于解决 当输出 pojo 对象的字段和 sql 查询出来的字段名不对应。 常用在一对一关联查询、一对多关联查询 等。
public class Person {
	private int id;
	private String name;
	private String addr;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + ", addr=" + addr + "]";
	}
}
public class Person {
	private int id;
	private String name;
	private String addr;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + ", addr=" + addr + "]";
	}
}
<!-- 定义resultMap -->
<resultMap type="person" id="resultMapPerson">
	<!-- 结果集的主键, property是Person pojo对象的属性名,userId是sql的列名 -->
	<id property="id" column="userId"/>
	<!-- 普通的列 -->
	<result property="name" column="username"/>
	<result property="addr" column="address"/>
</resultMap>
	
<!-- 综合查询用户信息,返回resultMap -->
<select id="findUserListRetrunResultMap" parameterType="user" resultMap="resultMapPerson">
	select * from users where username like '%${username}%' and sex = #{sex}
</select>
public void findUserListReturnResultMap() throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
	UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
		
	User user = new User();
	user.setUsername("Anna");
	user.setSex("0");
		
	List<Person> list = usermapper.findUserListRetrunResultMap(user);
	System.out.println(list.size());
	Iterator<Person> iterator = list.iterator();
	while (iterator.hasNext()) {
		Person person = iterator.next(); 
		System.out.println(person);
	}
}
public void findUserListReturnResultMap() throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
	UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
		
	User user = new User();
	user.setUsername("Anna");
	user.setSex("0");
		
	List<Person> list = usermapper.findUserListRetrunResultMap(user);
	System.out.println(list.size());
	Iterator<Person> iterator = list.iterator();
	while (iterator.hasNext()) {
		Person person = iterator.next(); 
		System.out.println(person);
	}
}