@MrXiao
2017-06-17T05:25:39.000000Z
字数 6119
阅读 1031
未分类

DriverManagerDataSource dataSource = new DriverManagerDataSource();dataSource.setDriverClassName("com.mysql.jdbc.Driver");dataSource.setUrl("jdbc:mysql://localhost:3306/springdb");dataSource.setUsername("root");dataSource.setPassword("***********");JdbcTemplate template = new JdbcTemplate(dataSource);template.execute("insert into student values(1,'张三',23)");
设置Bean
<bean id="dao" class="com.topvision.daotest.Dao"><property name="jdbcTemplate" ref="jdbcTemplate" /></bean><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource" /></bean><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/springdb" /><property name="username" value="root" /><property name="password" value="xiaoyue*AZ2V65" /></bean>
编写dao层测试类
@Testpublic void Demo1() {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("jdbcTemplete.xml");JdbcTemplate jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");jdbcTemplate.execute("insert into student values(5,'23423',24)");}
或者注解式
开启注解扫描
<context:component-scan base-package="com.topvision.daotest"/>
去掉dao层的bean,即如下配置
<bean id="dao" class="com.topvision.daotest.Dao"><property name="jdbcTemplate" ref="jdbcTemplate" /></bean>
编写测试类
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations="classpath:jdbcTemplete.xml")public class Dao {@Autowiredprivate JdbcTemplate jdbcTemplate;public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}@Testpublic void Demo2() {jdbcTemplate.execute("insert into student values(8,'王五',25)");}}
JdbcDaoSupport是JDBC数据访问对象的超类,其实它使用的还是jdbcTemplete,不过需要DI依赖注入dataSource或者jdbcTemplete.
xml配置文件
<bean id="jdbcDaoSupportTest" class="com.topvision.daotest.JDBCDaoSupportTest"><bean id="jdbcDaoSupportTest" class="com.topvision.daotest.JDBCDaoSupportTest"><!-- 必须为继承JdbcDaoSupport的类注入JdbcTemplate,变量名为jdbcTemplate --><!-- <property name="jdbcTemplate" ref="jdbcTemplate"/> --><!-- 还可以注入DataSource对象,由JdbcDaoSupport自动创建JdbcTemplate对象 --><property name="dataSource" ref="dataSource" /></bean><bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/springdb" /><property name="username" value="root" /><property name="password" value="*****" /></bean>
测试类
public class JDBCDaoSupportTest extends JdbcDaoSupport{private static ClassPathXmlApplicationContext ctx;@Testpublic void Demo2() {ctx = new ClassPathXmlApplicationContext("jdbcDaoSupport.xml");JDBCDaoSupportTest bean = (JDBCDaoSupportTest) ctx.getBean("jdbcDaoSupportTest");JdbcTemplate template = bean.getJdbcTemplate();String sql = "select name from student where id = ?";String name = template.queryForObject(sql, String.class,1);System.out.println(name);}}
xml配置
<bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/springdb" /><property name="username" value="root" /><property name="password" value="*****" /></bean>
测试类
public class JDBCDaoSupportTest extends JdbcDaoSupport{@Autowiredprivate DataSource dataSource;@PostConstructprivate void init() {setDataSource(dataSource);}@Testpublic void Demo1() {JdbcTemplate template = this.getJdbcTemplate();String sql = "select name from student where id = ?";String name = template.queryForObject(sql, String.class,1);System.out.println(name);}}
注入dataSource注意事项
查看jdbcDaoSupport源码,可以注入dataSource或者注入jdbcTemplete
注入方法有两种:
构造方法中注入:
@AutowiredUserVODaoImpl(DataSource dataSource) {setDataSource(dataSource);}
实例化前注入
@Autowiredprivate DataSource dataSource;@PostConstructprivate void init() {setDataSource(dataSource);}
资源包
<dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId></dependency>
配置项
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/springdb"/><property name="username" value="root" /><property name="password" value="****" /></bean>
资源包
<dependency><groupId>org.apache.commons</groupId><artifactId>com.springsource.org.apache.commons.dbcp</artifactId><version>1.2.2.osgi</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>com.springsource.org.apache.commons.pool</artifactId><version>1.5.3</version></dependency>
配置项
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/springdb" /><property name="username" value="root" /><property name="password" value="*****" /></bean>
资源包
<dependency><groupId>com.mchange.c3p0</groupId><artifactId>com.springsource.com.mchange.v2.c3p0</artifactId><version>0.9.1.2</version></dependency>
配置项
<bean id="dataSource3" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${driverClass}" /><property name="jdbcUrl" value="${url}" /><property name="user" value="${jdbc_username}" /><property name="password" value="${password}" /></bean>
编写properties文件(jdbc.properties)
driverClass=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/springdbjdbc_username=rootpassword=*****
在spring配置文件中扫描properties
方式一:可用*匹配加载多个
<context:property-placeholder location="classpath:jdbc.properties"/>
方式二:一个property对应一个文件
<bean class="org.springframework.beans.factory.config.PropertyPlaceholdeConfigurer"><property name="location" value="classpath:jdbc.properties" /><property name="location" value="classpath:dsafgusd" /></bean>
<bean id="dataSource3" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${driverClass}" /><property name="jdbcUrl" value="${url}" /><property name="user" value="${jdbc_username}" /><property name="password" value="${password}" /></bean>