spring学习(九)—spring jdbcTemplate的使用简介
2017-02-17 15:58
513 查看
转载自传智博客学习视频
1.使用spring的jdbcTemplate进行数据库的操作
**
2.使用c3p0数据库连接池进行数据库的查询操作,并将c3p0连接池的相关配置工作放到配置文件中进行
**
测试类:
1.使用spring的jdbcTemplate进行数据库的操作
package jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.junit.Test; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; public class JdbcTemplateDemo1 { @Test public void add() { //使用spring的jdbcTemplate进行数据库的添加操作,不需要传统的jdbc方法要使用大量的冗余代码保证流程的正常进行 DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql:///springlearn"); dataSource.setUsername("root"); dataSource.setPassword("root"); JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); String sql = "insert into user values(?,?)"; int rows = jdbcTemplate.update(sql, "lucy", "250"); System.out.println(rows); } @Test public void testJDBC() { //使用传统的jdbc驱动进行数据库的查询操作,此种方法需要大量的冗余代码以保证数据库查询操作的正常进行。 Connection conn = null; PreparedStatement psmt = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql:///springlearn", "root", "vislecaina"); String sql = "select * from user where username=?"; psmt = conn.prepareStatement(sql); psmt.setString(1, "lucy"); rs = psmt.executeQuery(); while(rs.next()){ String username = rs.getString("username"); String password = rs.getString("password"); User user = new User(); user.setUsername(username); user.setPassword(password); System.out.println("user:"+user); } } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally{ try{ rs.close(); psmt.close(); conn.close(); } catch(Exception e){ e.printStackTrace(); } } } }
package jdbc; public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [username=" + username + ", password=" + password + "]"; } }
**
2.使用c3p0数据库连接池进行数据库的查询操作,并将c3p0连接池的相关配置工作放到配置文件中进行
**
测试类:
package c3p0; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestService { @Test public void testDemo(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); UserService userService = (UserService) context.getBean("userService"); userService.add(); } }
package c3p0; import org.springframework.jdbc.core.JdbcTemplate; public class UserDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void add() { String sql = "insert into user values(?,?)"; jdbcTemplate.update(sql, "lilei", "520"); } }
package c3p0; public class UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void add() { userDao.add(); } }
<?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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here --> <!-- 配置c3p0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 注入属性值 --> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///springlearn"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> </bean> <bean id="userService" class="c3p0.UserService"> <property name="userDao" ref="userDao"></property> </bean> <bean id="userDao" class="c3p0.UserDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <!-- 创建jdbc模板对象 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
相关文章推荐
- Spring学习09-- AOP简介和使用
- Spring学习:使用Spring调度任务
- FastSpring学习笔记二(使用工具MyGeneration生成Model和NHibernate的代码 )
- Unity 学习笔记(1) — Unity简介及简单使用
- [转]spring学习笔记:annotation的使用1
- Spring学习笔记(15)----使用Spring注解方式管理事务
- 学习使用Spring应该能具有过滤的能力.
- Java 线程池学习 Reference: 《创建Java线程池》[1],《Java线程:新特征-线程池》[2], 《Java线程池学习》[3],《线程池ThreadPoolExecutor使用简介
- spring学习笔记5——Spring里使用JDBC
- Spring视频学习(十)使用XML配置事务
- [转]spring学习笔记:annotation的使用3
- FastSpring学习笔记二(使用工具MyGeneration生成Model和NHibernate的代码 )
- ITCAST视频-Spring学习笔记(使用CGLIB实现AOP功能与AOP概念解释)
- ITCAST视频-Spring学习笔记(使用JDK中的Proxy技术实现AOP功能)
- spring包使用详解 --- 传智播客spring视频学习自总结
- Spring学习-webcontex的自动配置和事件传播机制及使用
- ITCAST视频-Spring学习笔记(使用构造器装配属性)
- ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP的细节)
- MyBatis学习 之 一、MyBatis简介与配置MyBatis+Spring+MySql
- mybatis学习 一、MyBatis简介与配置MyBatis+Spring+MySql