欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MyBatis的CRUD中的不同參數(shù)綁定查詢實現(xiàn)

 更新時間:2023年12月20日 11:23:13   作者:冰冰的偽善  
本文主要介紹了MyBatis的CRUD中的不同參數(shù)綁定查詢實現(xiàn),主要包括單個參數(shù)傳遞綁定,序號參數(shù)傳遞綁定,注解參數(shù)傳遞綁定,pojo(對象)參數(shù)傳遞綁定,map參數(shù)傳遞綁定這幾種類型,具有一定的參考價值,感興趣的可以了解一下

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)文章

最新評論