mybatis--删除员工&新增员工&预编译操作
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package com.inmind;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringbootMybatisCrudApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringbootMybatisCrudApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.inmind.mapper;
|
||||
|
||||
import com.inmind.pojo.Emp;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper//生成接口的实现类对象,交给spring容器管理,DI自动注入
|
||||
public interface EmpMapper {
|
||||
/*
|
||||
删除员工
|
||||
*/
|
||||
@Delete("delete from emp where id = #{id}")
|
||||
// @Delete("delete from emp where id = ${id}")
|
||||
public int delete(Integer 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 insert(Emp emp);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.inmind.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Emp {
|
||||
private Integer id; //ID
|
||||
private String username; //用户名
|
||||
private String password;//密码
|
||||
private String name;//姓名
|
||||
private Short gender;//性别,1:男 2:女
|
||||
private String image;//图像url
|
||||
private Short job;//职位, 1:班主任 2:讲师 3:学工主管 4:教研主管 5:咨询师
|
||||
private LocalDate entrydate;// 入职日期
|
||||
private Integer deptId;//部门ID
|
||||
private LocalDateTime createTime;//创建时间
|
||||
private LocalDateTime updateTime;//修改时间
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
|
||||
#下面这些内容是为了让MyBatis映射
|
||||
#指定Mybatis的Mapper文件
|
||||
mybatis.mapper-locations=classpath:mappers/*xml
|
||||
#指定Mybatis的实体目录
|
||||
mybatis.type-aliases-package=com.inmind.mybatis.entity
|
||||
|
||||
|
||||
#驱动类名称
|
||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
#数据库连接的url
|
||||
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis2
|
||||
#连接数据库的用户名
|
||||
spring.datasource.username=root
|
||||
#连接数据库的密码
|
||||
spring.datasource.password=1234
|
||||
|
||||
|
||||
#输出mybatis的日志
|
||||
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.inmind;
|
||||
|
||||
import com.inmind.mapper.EmpMapper;
|
||||
import com.inmind.pojo.Emp;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@SpringBootTest
|
||||
class SpringbootMybatisCrudApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private EmpMapper empMapper;
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDelete(){
|
||||
//调用员工Mapper对象的删除员工的方法
|
||||
int delete = empMapper.delete(14);
|
||||
System.out.println("删除的员工数量为:"+delete);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testInsert() {
|
||||
Emp emp = new Emp();
|
||||
emp.setUsername("zhangsan");
|
||||
emp.setName("张三");
|
||||
emp.setGender((short) 1);
|
||||
emp.setImage("1.jpg");
|
||||
emp.setEntrydate(LocalDate.of(2025,10,26));
|
||||
emp.setDeptId(1);
|
||||
emp.setCreateTime(LocalDateTime.now());
|
||||
emp.setUpdateTime(LocalDateTime.now());
|
||||
empMapper.insert(emp);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user