发表于: 2025-04-02 20:23:45
0 2
今天完成的事情:
练习spring的应用
UserDao
package org.example.dao;
import org.example.model.User;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class UserDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
// 添加用户
public int insert(User user) {
String sql = "INSERT INTO ye(name, email, age) VALUES(?, ?, ?)";
return jdbcTemplate.update(sql, user.getName(), user.getEmail(), user.getAge());
}
// 查询所有用户
public List<User> selectAll() {
String sql = "SELECT * FROM ye";
return jdbcTemplate.query(sql, new UserRowMapper());
}
// 结果集映射
private static final class UserRowMapper implements RowMapper<User> {
@Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setEmail(rs.getString("email"));
user.setAge(rs.getInt("age"));
return user;
}
}
}
User
package org.example.model;
public class User {
private Integer id;
private String name;
private String email;
private Integer age;
// Getters and Setters
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
}
UserService
package org.example.service;
import org.example.dao.UserDao;
import org.example.model.User;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Transactional
public void addUser(User user) {
userDao.insert(user);
}
public List<User> getAllUsers() {
return userDao.selectAll();
}
}
beans.xml
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 加载数据库配置 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialSize" value="${jdbc.initialSize}"/>
<property name="maxTotal" value="${jdbc.maxActive}"/>
</bean>
<!-- 配置 JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置 DAO -->
<bean id="userDao" class="org.example.dao.UserDao">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!-- 配置 Service -->
<bean id="userService" class="org.example.service.UserService">
<property name="userDao" ref="userDao"/>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 启用注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
明天计划的事情:(一定要写非常细致的内容)
学习spring的一些基础知识
遇到的问题:(遇到什么困难,怎么解决的)
代码错误较多
收获:(通过今天的学习,学到了什么知识)
手动做出了spring简单代码
评论