MyBatis中Dao-Mapper操作(常用操作)

 

实体类

MyBatis中Dao-Mapper操作(常用操作)
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

MyBatis中Dao-Mapper操作(常用操作)
package com.test.dao.user;

import com.test.model.user.User;

public interface UserDao {
    
    /**
     * 创建
     */
    boolean create(User user);
}
View Code

Mapper文件:UserMapper

MyBatis中Dao-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.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>

</mapper>
View Code

测试:

MyBatis中Dao-Mapper操作(常用操作)
/**
     * 创建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);
    }
View Code

运行结果:

MyBatis中Dao-Mapper操作(常用操作)

 

MyBatis中Dao-Mapper操作(常用操作)

上一篇:使用application loader上传app,且生成构建版本


下一篇:关于.Net Core平台在苹果Mac系统快速卸载