mybatis--删除员工&新增员工&预编译操作
This commit is contained in:
33
springboot-mybatis-crud/.gitignore
vendored
Normal file
33
springboot-mybatis-crud/.gitignore
vendored
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
HELP.md
|
||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### STS ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
83
springboot-mybatis-crud/pom.xml
Normal file
83
springboot-mybatis-crud/pom.xml
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.inmind</groupId>
|
||||||
|
<artifactId>springboot-mybatis-crud</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>springboot-mybatis-crud</name>
|
||||||
|
<description>springboot-mybatis-crud</description>
|
||||||
|
<properties>
|
||||||
|
<java.version>11</java.version>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
|
<spring-boot.version>2.7.6</spring-boot.version>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis.spring.boot</groupId>
|
||||||
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
|
<version>2.3.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-dependencies</artifactId>
|
||||||
|
<version>${spring-boot.version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.8.1</version>
|
||||||
|
<configuration>
|
||||||
|
<source>11</source>
|
||||||
|
<target>11</target>
|
||||||
|
<encoding>UTF-8</encoding>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<version>${spring-boot.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<mainClass>com.inmind.SpringbootMybatisCrudApplication</mainClass>
|
||||||
|
<skip>true</skip>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>repackage</id>
|
||||||
|
<goals>
|
||||||
|
<goal>repackage</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -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