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` 方法能正确返回结果。 [2025-03-19 11:21:32 | AI写代码神器 | 1275点数解答]