MyBatis 入門開發 —-輸入,輸出參數
阿新 • • 發佈:2018-05-15
MyBatis 入門開發第一: parameterType傳入參數傳遞簡單數據類型
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列表
@Test
public void dimFind() {
SqlSession session = MyBatisUtil.getSession();
List list = session.selectList("test1.findUserByUsername", "常山");
System.out.println(list);
}
輸出hashmap
@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=張小明}]
resultMap
resultMap 替代 resultType.
```
resultType可以指定pojo將查詢結果映射為pojo,但需要pojo的屬性名和sql查詢的列名一致方可映射成功。
如果sql查詢字段名和pojo的屬性名不一致,可以通過resultMap將字段名和屬性名作一個對應關系 ,resultMap實質上還需要將查詢結果映射到pojo對象中。
resultMap可以實現將查詢結果映射為復雜類型的pojo,比如在查詢結果映射對象中包括pojo和list實現一對一查詢和一對多查詢。
```
--------------------------------
@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();
}
<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(輸出類型)輸出簡單類型
Public 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(); } 輸出簡單類型MyBatis 入門開發 —-輸入,輸出參數