需要新增的包
mysql-connector-java.jar(數(shù)據(jù)庫連接包)
c3p0.jar(數(shù)據(jù)庫連接池包)
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可能只是想用多態(tài)吧
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");
// 傳入類型參數(shù)请琳,表示查詢的列的類型; 這里只能查詢一列
// 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());
}
}