MyBatis的CRUD中的不同參數(shù)綁定查詢實(shí)現(xiàn)
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);
//創(chuàng)建SessionFactory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//使用數(shù)據(jù)的會(huì)話實(shí)例
sqlSession = sessionFactory.openSession();
}
?
@After
public void close() throws IOException {
sqlSession.close();
inputStream.close();
}
?單個(gè)參數(shù)傳遞綁定
在UserDao接口中:
User getUserById(Integer id);
在UserDao.xml中:
<!-- 傳遞單個(gè)參數(shù)-->
<select id="getUserById" parameterType="java.lang.Integer" resultType="com.by.pojo.User">
select * from user where id=#{id};
</select>測試類:
@Test
//傳遞單個(gè)參數(shù)有
public void testgetUserById() throws IOException {
//返回接口的代理類
UserDao userDao = sqlSession.getMapper(UserDao.class);
User user = userDao.getUserById(43);
System.out.println(user);
}序號(hào)參數(shù)傳遞綁定
在UserDao接口中:
//序號(hào)多個(gè)參數(shù)
User getUser(Integer id, String username);在UserDao.xml中:
<!-- 序號(hào)傳遞多個(gè)參數(shù)-->
<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
//序號(hào)傳遞多個(gè)參數(shù)
public void testgetUser() throws IOException {
//返回接口的代理類
UserDao userDao = sqlSession.getMapper(UserDao.class);
User user = userDao.getUser(43, "俞蓮舟");
System.out.println(user);
}注解參數(shù)傳遞綁定
在UserDao接口中:
//注解多個(gè)參數(shù)
User getUser2(@Param("id") Integer id, @Param("username") String username);在UserDao.xml中:
<!-- 注解傳遞多個(gè)參數(shù)-->
<select id="getUser2" resultType="com.by.pojo.User">
select * from user where id=#{id} and username=#{username};
</select>測試類:
@Test
//注解傳遞多個(gè)參數(shù)
public void testgetUser2() throws IOException {
//返回接口的代理類
UserDao userDao = sqlSession.getMapper(UserDao.class);
User user = userDao.getUser2(43, "俞蓮舟");
System.out.println(user);
}pojo(對(duì)象)參數(shù)傳遞綁定
在UserDao接口中:
//pojo參數(shù)
User getUser3(User user);在UserDao.xml中:
<!-- pojo傳遞多個(gè)參數(shù)-->
<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(對(duì)象)傳遞多個(gè)參數(shù)
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參數(shù)傳遞綁定
在UserDao接口中:
//map參數(shù)
User getUser4(Map<String, Object> map);在UserDao.xml中:
</select>
<!-- map傳遞多個(gè)參數(shù)-->
<select id="getUser4" parameterType="java.util.Map" resultType="com.by.pojo.User">
select * from user where id=#{id} and username=#{username};
</select>測試類:
@Test
//map傳遞多個(gè)參數(shù)
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);
}到此這篇關(guān)于MyBatis的CRUD中的不同參數(shù)綁定查詢實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)MyBatis CRUD參數(shù)綁定查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringSecurity中的UserDetails和UserDetailsService接口詳解
這篇文章主要介紹了SpringSecurity中的UserDetails和UserDetailsService接口詳解,UserDetailsService 在 Spring Security 中主要承擔(dān)查詢系統(tǒng)內(nèi)用戶、驗(yàn)證密碼、封裝用戶信息和角色權(quán)限,需要的朋友可以參考下2023-11-11
Java的四種常見線程池及Scheduled定時(shí)線程池實(shí)現(xiàn)詳解
這篇文章主要介紹了Java的四種常見線程池及Scheduled定時(shí)線程池實(shí)現(xiàn)詳解,在Java中,我們可以通過Executors類來創(chuàng)建ScheduledThreadPool,Executors類提供了幾個(gè)靜態(tài)方法來創(chuàng)建不同類型的線程池,包括ScheduledThreadPool,需要的朋友可以參考下2023-09-09
Java中Map與JSON數(shù)據(jù)之間的互相轉(zhuǎn)化
我們?cè)陂_發(fā)中難免和JSON打交道,這不小編最近遇到了,需要把一些信息轉(zhuǎn)成JSON字符串,下面這篇文章主要給大家介紹了關(guān)于Java中Map與JSON數(shù)據(jù)之間的互相轉(zhuǎn)化,需要的朋友可以參考下2023-04-04
SpringBoot項(xiàng)目找不到接口報(bào)404錯(cuò)誤的解決辦法
寫了一個(gè)簡單的springboot項(xiàng)目,在啟動(dòng)的時(shí)候idea未報(bào)錯(cuò),瀏覽器訪問接口時(shí)報(bào)404的錯(cuò)誤,所以本文給大家介紹了SpringBoot項(xiàng)目找不到接口報(bào)404錯(cuò)誤的解決辦法,文中有相關(guān)的圖文供大家參考,需要的朋友可以參考下2024-12-12
postman中實(shí)現(xiàn)傳遞@RequestBody參數(shù)
這篇文章主要介紹了postman中實(shí)現(xiàn)傳遞@RequestBody參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10

