酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

我要使用 MySQL 数据库,操作一个名为 student 的表,该表包含 id(int 类型)、name(varchar 类型)、age(int 类型)字段。使用 德鲁伊 连接池和 Spring JDBC 进行数据库操作,从 .properties 文件中读取连接池所需的参数。对 student 表按 id 进行查询操作,将查询结果用 Student 类封装,该类包含 id、name、age 属性。代码采用 MVC 架构,将数据持久层代码放在 dao 包下的 StudentDao 类中,同时提供测试上述功能的代码,测试功能使用junit4.0以上技术实现,使用@Transactional注解标记service类,将若干个增删改操作打包成一个事务,并验证事务的有效性,并写出它的pom.xml文件

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点数解答]

相关提问