开发者

MyBatis的CRUD中的不同参数绑定查询实现

开发者 https://www.devze.com 2023-12-21 10:39 出处:网络 作者: 冰冰的伪善
目录单个参数传递绑定序号参数传递绑定注解参数传递绑定pojo(对象)参数传递绑定map参数传递绑定com.by.pojo下的User类
目录
  • 单个参数传递绑定
  • 序号参数传递绑定
  • 注解参数传递绑定
  • pojo(对象)参数传递绑定
  • map参数传递绑定

com.by.pojo下的User类

packagjse 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() {
        re编程客栈turn "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>

测试类:

@Tes编程客栈t
    //注解传递多个参数
    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中:

<!--python    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参数绑定查询内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)! 

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号