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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}
测试类
private SqlSession sqlSession;
private InputStream inputStream;
@Before
public void init() throws IOException {
//加载配置文件
String resource = "mybatis-config.xml";
inputStream = Resources.getResourceAsStream(resource);
//创建SessionFactory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//使用数据的会话实例
sqlSession = sessionFactory.openSession();
}
@After
public void close() throws IOException {
sqlSession.close();
inputStream.close();
}
单个参数传递绑定
在UserDao接口中:
User getUserById(Integer id);
在UserDao.xml中:
测试类:
@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中:
测试类:
@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中:
测试类:
@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中:
测试类:
@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 map);
在UserDao.xml中:
测试类:
@Test
//map传递多个参数
public void testgetUser4() throws IOException {
//返回接口的代理类
UserDao userDao = sqlSession.getMapper(UserDao.class);
HashMap map = new HashMap<>();
map.put("id", 43);
map.put("username", "俞莲舟");
User user = userDao.getUser4(map);
System.out.println(user);
}