MyBatis实现动态语句操作
实体类
public class Person { //实体属性 private Integer id; private String name; private Integer age; private String sex; private String birthday; private Double salary; private Timestamp createTime; //get-set方法 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } public Timestamp getCreateTime() { return createTime; } public void setCreateTime(Timestamp createTime) { this.createTime = createTime; } //toString方法 @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday + ", salary=" + salary + ", createTime=" + createTime + "]"; } }
Dao层接口
public interface PersonDao { /** * 查询(条件) */ Collection<Person> selectPerson(Person person); /** * 查询所有 */ Collection<Person> selectAll(); /** * 修改 */ boolean update(Person person); /** * 插入 */ boolean insertPerson(Person person); /** * 删除 */ int deleteOne(Person person); /** * 批量删除 */ int deleteMany(List<Person> personList); }
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" > <mapper namespace="com.test.dao.user.PersonDao"> <!-- 增加实体 --> <insert id="insertPerson" useGeneratedKeys="true" keyProperty="id"> insert into user <trim prefix="(" suffix=")" suffixOverrides=","> <if test="name !=null">name,</if> <if test="age != 0">age,</if> <if test="sex !=null">sex</if> </trim> values <trim prefix="(" suffix=")" suffixOverrides=","> <if test="name !=null">#{name},</if> <if test="age != 0">#{age},</if> <if test="sex !=null">#{sex}</if> </trim> </insert> <!-- 查询 条件--> <select id="selectPerson" resultType="com.test.model.user.Person"> select * from user <where> <if test="id!=null">id = #{id}</if> <if test="id!=null">and name = #{name}</if> <if test="id!=null">and age = #{age}</if> </where> </select> <!-- 查询所有 --> <select id="selectAll" resultType="com.test.model.user.Person"> select * from user </select> <!-- 更新 --> <update id="update"> update user <set> <if test="name!=null">name=#{name},</if> <if test="age!=null">age=#{age},</if> <if test="sex!=null">sex=#{sex},</if> <if test="birthday!=null">birthday=#{birthday},</if> <if test="birthday!=null">birthday=#{birthday},</if> <if test="salary!=null">salary=#{salary},</if> <if test="createTime!=null">createTime=#{createTime},</if> </set> where id = #{id} </update> <!-- 删除 --> <delete id="deleteOne"> delete from user <where> <if test="id != null">id=#{id}</if> <if test="name != null">and name=#{name}</if> <if test="age != null">and age=#{age}</if> </where> </delete> <!-- 批量删除 --> <delete id="deleteMany"> delete from user where id in <foreach collection="list" open="(" close=")" separator="," item="id"> #{id} </foreach> </delete> </mapper>
测试类
/** *插入 */ @Test public void insert() { //创建实体 Person person = new Person(); person.setName("Tom"); person.setAge(18); person.setSex("男"); person.setBirthday("2001-09-01"); person.setSalary(12000.0); Date date = new Date(System.currentTimeMillis()); Timestamp createTime = new Timestamp(date.getTime()); person.setCreateTime(createTime); System.out.println(person); //新增实体到数据库 boolean create = personDao.insertPerson(person); System.out.println(create); } /** * 查询全部 */ @Test public void selectAll() { Collection<Person> selectAll = personDao.selectAll(); for (Person person : selectAll) { System.out.println(person); } } /** * 条件查询 */ @Test public void select() { Person person = new Person(); // person.setId(4); person.setName("Tom"); person.setAge(18); Collection<Person> selectPerson = personDao.selectPerson(person); for (Person person2 : selectPerson) { System.out.println(person2); } } /** * 根据id查询用户 */ @Test public void update() { //创建实体 Person person = new Person(); person.setId(4); person.setName("Rose"); person.setAge(18); boolean update = personDao.update(person); System.out.println(update); } /** * 根据id查询用户 */ @Test public void delete() { //创建实体 Person person = new Person(); person.setId(4); person.setName("Rose"); person.setAge(18); int b = personDao.deleteOne(person); System.out.println(b); }
后面要整理。。。。