第一: parameterType传入参数传递简单数据类型
<select id="findUserById" parameterType="int" resultType="cn.fy.domain.User"> SELECT id,username,birthday,sex,address FROM USER WHERE id = #{id} </select>
传递pojo对象
<!—传递pojo对象综合查询用户信息 --> user 使用了别名. <select id="findUserByUser" parameterType="user" resultType="user"> select * from user where id=#{id} and username like '%${username}%' </select> #{id} : 使用 user中的id ${username}:使用user中的username. // 如果参数类型user 中有属性 order 类 <select id="findUserByUser" parameterType="user" resultType="user"> select * from user where id=#{order.id} and username like '%${username}%' </select>
mybatis可以通过ognl 表达式: #{属性.属性.属性}
传递hashmap
map综合查询用户信息 --> <select id="findUserByHashmap" parameterType="hashmap" resultType="user"> select * from user where id=#{id} and username like '%${username}%' </select> @Test public void findUserByHashmap() { SqlSession session = MyBatisUtil.getSession(); UserMapper userMapper = session.getMapper(UserMapper.class); //构造查询条件Hashmap对象 HashMap<String, Object> map = new HashMap<String, Object>(); map.put("id", 1); map.put("username", "张"); //传递Hashmap对象查询用户列表 List<User>list = userMapper.findUserByHashmap(map); //关闭session session.close(); } select * from user where id=? and username like '%管理员%'
第二: resultType(输出类型)输出简单类型
select count(1) from userPublic void testFindUserCount() throws Exception{ //获取session SqlSession session = sqlSessionFactory.openSession(); //获取mapper接口实例 UserMapper userMapper = session.getMapper(UserMapper.class); User user = new User(); user.setUsername("管理员"); //传递Hashmap对象查询用户列表 int count = userMapper.findUserCount(user); //关闭session session.close(); }输出简单类型
select * from user where id = #{id}Public void testFindUserById() throws Exception { //获取session SqlSession session = sqlSessionFactory.openSession(); //获限mapper接口实例 UserMapper userMapper = session.getMapper(UserMapper.class); //通过mapper接口调用statement User user = userMapper.findUserById(1); System.out.println(user); //关闭session session.close(); }输出pojo列表
select * from user where username like '%${value}%'@Test public void dimFind() { SqlSession session = MyBatisUtil.getSession(); List
list = session.selectList("test1.findUserByUsername", "常山"); System.out.println(list); }输出hashmapselect username,sex from user@Test public void findUserNameAndSex() { SqlSession session = MyBatisUtil.getSession(); UserMapper userMapper = session.getMapper(UserMapper.class); //构造查询条件Hashmap对象 // 传递Hashmap对象查询用户列表 // 注意不能返回HashMap,还是要返回list List list = userMapper.findUserNameAndSex(); System.out.println("--list-" + list); //关闭session session.close(); } --list-[{sex=2, username=王五}, {sex=1, username=张三}, {sex=1, username=张小明}]resultMapresultMap 替代 resultType.```resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功。如果sql查询字段名和pojo的属性名不一致,可以通过resultMap将字段名和属性名作一个对应关系 ,resultMap实质上还需要将查询结果映射到pojo对象中。resultMap可以实现将查询结果映射为复杂类型的pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询。```SELECT id,username,birthday,sex,address FROM USER WHERE id = #{id}-------------------------------- @Test public void findUserResultMap() throws Exception { SqlSession session = MyBatisUtil.getSession(); UserMapper userMapper = session.getMapper(UserMapper.class); //构造查询条件Hashmap对象 List list = userMapper.findUserResultMap(10); //--list-[User{id=10, username='张三', sex='1', birthday=Thu Jul 10 00:00:00 CST 2014, address='北京市'}] System.out.println("--list-" + list); //关闭session session.close(); }