Spring 整合JDBC:
JdbcTemplate操作数据库:
在这个类中的内部已经处理完了数据库资源的建立和释放并且可以避免一些常见的错误,可以直接实例化,也可以通过依赖注入 的方式在ApplicationContext中参生作为Javabean的引用。运行核心的jdbc的工作流程提供很多重载方法提高程序的灵活性。使用 JdbcTemplate操作数据:直接操作不需要spring容器进行管理这个对象直接使用原始的方法只是spring对jdbc的操作单独封装到一个类中而已
/**
* jdbcTemplate类
* @author leoi555
*
*/
public class JdbcTemplateTest {
@Test
public void test() throws Exception {
//c3p0连接池
ComboPooledDataSource dataSource=new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql:///hibernate");
dataSource.setUser("root");
dataSource.setPassword("123");
JdbcTemplate jT=new JdbcTemplate();
jT.setDataSource(dataSource);
String sql="insert into user value(null,'123','adc','asssd.com')";
jT.update(sql);
}
@Test
public void test1() throws Exception {
//c3p0连接池
ComboPooledDataSource dataSource=new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql:///hibernate");
dataSource.setUser("root");
dataSource.setPassword("123");
JdbcTemplate jT=new JdbcTemplate();
jT.setDataSource(dataSource);
String sql="delete from user where id=1";
jT.update(sql);
System.out.println("删除成功");
}
@Test
public void test3() throws Exception {
//c3p0连接池
ComboPooledDataSource dataSource=new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql:///hibernate");
dataSource.setUser("root");
dataSource.setPassword("123");
JdbcTemplate jT=new JdbcTemplate();
jT.setDataSource(dataSource);
String sql="update user set name='账',password='1223',email='asdw@oo.com' where id=1";
jT.update(sql);
System.out.println("修改成功");
}
}
使用spring管理注入让spring管理jdbcTemplate对象
//public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
//super.getJdbcTemplate().
//可以直接将DataSource直接在这个类中注入时直接作为参数把数据源给注入就可以了
public class UserDaoImpl implements UserDao {
private JdbcTemplate Jt;
public JdbcTemplate getJt() {
return Jt;
}
public void setJt(JdbcTemplate jt) {
Jt = jt;
}
@Override
public void addUser(User user) {
// TODO Auto-generated method stub
String sql="insert into user values(null,'kkk','123','kkka222.COM')";
Jt.update(sql);
}
@Override
public void deleteUser(int id) {
// TODO Auto-generated method stub
String sql="delete from user where id=?";
Jt.update(sql,id);
System.out.println("删除成功");
}
@Override
public void updateUser(User user) {
// TODO Auto-generated method stub
String sql="update user set name='?',password='?',email='?' where id=?";
Jt.update(sql, user.getName(),user.getPassword(),user.getEmail(),user.getId());
}
@Override
public int getTotalCount() {
// TODO Auto-generated method stub
String sql="select count(*) from user";
return Jt.queryForObject(sql, Integer.class);
}
@Override
public List<User> getAllUser() {
// TODO Auto-generated method stub
String sql="select * from user ";
List<User> list=Jt.query(sql, new RowMapper<User>(){
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
// TODO Auto-generated method stub
User user=new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
return user;
}
});
return list;
}
@Override
public User getUserbyId(Integer id) {
// TODO Auto-generated method stub
String sql="select * from user where id=?";
return Jt.queryForObject(sql, new RowMapper<User>(){
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
// TODO Auto-generated method stub
User user=new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
return user;
}
},id);
}
配置 文件:
<!--Spring 读取指定的db.property配置 -->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!-- 将连接池放到spring 容器 -->
<bean name ="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 直接在这设置:<property name="jdbcUrl" value="jdbc:mysql:///hibernate"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="123"></property> -->
<!-- 这是通过Spring进行读取配置文件然后进行读取各个属性 -->
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 将JdbcTemplate放入Spring容器 -->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 将UserDao放入到Spring容器中 -->
<bean name="userDao" class="com.leo.jdbc.UserDaoImpl">
<property name="Jt" ref="jdbcTemplate"></property>
</bean>
</beans>
这样在测试中可以直接进行获得bean就可以操作了
**
* 通过交给Spring来实现
* @author leoi555
*
*/
public class AOPJdbcTemplate {
//先配置Datasource
//JDBCTemplate需要接受连接池
private UserDao userDao;
public void add() {
ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml");
userDao=(UserDao) context.getBean("userDao");
User user=new User();
user.setEmail("lkl@com");
user.setName("123");
user.setPassword("123");
userDao.addUser(user);
System.out.println("添加成功");
}
public void delete() {
ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml");
userDao=(UserDao) context.getBean("userDao");
User user=new User();
user.setId(1);
userDao.deleteUser(user.getId());
System.out.println("删除成功");
}
public void update() {
ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml");
userDao=(UserDao) context.getBean("userDao");
User user=new User();
user.setEmail("lkl@com");
user.setName("123");
user.setPassword("123");
user.setId(3);
userDao.updateUser(user);
System.out.println("修改成功");
}
public User getUserbyId() {
ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml");
userDao=(UserDao) context.getBean("userDao");
System.out.println("获得成功");
User user=new User();
user.setId(4);
User user2 =userDao.getUserbyId(user.getId());
System.out.println("获得成功");
System.out.println(user2.getId()+","+user2.getName()+","+user2.getPassword());
return user2;
}
public List<User> getListUser() {
ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml");
userDao=(UserDao) context.getBean("userDao");
List<User> user2 =userDao.getAllUser();
System.out.println("获得成功");
return user2;
}
}