MyBatis的CRUD中的不同参数绑定查询实现
更新时间:2023年12月20日 11:23:13 作者:冰冰的伪善
本文主要介绍了MyBatis的CRUD中的不同参数绑定查询实现,主要包括单个参数传递绑定,序号参数传递绑定,注解参数传递绑定,pojo(对象)参数传递绑定,map参数传递绑定这几种类型,具有一定的参考价值,感兴趣的可以了解一下
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); }
到此这篇关于MyBatis的CRUD中的不同参数绑定查询实现的文章就介绍到这了,更多相关MyBatis CRUD参数绑定查询内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
相关文章
利用IDEA社区版创建SpringBoot项目的详细图文教程
大家应该都知道Idea社区版本,默认是不能创建SpringBoot项目的,下面这篇文章主要给大家介绍了关于利用IDEA社区版创建SpringBoot项目的详细图文教程,文中通过图文介绍的非常详细,需要的朋友可以参考下2023-04-04
最新评论