MyBatis的CRUD中的不同參數(shù)綁定查詢(xún)實(shí)現(xiàn)
com.by.pojo下的User類(lèi)
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 + '\'' +
'}';
}
}測(cè)試類(lèi)
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ì)話(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>測(cè)試類(lèi):
@Test
//傳遞單個(gè)參數(shù)有
public void testgetUserById() throws IOException {
//返回接口的代理類(lèi)
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>測(cè)試類(lèi):
@Test
//序號(hào)傳遞多個(gè)參數(shù)
public void testgetUser() throws IOException {
//返回接口的代理類(lèi)
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>測(cè)試類(lèi):
@Test
//注解傳遞多個(gè)參數(shù)
public void testgetUser2() throws IOException {
//返回接口的代理類(lèi)
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>測(cè)試類(lèi):
@Test
//pojo(對(duì)象)傳遞多個(gè)參數(shù)
public void testgetUser3() throws IOException {
//返回接口的代理類(lèi)
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>測(cè)試類(lèi):
@Test
//map傳遞多個(gè)參數(shù)
public void testgetUser4() throws IOException {
//返回接口的代理類(lèi)
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ù)綁定查詢(xún)實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)MyBatis CRUD參數(shù)綁定查詢(xún)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java基于websocket協(xié)議與netty實(shí)時(shí)視頻彈幕交互實(shí)現(xiàn)
本文主要介紹了Java基于websocket協(xié)議與netty實(shí)時(shí)視頻彈幕交互實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
eclipse/intellij idea 遠(yuǎn)程調(diào)試hadoop 2.6.0
Spring監(jiān)聽(tīng)器及定時(shí)任務(wù)實(shí)現(xiàn)方法詳解
Java各種鎖在工作中使用場(chǎng)景和細(xì)節(jié)經(jīng)驗(yàn)總結(jié)
Java實(shí)現(xiàn)深度優(yōu)先搜索(DFS)和廣度優(yōu)先搜索(BFS)算法
Java中字符串與byte數(shù)組之間的相互轉(zhuǎn)換
Java類(lèi)的定義以及執(zhí)行順序?qū)W習(xí)教程

