MyBatis的CRUD中的不同參數(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ù)的會話實例
sqlSession = sessionFactory.openSession();
}
?
@After
public void close() throws IOException {
sqlSession.close();
inputStream.close();
}
?單個參數(shù)傳遞綁定
在UserDao接口中:
User getUserById(Integer id);
在UserDao.xml中:
<!-- 傳遞單個參數(shù)-->
<select id="getUserById" parameterType="java.lang.Integer" resultType="com.by.pojo.User">
select * from user where id=#{id};
</select>測試類:
@Test
//傳遞單個參數(shù)有
public void testgetUserById() throws IOException {
//返回接口的代理類
UserDao userDao = sqlSession.getMapper(UserDao.class);
User user = userDao.getUserById(43);
System.out.println(user);
}序號參數(shù)傳遞綁定
在UserDao接口中:
//序號多個參數(shù)
User getUser(Integer id, String username);在UserDao.xml中:
<!-- 序號傳遞多個參數(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
//序號傳遞多個參數(shù)
public void testgetUser() throws IOException {
//返回接口的代理類
UserDao userDao = sqlSession.getMapper(UserDao.class);
User user = userDao.getUser(43, "俞蓮舟");
System.out.println(user);
}注解參數(shù)傳遞綁定
在UserDao接口中:
//注解多個參數(shù)
User getUser2(@Param("id") Integer id, @Param("username") String username);在UserDao.xml中:
<!-- 注解傳遞多個參數(shù)-->
<select id="getUser2" resultType="com.by.pojo.User">
select * from user where id=#{id} and username=#{username};
</select>測試類:
@Test
//注解傳遞多個參數(shù)
public void testgetUser2() throws IOException {
//返回接口的代理類
UserDao userDao = sqlSession.getMapper(UserDao.class);
User user = userDao.getUser2(43, "俞蓮舟");
System.out.println(user);
}pojo(對象)參數(shù)傳遞綁定
在UserDao接口中:
//pojo參數(shù)
User getUser3(User user);在UserDao.xml中:
<!-- pojo傳遞多個參數(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(對象)傳遞多個參數(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傳遞多個參數(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傳遞多個參數(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ù)綁定查詢實現(xiàn)的文章就介紹到這了,更多相關(guān)MyBatis CRUD參數(shù)綁定查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)示例詳解
這篇文章主要為大家介紹了SpringMVC域?qū)ο蠊蚕頂?shù)據(jù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Spring Cloud Alibaba Nacos Config加載配置詳解流
這篇文章主要介紹了Spring Cloud Alibaba Nacos Config配置中心實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-07-07
Java并發(fā)編程之關(guān)鍵字volatile的深入解析
提高java的并發(fā)編程,就不得不提volatile關(guān)鍵字,不管是在面試還是實際開發(fā)中volatile都是一個應(yīng)該掌握的技能,這篇文章主要給大家介紹了關(guān)于Java并發(fā)編程之關(guān)鍵字volatile的相關(guān)資料,需要的朋友可以參考下2021-09-09
SprinBoot如何集成參數(shù)校驗Validator及參數(shù)校驗的高階技巧
這篇文章主要介紹了SprinBoot如何集成參數(shù)校驗Validator及參數(shù)校驗的高階技巧包括自定義校驗、分組校驗,本文分步驟給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
Java BeanMap實現(xiàn)Bean與Map的相互轉(zhuǎn)換
這篇文章主要介紹了利用BeanMap進(jìn)行對象與Map的相互轉(zhuǎn)換,通過net.sf.cglib.beans.BeanMap類中的方法來轉(zhuǎn)換,效率極高,本文給大家分享實現(xiàn)代碼,感興趣的朋友一起看看吧2022-11-11

