Springboot中,用Mybatis对数据库进行增删改查
Mapper类
java
package com.wzb.MybatisExercise20240924;
import com.wzb.Pojo20240924.Emp;
import org.apache.ibatis.annotations.*;
@Mapper
public interface EmpMapper {
// 根据id查询数据
@Select("select * from emp where id=#{id}")
public void getEmp(Integer id);
// 增加新的数据
// 设置主键返回
@Options(useGeneratedKeys = true, keyProperty = "id")
@Insert("insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, " +
"update_time) values (#{username}, #{name}, #{gender}, #{image}, #{job}, #{entrydate}, " +
"#{deptId}, #{createTime}, #{updateTime})")
public void insertEmp(Emp emp);
// 修改表中数据
@Update("update emp set username = #{username}, name = #{name}, gender = #{gender}, image = #{image}, " +
"job = #{job}, entrydate = #{entrydate}, dept_id = #{deptId}, update_time = #{updateTime} " +
"where id = #{id}")
public void updateEmp(Emp emp);
// 按照id删除表中数据
@Delete("delete from emp where id = #{id}")
public void deleteEmp(Integer id);
}
Test测试类
java
package com.wzb;
import com.wzb.ConditionSelectExercise20240924.ConditionMapper;
import com.wzb.MybatisExercise20240924.EmpMapper;
import com.wzb.Pojo20240924.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.locks.Condition;
@SpringBootTest
class SpringbootExercise20240924ApplicationTests {
@Autowired
private EmpMapper empMapper;
@Autowired
private ConditionMapper cm;
// 查
// @Test
// public void getEmp() {
// empMapper.getEmp(19);
// }
// 增
// @Test
// public void insertEmp() {
// Emp emp = new Emp();
// emp.setUsername("Rose");
// emp.setName("肉丝");
// emp.setImage("1.jpg");
// emp.setGender((short)2);
// emp.setJob((short)1);
// emp.setEntrydate(LocalDate.of(2000,1,1));
// emp.setCreateTime(LocalDateTime.now());
// emp.setUpdateTime(LocalDateTime.now());
// emp.setDeptId(1);
//
// empMapper.insertEmp(emp);
// }
// // 改
// @Test
// public void updateEmp() {
// Emp emp = new Emp();
// // 根据Id修改
// emp.setId(25);
// emp.setUsername("Rose");
// emp.setPassword(null);
// emp.setName("肉丝儿");
// emp.setImage("2.jpg");
// emp.setGender((short)1);
// emp.setJob((short)2);
// emp.setEntrydate(LocalDate.of(2012,1,1));
// emp.setCreateTime(null);
// emp.setUpdateTime(LocalDateTime.now());
// emp.setDeptId(2);
//
// empMapper.updateEmp(emp);
// }
//
// // 删
// @Test
// public void deleteEmp() {
// empMapper.deleteEmp(25);
// }
// 条件查询
@Test
public void conditionSelect() {
List<Emp> empList = cm.conditionSelect("张", (short)1, LocalDate.of(2010, 1, 1),
LocalDate.of(2020, 1, 1));
for (Emp e : empList) {
System.out.println(e);
}
}
}
PojoEmp类
java
package com.wzb.Pojo20240924;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
private Integer id;
private String username;
private String password;
private String name;
private Short gender;
private String image;
private Short job;
private LocalDate entrydate; //LocalDate类型对应数据表中的date类型
private Integer deptId;
private LocalDateTime createTime;//LocalDateTime类型对应数据表中的datetime类型
private LocalDateTime updateTime;
}