需要新增的包
mysql-connector-java.jar(数据库连接包)
c3p0.jar(数据库连接池包)
spring-jdbc.jar(jdbc的支持)
beam.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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 加载Proerties配置文件 -->
<context:property-placeholder location="classpath:com/xxjqr/spring01/jdbc/db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
</bean>
<bean id="userDaoImpl" class="com.xxjqr.spring01.jdbc.UserDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
</beans>
db.properties
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///spring_db
user=root
password=221121
initialPoolSize=3
maxPoolSize=6
acquireIncrement=2
类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private String sex;
}
public interface UserDao {
public void save(User user);
public void update(User user);
public void delete(Serializable id);//Serializable可能只是想用多态吧
public User findById(Serializable id);
public List<User> getAll();
}
@Data
public class UserDaoImpl implements UserDao {
// 接收容器注入的JdbcTemplate对象
private JdbcTemplate jdbcTemplate;
// 1. 原始jdbc代码
public void save(User user) {
jdbcTemplate.update("insert into user_t(name,sex) values(?,?)", user.getName(),user.getSex());
}
@Override
public void delete(Serializable id) {
jdbcTemplate.update("delete from user_t where id=?", id);
}
@Override
public void update(User user) {
jdbcTemplate.update("update user_t set name=? where id=?", user.getName(),user.getId());
}
@Override
public User findById(Serializable id) {
// queryForList 把每一行都封装为map对象,再添加到list中
// List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from t_User");
// 传入类型参数,表示查询的列的类型; 这里只能查询一列
// List<String> list = jdbcTemplate.queryForList("select UserName from t_User", String.class);
List<User> list = jdbcTemplate.query("select * from user_t where id=?", new MyRowMapper(), id);
return (list!=null&&list.size()>0)?list.get(0):null;
}
@Override
public List<User> getAll() {
List<User> list = jdbcTemplate.query("select * from user_t", new MyRowMapper());
return list;
}
// 封装Springjdbc查询的结果集
class MyRowMapper 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"));
return user;
}
}
}
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml",App.class);
@Test
public void testApp() throws Exception {
UserDao userDao = (UserDao) ac.getBean("userDaoImpl");
// User user = new User();
// user.setName("丁昌江");
// user.setSex("男");
// userDao.save(user);
// userDao.delete(1);
// User user = new User();
// user.setId(2);
// user.setName("丁丁");
// userDao.update(user);
// 查询方法
// User user = userDao.findById(2);
// System.out.println(user);
System.out.println(userDao.getAll());
}
}