MyBatis的CRUD中的不同参数绑定查询
2023-12-20 10:29:43
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中:
<!-- 传递单个参数-->
<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);
}
文章来源:https://blog.csdn.net/m0_74007708/article/details/135095156
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!