Spring JDBC+德鲁伊连接池:MVC架构下MySQL学生表查询、事务验证及代码实现全解析
1. 创建 `Student` 类 ```java package com.example.model; public class Student { private int id; private String name; private int age; // Getters and Setters public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } ``` 2. 创建 `StudentDao` 接口 ```java package com.example.dao; import com.example.model.Student; public interface StudentDao { Student findById(int id); } ``` 3. 实现 `StudentDao` 接口 ```java package com.example.dao.impl; import com.example.dao.StudentDao; import com.example.model.Student; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import javax.sql.DataSource; public class StudentDaoImpl implements StudentDao { private JdbcTemplate jdbcTemplate; public void setDataSource(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } @Override public Student findById(int id) { String sql = "SELECT * FROM student WHERE id = ?"; return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(Student.class)); } } ``` 4. 创建 `StudentService` 类 ```java package com.example.service; import com.example.dao.StudentDao; import com.example.model.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @Transactional public class StudentService { @Autowired private StudentDao studentDao; public Student getStudentById(int id) { return studentDao.findById(id); } } ``` 5. 配置 Spring 和 Druid 连接池 在 `applicationContext.xml` 中配置: ```xml <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="initialSize" value="1"/> <property name="minIdle" value="1"/> <property name="maxActive" value="20"/> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="studentDao" class="com.example.dao.impl.StudentDaoImpl"> <property name="dataSource" ref="dataSource"/> </bean> ``` 6. 创建测试类 ```java package com.example.service; import com.example.model.Student; 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; import static org.junit.Assert.assertNotNull; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:applicationContext.xml"}) public class StudentServiceTest { @Autowired private StudentService studentService; @Test public void testGetStudentById() { Student student = studentService.getStudentById(1); assertNotNull(student); } } ``` 7. `pom.xml` 文件配置 ```xml <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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>student-management</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.21</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.21</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.8</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.21</version> <scope>test</scope> </dependency> </dependencies> </project> ``` 注意事项 1. 确保 `applicationContext.xml` 和 `.properties` 文件正确配置,特别是数据库连接参数。 2. 在使用 `@Transactional` 注解时,确保 Spring 的事务管理器正确配置。 3. 测试时确保数据库中有相应的测试数据,以便 `findById` 方法能正确返回结果。 ######[AI写代码神器 | 1275点数解答 | 2025-03-19 11:21:32]
- 解决数据库“dm.jdbc.driver.dmexception: 数据溢出”问题的全步骤指南(阿里通义 | 598点数解答 | 2024-10-15 15:48:38)303
- 达梦数据库数据溢出问题排查及多维度解决方法(阿里通义 | 403点数解答 | 2024-10-15 15:48:41)1132
- MySQL 数据库操作:用德鲁伊连接池和 Spring JDBC 实现 MVC 架构下 Student 表操作及事务测试全解析(字节豆包 | 1115点数解答 | 2025-03-19 11:17:31)289
- Spring JDBC+德鲁伊连接池:MVC架构下MySQL学生表查询、事务验证及代码实现全解析(DeepSeek | 1275点数解答 | 2025-03-19 11:21:32)273
- 主副表 MySQL 数据库连接配置全揭秘(讯飞星火 | 92点数解答 | 2024-06-26 15:47:44)336
- MySQL主副表数据库配置信息大揭秘:驱动、URL、用户名与密码全知晓 (讯飞星火 | 210点数解答 | 2024-06-26 15:49:53)321
- 数据库连接报错剖析:SSL 配置不匹配及解决攻略 (字节豆包 | 205点数解答 | 2024-09-04 19:14:42)348
- Spring框架实战:从配置到实现,搞定员工信息查询功能(GPT | 2135点数解答 | 2024-06-12 08:32:35)311
- IntelliJ IDEA 搭建 Spring Boot 项目:从创建到 JPA 实体类及数据源配置全攻略(讯飞星火 | 920点数解答 | 2024-10-31 15:37:22)263
- 一文搞懂:B/S架构与C/S架构的定义、交互原理及常见应用 (阿里通义 | 107点数解答 | 2023-11-06 23:10:24)296
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)342
- MySQL:巧用 NOT LIKE 查询字段不包含特定字符串及注意要点 (DeepSeek | 264点数解答 | 2025-02-13 18:25:01)178