发表于: 2025-03-18 20:49:09
0 18
今天完成的事情:
- 使用JdbcTemplate连接数据库分离Interface和Imple
添加 Spring 依赖
- 在你的 pom.xml 文件中添加以下依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Test3</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>6</source>
<target>6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Spring JDBC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.20</version>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<!-- Spring Context for configuration -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.12.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>创建 DAO 接口
创建一个名为 UserDao.java 的接口文件:package org.example;
import java.util.List;
public interface UserDao {
void addUser(String name, int QQ);
void updateUser(int id, String name, int QQ);
void deleteUser(int id);
List<String> queryAllUsers();
实现 DAO 接口
创建一个名为 UserDaoImpl.java 的类文件,使用 JdbcTemplate 实现 UserDao 接口:
package org.example;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import javax.sql.DataSource;
import java.util.List;
@Repository
public class UserDaoImpl implements UserDao {
private JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@Override
public void addUser(String name, int QQ) {
String sql = "INSERT INTO ye (name, QQ) VALUES (?, ?)";
jdbcTemplate.update(sql, name, QQ);
System.out.println("用户已添加: " + name + ", QQ: " + QQ);
}
@Override
public void updateUser(int id, String name, int QQ) {
String sql = "UPDATE ye SET name = ?, QQ = ? WHERE id = ?";
jdbcTemplate.update(sql, name, QQ, id);
System.out.println("用户已更新: ID: " + id + ", Username: " + name + ", QQ: " + QQ);
}
@Override
public void deleteUser(int id) {
String sql = "DELETE FROM ye WHERE id = ?";
jdbcTemplate.update(sql, id);
System.out.println("用户已删除: ID: " + id);
}
@Override
public List<String> queryAllUsers() {
String sql = "SELECT * FROM ye";
return jdbcTemplate.query(sql, new UserRowMapper());
}
private static class UserRowMapper implements RowMapper<String> {
@Override
public String mapRow(java.sql.ResultSet rs, int rowNum) throws java.sql.SQLException {
int id = rs.getInt("id");
String username = rs.getString("name");
int QQ = rs.getInt("QQ");
return "ID: " + id + ", Username: " + username + ", QQ: " + QQ;
}
}
}
配置数据源
创建一个 Spring 配置文件 applicationContext.xml 来配置数据源和 Bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 启用注解扫描 -->
<context:component-scan base-package="org.example"/>
<!-- 数据源配置 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/run?useSSL=false&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="199410"/>
</bean>
<!-- JdbcTemplate 配置 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
主程序
最后,修改 main 方法以从 Spring 上下文中获取 UserDao 实例:
package org.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = context.getBean(UserDao.class);
// 调用addUser方法添加新用户
userDao.addUser("newUsername", 12); // 自动分配ID
// 更新用户信息
userDao.updateUser(3, "updatedUsername", 98765);
// 删除用户
userDao.deleteUser(11);
// 查询所有用户
List<String> allUsers = userDao.queryAllUsers();
for (String user : allUsers) {
System.out.println(user);
}
}
}
明天计划的事:
学习Mybatis连接数据库
遇到的问题:
对代码的具体含义不了解
收获:
成功使用 jdbcTemplate连接数据库,并进行增减查改的操作以及junit测试,学习一些面向对象的基础知识。
评论