mybatis的根据各种参数类型进行查询

书写一个实体类

package com.yunhe.entity;

import java.io.Serializable;
import java.sql.Date;

/**
 * @author hou
 * @date 2021/12/15
 */
public class User implements Serializable {


    private int id;
    private String name;
    private double sal;
    private Date birthday;

    public User(int id, String name, double sal) {
        this.id = id;
        this.name = name;
        this.sal = sal;
    }

    public User() {
    }

    public User(String name, double sal) {
        this.name = name;
        this.sal = sal;
    }

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public User(int id, String name, double sal, Date birthday) {
        this.id = id;
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }
}

 编写一个mapper接口,根据各种参数类型进行查找

package mapper;

import com.yunhe.entity.User;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author hou
 * @date 2021/12/15
 */
public interface UserMapper {
    //查询所有的数据
    ArrayList<User> selectAll();
    //根据id查询
    ArrayList<User> selectById(int id);
    //根据id,name查询
    ArrayList<User> selectByIdAndName(int id,String name);
    //根据list集合查询
    ArrayList<User> selectList(ArrayList<Integer> list);
    //根据数组查询
    ArrayList<User> selectArry(int[] arr);
    //根据map集合查询
    ArrayList<User> selectMap(HashMap<String,String> map);
    //根据Object查询
    User selectObject(User user);

    //添加用户
    int insertUser(User user);
    //修改用户
    int updateUser(User user);
    //删除用户
    int deleteBy(int id);
}

 mapper映射的文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
	namespace唯一表示此名字下的crud语句
 -->
<mapper namespace="mapper.UserMapper">

    <!--  在查询时建议使用指定字段代替* 但是在语句书写时会书写多次 这个时候就可以通过sql进行定义和使用  -->
    <sql id="test">
        `id`,`name`,`sal`,`birthday`
    </sql>

    <select id="selectAll" resultType="com.yunhe.entity.User" >
        select <include refid="test"></include> from t_user
    </select>

    <select id="selectById" resultType="user">
        select * from t_user where id=#{id}
    </select>

    <select id="selectByIdAndName" resultType="com.yunhe.entity.User">
        select * from t_user where id=#{arg0} and name=#{arg1}
    </select>

    <select id="selectList" resultType="com.yunhe.entity.User">
        select * from t_user where id in (#{list[0]},#{list[1]})
    </select>

    <select id="selectArry" resultType="com.yunhe.entity.User">
        select * from t_user where id in (#{array[0]},#{array[1]})
    </select>

    <select id="selectMap" resultType="com.yunhe.entity.User">
        select * from t_user where id=#{id} and name=#{name}
    </select>

    <select id="selectObject" resultType="com.yunhe.entity.User">
        select * from t_user where id=#{id} and name=#{name}
    </select>

    <!--在insert语句执行之后,执行select语句,并将id的结果返回给实体类的id-->
    <insert id="insertUser">
        <selectKey resultType="int" keyColumn="id" keyProperty="id" order="AFTER">
            select LAST_INSERT_ID() as id
        </selectKey>
        insert into t_user (name,sal,birthday) values (#{name},#{sal},now())
    </insert>

    <update id="updateUser">
        update t_user set name=#{name} , sal=#{sal} where id=#{id}
    </update>

    <delete id="deleteBy">
        delete from t_user where id=#{id}
    </delete>

    <cache />
</mapper>

编写一个测试类 

package com.yunhe.util;

import com.yunhe.entity.User;
import mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.util.ArrayList;

/**
 * @author hou
 * @date 2021/12/15
 */
public class Test {
    public static void main(String[] args) throws IOException {
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //ArrayList<User> users = mapper.selectById(1);
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);   list.add(2);
        ArrayList<User> users = mapper.selectList(list);
//        ArrayList<User> users = mapper.selectByIdAndName(2, "武松");
//        HashMap<String,String> map = new HashMap<>();
//        map.put("id","2");
//        map.put("name","武松");
//        ArrayList<User> users = mapper.selectMap(map);
//
        for (User user:users) {
            System.out.println(user);
        }
//        User user = new User(2,"武松");
//        User user1 = mapper.selectObject(user);
//        System.out.println(user1);
//        User u = new User("孙二娘",500);
//        int i = mapper.insertUser(u);
//        User u = new User(3,"孙二娘2",100);
//        int i = mapper.updateUser(u);
//        int i = mapper.deleteBy(3);
//        User u = new User("宋江", 900);
//        int i = mapper.insertUser(u);
//        System.out.println(i);
//        System.out.println(u);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值