MyBatis实现简单增删改查操作

 MyBatis是一种半自动对象-表映射关系的数据访问(DAO)层框架,能够简单高效的实现对数据层访问。可以自动的进行对象的封装,但是sql仍然需要自己来写。

映射文件有元素:

insert – 映射插入语句
update – 映射更新语句
delete – 映射删除语句
select – 映射查询语句

元素属性:

id:Dao层接口中定义的方法名
parameterType:将要传入语句的参数的完全限定类名或别名。可选;
resultType:返回类型
useGeneratedKeys: 自动递增字段,默认false。(inser,update使用)
keyProperty:自动递增的字段,例如id

 

实体类

package com.test.model.user;

import java.sql.Timestamp;

public class User {
    
    private Integer id;
    private String name;
    private Integer age;
    private String sex;
    private String birthday;
    private Double salary;
    private Timestamp createTime;
    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;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday
                + ", salary=" + salary + ", createTime=" + createTime + "]";
    }

}
View Code

简单的增删改查操作

Dao层接口:UserDao

public interface UserDao {
    
    /**
     * 创建
     */
    boolean create(User user);
    
    /**
     * 查询
     */
    User getUserById(int id);
    
    /**
     * 更新
     */
    boolean update(User user);
    
    /**
     * 删除
     */
    boolean delete(int id);
    
}
View Code

Mapper文件:UserMapper

<?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.UserDao">
    <!-- 增加实体 -->
    <insert id="create" useGeneratedKeys="true" keyProperty="id">
        insert into user(
        name,
        age,
        sex,
        birthday,
        salary,
        create_time
        ) values(
        #{name},
        #{age},
        #{sex},
        #{birthday},
        #{salary},
        #{createTime}
        )
    </insert>
    
    <!-- 查询 -->
    <select id="getUserById" resultType="com.test.model.user.User">
        select * from user where id = #{id}
    </select>
    
    <!-- 更新 -->
    <update id="update">
        update user set name=#{name},sex=#{sex} where id = #{id}
    </update>
    
    <!-- 删除 -->
    <delete id="delete">
        delete from user where id = #{id}
    </delete>
    
</mapper>
View Code

测试代码:

/**
     * 创建User
     */
    @Test
    public void createUser() {
        //创建实体
        User user = new User();
        user.setName("Tom");
        user.setAge(18);
        user.setSex("男");
        user.setBirthday("2001-09-01");
        user.setSalary(12000.0);
        Date date = new Date(System.currentTimeMillis());       
        Timestamp createTime = new Timestamp(date.getTime());
        user.setCreateTime(createTime);
        System.out.println(user);
        //新增实体到数据库
        boolean create = userDao.create(user);
        System.out.println(create);
    }
    
    /**
     * 根据id查询用户
     */
    @Test
    public void getUserById() {
        User user = userDao.getUserById(1);
        System.out.println(user);
    }
    
    
    /**
     * 根据id查询用户
     */
    @Test
    public void update() {
        //创建实体
        User user = new User();
        user.setId(1);
        user.setName("rose");
        user.setSex("女");
        boolean update = userDao.update(user);
        System.out.println(update);
    }
    
    
    /**
     * 根据id查询用户
     */
    @Test
    public void delete() {
        boolean b = userDao.delete(2);
        System.out.println(b);
    }
View Code

 

运行结果:

新增

如果返回值定义的是boolean,新增成功返回true,新增失败返回false。如果定义的是int返回的是影响的行数。

 

查询

User [id=1, name=Tom, age=18, sex=男, birthday=2001-09-01, salary=12000.0, createTime=2019-09-28 14:11:51.0]

如果查不到返回null

 

更新

 

 如果更新失败返回false,更新成功返回true

 

删除

true

如果我们返回类型定义int类型,返回的是总共删除的行数

 

 

 

posted @ 2019-09-28 08:12  Jumpkin1122  阅读(281)  评论(1编辑  收藏  举报