实体类
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 + "]"; } }
新增操作
Dao层接口:UserDao
package com.test.dao.user; import com.test.model.user.User; public interface UserDao { /** * 创建 */ boolean create(User user); }
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> </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); }
运行结果: