diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/20241223 Spring\347\272\257\346\263\250\350\247\243.md" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/20241223 Spring\347\272\257\346\263\250\350\247\243.md" new file mode 100644 index 0000000000000000000000000000000000000000..064bb73ffed3e676ee8a93fccd3933964e67475b --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/20241223 Spring\347\272\257\346\263\250\350\247\243.md" @@ -0,0 +1,597 @@ +## 课堂笔记 + +#### **使用 Spring 纯 注解 进行数据库的操作步骤 及 注意事项** + +1. ##### **准备sql数据** + +2. ##### **导入依赖项** + +3. ##### **在 controller 目录中编写 表示层** + +4. ##### **在 dao 目录 (Mapper) 中编写 数据处理层** + +5. ##### **在 service 目录中编写 业务逻辑层** + +6. ##### **为 Bean 创建目录 config,并编写对应的 Bean 配置类** + + 1. ##### **SpringConfig 配置** + + 1. 声明自己是一个配置类 @Configuration + 2. 扫描Bean所在的包@ComponentScan("com.pdd") + 3. 为了方便管理,将jdbc和mybatis的配置单独成一个子配置文件 + 4. @ComponentScan({"com.controller","com.service","com.entity"}) 扫描包并生成对于的Bean类 + 5. @Import({JdbcConfig.class, MyBatisConfig.class}) 将刚才创建的两个子配置文件导入进来 + + 2. **JdbcConfig 配置** + + 1. @Component 声明自己是组件一部分 + 2. @PropertySource("classpath:jdbc.properties") 加载外部的属性文件 + 3. 以@Value注解,注入四个属性变量@Value("${jdbc.driver}"),@Value("${jdbc.url}") ...... + 4. 以自定义方法的形式,返回一个DataSource的对象 + 5. 不要忘记在这个方法上加上@Bean注解 + + 3. ##### **MybatisConfig 配置** + + 1. @Component 声明自己是组件一部分 + 2. @MapperScan("com.dao") 扫描daor代理接口所在的包 + 3. 以自定义方式的形式, 返回一个SqlSessionFactoryBean的对象 + 4. 不要忘记在这个方法上加上@Bean注解 + +7. ##### **测试代码** + + ##### **依赖注入的方式** + + 1. @RunWith(SpringJUnit4ClassRunner.class) 测试类上方加这个注解 + 2. @ContextConfiguration(classes = SpringConfig.class) 测试类上方加这个注解 + 3. 直接将需要的对象,以属性自动装配的方式注入 @Autowired ; + 4. 直接使用这个注入的对象; + +## 课后作业 + +#### **使用 Spring 纯 注解 进行数据库的操作** + +#### **项目结构预览** + +![image-20241223103750551](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/upgit_20241223_1734921470.png) + +#### **具体步骤** + +##### **一、准备sql数据** + +```sql +/* + Navicat Premium Dump SQL + + Source Server : mysql + Source Server Type : MySQL + Source Server Version : 80034 (8.0.34) + Source Host : localhost:3306 + Source Schema : company + + Target Server Type : MySQL + Target Server Version : 80034 (8.0.34) + File Encoding : 65001 + + Date: 06/12/2024 10:48:27 +*/ +drop database if exists company; +create database if not exists company; + +use company; + +SET NAMES utf8mb4; +SET FOREIGN_KEY_CHECKS = 0; + +-- ---------------------------- + +-- Table structure for dept + +-- ---------------------------- + +DROP TABLE IF EXISTS `dept`; +CREATE TABLE `dept` ( + `deptno` int NOT NULL, + `dname` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, + `loc` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, + PRIMARY KEY (`deptno`) USING BTREE +) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; + +-- ---------------------------- + +-- Records of dept + +-- ---------------------------- + +INSERT INTO `dept` VALUES (10, '教研部', '北京'); +INSERT INTO `dept` VALUES (20, '学工部', '上海'); +INSERT INTO `dept` VALUES (30, '销售部', '广州'); +INSERT INTO `dept` VALUES (40, '财务部', '武汉'); + +-- ---------------------------- + +-- Table structure for emp + +-- ---------------------------- + +DROP TABLE IF EXISTS `emp`; +CREATE TABLE `emp` ( + `empno` int NOT NULL, + `ename` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, + `job` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, + `mgr` int NULL DEFAULT NULL, + `hiredate` date NULL DEFAULT NULL, + `sal` decimal(8, 2) NULL DEFAULT NULL, + `comm` decimal(8, 2) NULL DEFAULT NULL, + `deptno` int NULL DEFAULT NULL, + PRIMARY KEY (`empno`) USING BTREE, + INDEX `deptno`(`deptno` ASC) USING BTREE, + CONSTRAINT `emp_ibfk_1` FOREIGN KEY (`deptno`) REFERENCES `dept` (`deptno`) ON DELETE SET NULL ON UPDATE CASCADE +) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; + +-- ---------------------------- + +-- Records of emp + +-- ---------------------------- + +INSERT INTO `emp` VALUES (1001, '甘宁', '文员', 1013, '2000-12-17', 8000.00, NULL, 20); +INSERT INTO `emp` VALUES (1002, '黛绮丝', '销售员', 1006, '2001-02-20', 16000.00, 3000.00, 30); +INSERT INTO `emp` VALUES (1003, '殷天正', '销售员', 1006, '2001-02-22', 12500.00, 5000.00, 30); +INSERT INTO `emp` VALUES (1004, '刘备', '经理', 1009, '2001-04-02', 29750.00, NULL, 20); +INSERT INTO `emp` VALUES (1005, '谢逊', '销售员', 1006, '2001-09-28', 12500.00, 14000.00, 30); +INSERT INTO `emp` VALUES (1006, '关羽', '经理', 1009, '2001-05-01', 28500.00, NULL, 30); +INSERT INTO `emp` VALUES (1007, '张飞', '经理', 1009, '2001-09-01', 24500.00, NULL, 10); +INSERT INTO `emp` VALUES (1008, '诸葛亮', '分析师', 1004, '2007-04-19', 30000.00, NULL, 20); +INSERT INTO `emp` VALUES (1009, '曾阿牛', '董事长', NULL, '2001-11-17', 50000.00, NULL, 10); +INSERT INTO `emp` VALUES (1010, '韦一笑', '销售员', 1006, '2001-09-08', 15000.00, 0.00, 30); +INSERT INTO `emp` VALUES (1011, '周泰', '文员', 1008, '2007-05-23', 11000.00, NULL, 20); +INSERT INTO `emp` VALUES (1012, '程普', '文员', 1006, '2001-12-03', 9500.00, NULL, 30); +INSERT INTO `emp` VALUES (1013, '庞统', '分析师', 1004, '2001-12-03', 30000.00, NULL, 20); +INSERT INTO `emp` VALUES (1014, '黄盖', '文员', 1007, '2002-01-23', 13000.00, NULL, 10); + +-- ---------------------------- + +-- Table structure for salgrade + +-- ---------------------------- + +DROP TABLE IF EXISTS `salgrade`; +CREATE TABLE `salgrade` ( + `grade` int NOT NULL, + `losal` int NULL DEFAULT NULL, + `hisal` int NULL DEFAULT NULL, + PRIMARY KEY (`grade`) USING BTREE +) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; + +-- ---------------------------- + +-- Records of salgrade + +-- ---------------------------- + +INSERT INTO `salgrade` VALUES (1, 7000, 12000); +INSERT INTO `salgrade` VALUES (2, 12010, 14000); +INSERT INTO `salgrade` VALUES (3, 14010, 20000); +INSERT INTO `salgrade` VALUES (4, 20010, 30000); +INSERT INTO `salgrade` VALUES (5, 30010, 99990); + +SET FOREIGN_KEY_CHECKS = 1; +``` + +##### **二、导入依赖项** + +**pom.xml 依赖配置文件** + +```xml + + + 4.0.0 + + com.pdd + spring05 + 1.0-SNAPSHOT + + + + org.projectlombok + lombok + 1.18.36 + compile + + + + org.mybatis + mybatis + 3.5.16 + + + + + com.mysql + mysql-connector-j + 8.3.0 + + + + junit + junit + 4.13.2 + test + + + + org.springframework + spring-context + 5.3.39 + + + + + org.springframework + spring-jdbc + 5.3.39 + + + + + + + com.alibaba + druid + 1.1.21 + + + + + + org.mybatis + mybatis-spring + 2.0.6 + + + + + org.springframework + spring-test + 5.3.39 + + + +``` + +##### **三、在 controller 目录中编写 表示层** + +**EmployeeController 类** + +```java +package com.controller; + +import com.entity.Employee; +import com.service.EmployeeService; +import lombok.Data; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; + +@Data +@Controller // 标识这是一个 bean +public class EmployeeController { + // 表示层 + @Autowired // 自动装配 + private EmployeeService employeeService; // 依赖接口 + + // 按 id 查询一个员工 + public Employee selectOneEmployee(int id) { + Employee employee = employeeService.selectEmployeeById(id); + return employee; + } + + // 新增一个员工 + public String insertOneEmployee(Employee employee) { + Boolean aBoolean = employeeService.insertEmployee(employee); + return aBoolean ? "新增成功" : "新增失败"; + } + + // 修改一个员工 + public String updateOneEmployee(Employee employee) { + Boolean aBoolean = employeeService.updateEmployeeById(employee); + return aBoolean ? "修改成功" : "修改失败"; + } + + // 按 id 删除一个员工 + public String deleteOneEmployee(int id) { + Boolean aBoolean = employeeService.deleteEmployeeById(id); + return aBoolean? "删除成功" : "删除失败"; + } +} +``` + +##### **四、在 dao 目录 (Mapper) 中编写 数据处理层** + +**EmployeeDao 接口** + +```java +package com.dao; + +import com.entity.Employee; +import org.apache.ibatis.annotations.*; + +@Mapper +public interface EmployeeDao { + // 数据处理层 + + // 搜索员工方法 + @Select("select * from emp where empno = #{id}") + Employee selectEmployeeById(int id); + + // 新增员工方法 + @Insert("INSERT INTO `emp` VALUES (#{empno},#{ename}, #{job}, #{mgr}, #{hiredate},#{sal}, #{comm}, #{deptno});") + int insertEmployee(Employee employee); + + // 修改员工方法 + @Update("update `emp`\n" + + " set ename = #{ename},\n" + + " job=#{job},\n" + + " mgr=#{mgr},\n" + + " hiredate=#{hiredate},\n" + + " sal=#{sal},\n" + + " comm= #{comm},\n" + + " deptno = #{deptno}\n" + + " where empno = #{empno};") + int updateEmployeeById(Employee employee); + + // 删除员工方法 + @Delete("delete from emp where empno = #{empno};") + int deleteEmployeeById(int id); +} +``` + +##### **五、在 service 目录中编写 业务逻辑层** + +**EmployeeService 接口** + +```java +package com.service; + +import com.entity.Employee; + +public interface EmployeeService { + // 业务逻辑层 + // 加工搜索员工方法 + Employee selectEmployeeById(int id); + + // 加工新增员工方法 + Boolean insertEmployee(Employee employee); + + // 加工删除员工方法 + Boolean deleteEmployeeById(int id); + + // 加工修改员工方法 + Boolean updateEmployeeById(Employee employee); +} +``` + +**EmployeeServiceImpl 实现类** + +```java +package com.service; + +import com.dao.EmployeeDao; +import com.entity.Employee; +import lombok.Data; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Data +@Service +public class EmployeeServiceImpl implements EmployeeService { + @Autowired // 自动装配 + private EmployeeDao employeeDao; // 依赖接口 + + @Override + public Employee selectEmployeeById(int id) { + Employee employee = employeeDao.selectEmployeeById(id); + return employee; + } + + @Override + public Boolean insertEmployee(Employee employee) { + int i = employeeDao.insertEmployee(employee); + return i > 0; + } + + @Override + public Boolean deleteEmployeeById(int id) { + int i = employeeDao.deleteEmployeeById(id); + return i > 0; + } + + @Override + public Boolean updateEmployeeById(Employee employee) { + int i = employeeDao.updateEmployeeById(employee); + return i > 0; + } + +} +``` + +##### **六、为 Bean 创建目录 config,** + +![image-20241223110020108](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/upgit_20241223_1734922820.png) + +**SpringConfig 配置类** + +```java +package com.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +@Configuration // 声明自己是一个配置类 +@ComponentScan({"com.controller","com.service","com.entity"}) // 扫描 Bean 所在的包 +@Import({JdbcConfig.class, MybatisConfig.class}) // 导入两个Bean 配置文件 +public class SpringConfig { + +} +``` + +**JdbcConfig 配置类** + +```java +package com.config; + +import com.alibaba.druid.pool.DruidDataSource; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Component; + +@Component // 声明自己是组件在一部分 +@PropertySource("classpath:jdbc.properties") // 加载外部的属性文件 +public class JdbcConfig { + @Value("${jdbc.driver}") + private String driverClassName; + @Value("${jdbc.url}") + private String url; + @Value("${jdbc.username}") + private String username; + @Value("${jdbc.password}") + private String password; + + @Bean + public DruidDataSource getDruidDataSource(){ + DruidDataSource dataSource = new DruidDataSource(); + dataSource.setDriverClassName(driverClassName); + dataSource.setUrl(url); + dataSource.setUsername(username); + dataSource.setPassword(password); + return dataSource; + } +} +``` + +**MybatisConfig 配置类** + +```java +package com.config; + +import org.mybatis.spring.SqlSessionFactoryBean; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +import javax.sql.DataSource; + +@Component // 声明自己是组件在一部分 +@MapperScan("com.dao") // 扫描 dao 代理接口所在的包 +public class MybatisConfig { + @Bean + public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource){ + SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); + sqlSessionFactoryBean.setDataSource(dataSource); + return sqlSessionFactoryBean; + } +} +``` + +##### **七、测试代码** + +**TestClass 测试类** + +```java +package com.test; + + +import com.config.SpringConfig; +import com.controller.EmployeeController; +import com.entity.Employee; +import org.junit.Test; + +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = SpringConfig.class) +public class TestClass { + // 通用员工对象 + @Autowired // 自动装配 + private Employee employee; + + @Autowired // 自动装配 + private EmployeeController employeeController; + + // 查询员工 + @Test + public void T1() { + // 获取员工 + Employee employee = employeeController.selectOneEmployee(1001); + System.out.println(employee); + } + + // 新增员工 + @Test + public void T2() { + // 新增员工 + Employee emp = employee; + // 从容器中拿用户对象 并创建 员工对象 给数据库 + emp.setEmpno(1050); + emp.setEname("陈俊杰"); + emp.setJob("程序员"); + emp.setMgr(1001); + emp.setHiredate("2024-12-19"); + emp.setSal(6000.00); + emp.setComm(0); + emp.setDeptno(10); + + String msg = employeeController.insertOneEmployee(emp); + System.out.println(msg); + } + + // 修改员工 + @Test + public void T3() { + // 新增员工 + Employee emp = employee; + // 从容器中拿用户对象 并创建 员工对象 给数据库 + emp.setEmpno(1050); + emp.setEname("陈俊杰"); + emp.setJob("程序猿"); + emp.setMgr(1001); + emp.setHiredate("2024-12-19"); + emp.setSal(6000.00); + emp.setComm(0); + emp.setDeptno(10); + + String msg = employeeController.updateOneEmployee(emp); + System.out.println(msg); + } + + // 删除员工 + @Test + public void T4() { + // 删除员工 + String msg = employeeController.deleteOneEmployee(1050); + System.out.println(msg); + } +} +``` + +查询员工 + +![image-20241223104225215](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/upgit_20241223_1734921745.png) + +新增员工 + +![image-20241223104243896](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/upgit_20241223_1734921764.png) + +修改员工 + +![image-20241223104300315](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/upgit_20241223_1734921780.png) + +删除员工 + +![image-20241223104317719](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/upgit_20241223_1734921797.png) diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/.idea/.gitignore" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/.idea/.gitignore" new file mode 100644 index 0000000000000000000000000000000000000000..35410cacdc5e87f985c93a96520f5e11a5c822e4 --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/.idea/.gitignore" @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/.idea/compiler.xml" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/.idea/compiler.xml" new file mode 100644 index 0000000000000000000000000000000000000000..2c65d972d843e120d65c4cb2e8af4f3090db69fd --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/.idea/compiler.xml" @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/.idea/dataSources.xml" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/.idea/dataSources.xml" new file mode 100644 index 0000000000000000000000000000000000000000..4af22f86f2c0ef34e9c31a33c2e1f6b585bf0ecd --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/.idea/dataSources.xml" @@ -0,0 +1,12 @@ + + + + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://localhost:3306 + $ProjectFileDir$ + + + \ No newline at end of file diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/.idea/encodings.xml" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/.idea/encodings.xml" new file mode 100644 index 0000000000000000000000000000000000000000..aa00ffab7828f4818589659c804ec2cfd99baed3 --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/.idea/encodings.xml" @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/.idea/jarRepositories.xml" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/.idea/jarRepositories.xml" new file mode 100644 index 0000000000000000000000000000000000000000..c7ea92037640e4664f08b5998f6d08fbec36c2cd --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/.idea/jarRepositories.xml" @@ -0,0 +1,25 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/.idea/misc.xml" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/.idea/misc.xml" new file mode 100644 index 0000000000000000000000000000000000000000..5d8d5c0752646929003e045116450ab7cda64718 --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/.idea/misc.xml" @@ -0,0 +1,15 @@ + + + + + + + + + + \ No newline at end of file diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/.idea/uiDesigner.xml" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/.idea/uiDesigner.xml" new file mode 100644 index 0000000000000000000000000000000000000000..2b63946d5b31084bbb7dda418ceb3d75eb686373 --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/.idea/uiDesigner.xml" @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/pom.xml" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/pom.xml" new file mode 100644 index 0000000000000000000000000000000000000000..fa4edefc77915e6b643fc7b4cdc446e161180bfb --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/pom.xml" @@ -0,0 +1,76 @@ + + + 4.0.0 + + com.pdd + spring05 + 1.0-SNAPSHOT + + + + org.projectlombok + lombok + 1.18.36 + compile + + + + org.mybatis + mybatis + 3.5.16 + + + + + com.mysql + mysql-connector-j + 8.3.0 + + + + junit + junit + 4.13.2 + test + + + + org.springframework + spring-context + 5.3.39 + + + + + org.springframework + spring-jdbc + 5.3.39 + + + + + + + com.alibaba + druid + 1.1.21 + + + + + + org.mybatis + mybatis-spring + 2.0.6 + + + + + org.springframework + spring-test + 5.3.39 + + + \ No newline at end of file diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/config/JdbcConfig.java" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/config/JdbcConfig.java" new file mode 100644 index 0000000000000000000000000000000000000000..45953b6ed07d0416dec0bfe3c3db05bd9ad9fd19 --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/config/JdbcConfig.java" @@ -0,0 +1,30 @@ +package com.config; + +import com.alibaba.druid.pool.DruidDataSource; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Component; + +@Component // 声明自己是组件在一部分 +@PropertySource("classpath:jdbc.properties") // 加载外部的属性文件 +public class JdbcConfig { + @Value("${jdbc.driver}") + private String driverClassName; + @Value("${jdbc.url}") + private String url; + @Value("${jdbc.username}") + private String username; + @Value("${jdbc.password}") + private String password; + + @Bean + public DruidDataSource getDruidDataSource(){ + DruidDataSource dataSource = new DruidDataSource(); + dataSource.setDriverClassName(driverClassName); + dataSource.setUrl(url); + dataSource.setUsername(username); + dataSource.setPassword(password); + return dataSource; + } +} diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/config/MybatisConfig.java" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/config/MybatisConfig.java" new file mode 100644 index 0000000000000000000000000000000000000000..1059ab4661a1747a0ebbe13363dd293f8a2c7ae3 --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/config/MybatisConfig.java" @@ -0,0 +1,19 @@ +package com.config; + +import org.mybatis.spring.SqlSessionFactoryBean; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +import javax.sql.DataSource; + +@Component // 声明自己是组件在一部分 +@MapperScan("com.dao") // 扫描 dao 代理接口所在的包 +public class MybatisConfig { + @Bean + public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource){ + SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); + sqlSessionFactoryBean.setDataSource(dataSource); + return sqlSessionFactoryBean; + } +} diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/config/SpringConfig.java" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/config/SpringConfig.java" new file mode 100644 index 0000000000000000000000000000000000000000..cec7a6fae758ee07e24404a747c7698678d236a9 --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/config/SpringConfig.java" @@ -0,0 +1,12 @@ +package com.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +@Configuration // 声明自己是一个配置类 +@ComponentScan({"com.controller","com.service","com.entity"}) // 扫描 Bean 所在的包 +@Import({JdbcConfig.class, MybatisConfig.class}) // 导入两个Bean 配置文件 +public class SpringConfig { + +} diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/controller/EmployeeController.java" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/controller/EmployeeController.java" new file mode 100644 index 0000000000000000000000000000000000000000..bea41e22bb1f5e469a65a15de485ed84875d8a02 --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/controller/EmployeeController.java" @@ -0,0 +1,39 @@ +package com.controller; + +import com.entity.Employee; +import com.service.EmployeeService; +import lombok.Data; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; + +@Data +@Controller // 标识这是一个 bean +public class EmployeeController { + // 表示层 + @Autowired // 自动装配 + private EmployeeService employeeService; // 依赖接口 + + // 按 id 查询一个员工 + public Employee selectOneEmployee(int id) { + Employee employee = employeeService.selectEmployeeById(id); + return employee; + } + + // 新增一个员工 + public String insertOneEmployee(Employee employee) { + Boolean aBoolean = employeeService.insertEmployee(employee); + return aBoolean ? "新增成功" : "新增失败"; + } + + // 修改一个员工 + public String updateOneEmployee(Employee employee) { + Boolean aBoolean = employeeService.updateEmployeeById(employee); + return aBoolean ? "修改成功" : "修改失败"; + } + + // 按 id 删除一个员工 + public String deleteOneEmployee(int id) { + Boolean aBoolean = employeeService.deleteEmployeeById(id); + return aBoolean? "删除成功" : "删除失败"; + } +} diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/dao/EmployeeDao.java" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/dao/EmployeeDao.java" new file mode 100644 index 0000000000000000000000000000000000000000..83068a07851092e00199de41ba189cd182d8d8bf --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/dao/EmployeeDao.java" @@ -0,0 +1,33 @@ +package com.dao; + +import com.entity.Employee; +import org.apache.ibatis.annotations.*; + +@Mapper +public interface EmployeeDao { + // 数据处理层 + + // 搜索员工方法 + @Select("select * from emp where empno = #{id}") + Employee selectEmployeeById(int id); + + // 新增员工方法 + @Insert("INSERT INTO `emp` VALUES (#{empno},#{ename}, #{job}, #{mgr}, #{hiredate},#{sal}, #{comm}, #{deptno});") + int insertEmployee(Employee employee); + + // 修改员工方法 + @Update("update `emp`\n" + + " set ename = #{ename},\n" + + " job=#{job},\n" + + " mgr=#{mgr},\n" + + " hiredate=#{hiredate},\n" + + " sal=#{sal},\n" + + " comm= #{comm},\n" + + " deptno = #{deptno}\n" + + " where empno = #{empno};") + int updateEmployeeById(Employee employee); + + // 删除员工方法 + @Delete("delete from emp where empno = #{empno};") + int deleteEmployeeById(int id); +} diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/entity/Employee.java" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/entity/Employee.java" new file mode 100644 index 0000000000000000000000000000000000000000..7d2ab9e40541092a8a43fdde6d97b5ed46a28064 --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/entity/Employee.java" @@ -0,0 +1,22 @@ +package com.entity; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.stereotype.Component; + +/* 员工实体类 */ +@Data +@AllArgsConstructor +@NoArgsConstructor +@Component // 声明自己是组件在一部分 +public class Employee { + private int empno; + private String ename; + private String job; + private int mgr; + private String hiredate; + private double sal; + private double comm; + private int deptno; +} diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/service/EmployeeService.java" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/service/EmployeeService.java" new file mode 100644 index 0000000000000000000000000000000000000000..d91fc77fe18e5dec55e494bab64b74a4db2b8933 --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/service/EmployeeService.java" @@ -0,0 +1,18 @@ +package com.service; + +import com.entity.Employee; + +public interface EmployeeService { + // 业务逻辑层 + // 加工搜索员工方法 + Employee selectEmployeeById(int id); + + // 加工新增员工方法 + Boolean insertEmployee(Employee employee); + + // 加工删除员工方法 + Boolean deleteEmployeeById(int id); + + // 加工修改员工方法 + Boolean updateEmployeeById(Employee employee); +} diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/service/EmployeeServiceImpl.java" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/service/EmployeeServiceImpl.java" new file mode 100644 index 0000000000000000000000000000000000000000..5b801c62dc4923e303aea10c887dde8e9a81ad75 --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/java/com/service/EmployeeServiceImpl.java" @@ -0,0 +1,39 @@ +package com.service; + +import com.dao.EmployeeDao; +import com.entity.Employee; +import lombok.Data; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Data +@Service +public class EmployeeServiceImpl implements EmployeeService { + @Autowired // 自动装配 + private EmployeeDao employeeDao; // 依赖接口 + + @Override + public Employee selectEmployeeById(int id) { + Employee employee = employeeDao.selectEmployeeById(id); + return employee; + } + + @Override + public Boolean insertEmployee(Employee employee) { + int i = employeeDao.insertEmployee(employee); + return i > 0; + } + + @Override + public Boolean deleteEmployeeById(int id) { + int i = employeeDao.deleteEmployeeById(id); + return i > 0; + } + + @Override + public Boolean updateEmployeeById(Employee employee) { + int i = employeeDao.updateEmployeeById(employee); + return i > 0; + } + +} diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/resources/jdbc.properties" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/resources/jdbc.properties" new file mode 100644 index 0000000000000000000000000000000000000000..f4751e97aa358e9313d8034945681f42c26543cc --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/main/resources/jdbc.properties" @@ -0,0 +1,4 @@ +jdbc.driver = com.mysql.cj.jdbc.Driver +jdbc.url = jdbc:mysql:///company +jdbc.username = root +jdbc.password = 123456 \ No newline at end of file diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/test/java/com/test/TestClass.java" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/test/java/com/test/TestClass.java" new file mode 100644 index 0000000000000000000000000000000000000000..1b2e5d08f83e0654978997c512053c2d52b8f0c6 --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/src/test/java/com/test/TestClass.java" @@ -0,0 +1,77 @@ +package com.test; + + +import com.config.SpringConfig; +import com.controller.EmployeeController; +import com.entity.Employee; +import org.junit.Test; + +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = SpringConfig.class) +public class TestClass { + // 通用员工对象 + @Autowired // 自动装配 + private Employee employee; + + @Autowired // 自动装配 + private EmployeeController employeeController; + + // 查询员工 + @Test + public void T1() { + // 获取员工 + Employee employee = employeeController.selectOneEmployee(1001); + System.out.println(employee); + } + + // 新增员工 + @Test + public void T2() { + // 新增员工 + Employee emp = employee; + // 从容器中拿用户对象 并创建 员工对象 给数据库 + emp.setEmpno(1050); + emp.setEname("陈俊杰"); + emp.setJob("程序员"); + emp.setMgr(1001); + emp.setHiredate("2024-12-19"); + emp.setSal(6000.00); + emp.setComm(0); + emp.setDeptno(10); + + String msg = employeeController.insertOneEmployee(emp); + System.out.println(msg); + } + + // 修改员工 + @Test + public void T3() { + // 新增员工 + Employee emp = employee; + // 从容器中拿用户对象 并创建 员工对象 给数据库 + emp.setEmpno(1050); + emp.setEname("陈俊杰"); + emp.setJob("程序猿"); + emp.setMgr(1001); + emp.setHiredate("2024-12-19"); + emp.setSal(6000.00); + emp.setComm(0); + emp.setDeptno(10); + + String msg = employeeController.updateOneEmployee(emp); + System.out.println(msg); + } + + // 删除员工 + @Test + public void T4() { + // 删除员工 + String msg = employeeController.deleteOneEmployee(1050); + System.out.println(msg); + } +} \ No newline at end of file diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/config/JdbcConfig.class" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/config/JdbcConfig.class" new file mode 100644 index 0000000000000000000000000000000000000000..66be7bcda4a41b4d8cea8b4b5fc2dcc2afc840ef Binary files /dev/null and "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/config/JdbcConfig.class" differ diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/config/MybatisConfig.class" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/config/MybatisConfig.class" new file mode 100644 index 0000000000000000000000000000000000000000..883d2b8e650f21903194eb3b27cb4396062dcdbb Binary files /dev/null and "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/config/MybatisConfig.class" differ diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/config/SpringConfig.class" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/config/SpringConfig.class" new file mode 100644 index 0000000000000000000000000000000000000000..01d03fa50569fe43b135d008040c12bff6332880 Binary files /dev/null and "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/config/SpringConfig.class" differ diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/controller/EmployeeController.class" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/controller/EmployeeController.class" new file mode 100644 index 0000000000000000000000000000000000000000..3f4461fac6957b382dd57ee69de3da554803c43f Binary files /dev/null and "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/controller/EmployeeController.class" differ diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/dao/EmployeeDao.class" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/dao/EmployeeDao.class" new file mode 100644 index 0000000000000000000000000000000000000000..3347a78b94f675922841636d1833ea369409f66c Binary files /dev/null and "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/dao/EmployeeDao.class" differ diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/entity/Employee.class" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/entity/Employee.class" new file mode 100644 index 0000000000000000000000000000000000000000..bf87892993ab82eb73ed686eee671dfc5444c650 Binary files /dev/null and "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/entity/Employee.class" differ diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/service/EmployeeService.class" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/service/EmployeeService.class" new file mode 100644 index 0000000000000000000000000000000000000000..724ba0ea6c4e349a8e37ffb5ca66c84ccb85813c Binary files /dev/null and "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/service/EmployeeService.class" differ diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/service/EmployeeServiceImpl.class" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/service/EmployeeServiceImpl.class" new file mode 100644 index 0000000000000000000000000000000000000000..c064903761870931610552658e51b28ed804941b Binary files /dev/null and "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/com/service/EmployeeServiceImpl.class" differ diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/jdbc.properties" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/jdbc.properties" new file mode 100644 index 0000000000000000000000000000000000000000..f4751e97aa358e9313d8034945681f42c26543cc --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/classes/jdbc.properties" @@ -0,0 +1,4 @@ +jdbc.driver = com.mysql.cj.jdbc.Driver +jdbc.url = jdbc:mysql:///company +jdbc.username = root +jdbc.password = 123456 \ No newline at end of file diff --git "a/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/test-classes/com/test/TestClass.class" "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/test-classes/com/test/TestClass.class" new file mode 100644 index 0000000000000000000000000000000000000000..9587b97f940705155f65e95cb118a325e1d2e185 Binary files /dev/null and "b/\350\224\241\347\216\256\351\223\255/20241223 Spring\347\272\257\346\263\250\350\247\243/SpringProject/target/test-classes/com/test/TestClass.class" differ