com.by.pojo下的User类
package com.by.pojo;import java.io.Serializable;
import java.util.Date;public class User implements Serializable {private Integer id;private String username;private Date birthday;private String sex;private String address;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "User{" +"id=" + id +", username='" + username + '\'' +", birthday=" + birthday +", sex='" + sex + '\'' +", address='" + address + '\'' +'}';}
}
测试类
private SqlSession sqlSession;private InputStream inputStream;@Beforepublic void init() throws IOException {//加载配置文件String resource = "mybatis-config.xml";inputStream = Resources.getResourceAsStream(resource);//创建SessionFactorySqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//使用数据的会话实例sqlSession = sessionFactory.openSession();}
@Afterpublic void close() throws IOException {sqlSession.close();inputStream.close();}
单个参数传递绑定
在UserDao接口中:
User getUserById(Integer id);
在UserDao.xml中:
<!-- 传递单个参数--><select id="getUserById" parameterType="java.lang.Integer" resultType="com.by.pojo.User">select * from user where id=#{id};</select>
测试类:
@Test//传递单个参数有public void testgetUserById() throws IOException {//返回接口的代理类UserDao userDao = sqlSession.getMapper(UserDao.class);User user = userDao.getUserById(43);System.out.println(user);}
序号参数传递绑定
在UserDao接口中:
//序号多个参数User getUser(Integer id, String username);
在UserDao.xml中:
<!-- 序号传递多个参数--><select id="getUser" resultType="com.by.pojo.User">select * from user where id=#{arg0} and username=#{arg1};select * from user where id=#{param1} and username=#{param2};</select>
测试类:
@Test//序号传递多个参数public void testgetUser() throws IOException {//返回接口的代理类UserDao userDao = sqlSession.getMapper(UserDao.class);User user = userDao.getUser(43, "俞莲舟");System.out.println(user);}
注解参数传递绑定
在UserDao接口中:
//注解多个参数User getUser2(@Param("id") Integer id, @Param("username") String username);
在UserDao.xml中:
<!-- 注解传递多个参数--><select id="getUser2" resultType="com.by.pojo.User">select * from user where id=#{id} and username=#{username};</select>
测试类:
@Test//注解传递多个参数public void testgetUser2() throws IOException {//返回接口的代理类UserDao userDao = sqlSession.getMapper(UserDao.class);User user = userDao.getUser2(43, "俞莲舟");System.out.println(user);}
pojo(对象)参数传递绑定
在UserDao接口中:
//pojo参数User getUser3(User user);
在UserDao.xml中:
<!-- pojo传递多个参数--><select id="getUser3" parameterType="com.by.pojo.User" resultType="com.by.pojo.User">select * from user where id=#{id} and username=#{username};</select>
测试类:
@Test//pojo(对象)传递多个参数public void testgetUser3() throws IOException {//返回接口的代理类UserDao userDao = sqlSession.getMapper(UserDao.class);User userParam = new User();userParam.setId(43);userParam.setUsername("俞莲舟");User user = userDao.getUser3(userParam);System.out.println(user);}
map参数传递绑定
在UserDao接口中:
//map参数User getUser4(Map<String, Object> map);
在UserDao.xml中:
</select><!-- map传递多个参数--><select id="getUser4" parameterType="java.util.Map" resultType="com.by.pojo.User">select * from user where id=#{id} and username=#{username};</select>
测试类:
@Test//map传递多个参数public void testgetUser4() throws IOException {//返回接口的代理类UserDao userDao = sqlSession.getMapper(UserDao.class);HashMap<String, Object> map = new HashMap<>();map.put("id", 43);map.put("username", "俞莲舟");User user = userDao.getUser4(map);System.out.println(user);}