注意要导包
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!--
配置dbcp的数据库连接池
-->
<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/hibernate0909" />
<property name="username" value="root" />
<!-- 如果你的密码是空的 就不要写这行属性<property name="password" value=" " /> -->
</bean>
<bean id="personDao" class="cn.itcast.spring0909.jdbc.PersonDaoImpl">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="personDao2" class="cn.itcast.spring0909.jdbc.PersonDaoImpl2">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate"/>
</property>
</bean>
<bean id="personDao3" class="cn.itcast.spring0909.jdbc.PersonDaoImpl3">
<constructor-arg index="0" ref="dataSource"></constructor-arg>
</bean>
</beans>
public class DataSourceTest extends SpringHelper{
static{
path = "cn/itcast/spring0909/jdbc/applicationContext.xml";
}
@Test
public void testDataSource(){
/**
* 完全的面向接口编程
*/
DataSource dataSource = (DataSource)context.getBean("dataSource");
System.out.println(dataSource);
}
}
public interface PersonDao {
public void savePerson();
public List<Person> getPersons();
}
public class PersonDaoImpl extends JdbcDaoSupport implements PersonDao {
public void savePerson() {
this.getJdbcTemplate().execute(
"insert into course(cid,cname) values(4,'aaa')");
}
@Override
public List<Person> getPersons() {
return this.getJdbcTemplate().query("select * from course", new PersonRowMapper());
}
}
public class PersonDaoImpl2 implements PersonDao {
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void savePerson() {
this.getJdbcTemplate().execute(
"insert into course(cid,cname) values(4,'aaa')");
}
@Override
public List<Person> getPersons() {
return null;
}
}
public class PersonDaoImpl3 extends JdbcTemplate implements PersonDao {
public PersonDaoImpl3(DataSource dataSource){
super(dataSource);
}
public void savePerson() {
// TODO Auto-generated method stub
this.execute(
"insert into course(cid,cname) values(4,'aaa')");
}
@Override
public List<Person> getPersons() {
// TODO Auto-generated method stub
return null;
}
}
以上为注入Datasource的三种方法
下面测试
public class PersonTest extends SpringHelper{
static{
path = "cn/itcast/spring0909/jdbc/applicationContext.xml";
}
@Test
public void testSave(){
PersonDao personDao = (PersonDao)context.getBean("personDao");
personDao.savePerson();
}
}
\
public class PersonRowMapper implements RowMapper{
@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Person person = new Person();
person.setPid(rs.getLong("cid"));
person.setPname(rs.getString("cname"));
return person;
}
/**
* 练习:
* 用spring集合jdbc完成crud的操作
* 查询:
* 查询一张表中所有的数据
* 根据主键查询某一行数据
* 根据条件查询数据
*/
}
spring的声明式事务管理
public interface PersonDao {
public void savePerson();
}
public class PersonDaoImpl extends JdbcDaoSupport implements PersonDao{
@Override
public void savePerson() {
this.getJdbcTemplate().execute("insert into person(pname) values('aa')");
int a = 1/0;
this.getJdbcTemplate().execute("insert into person(pname) values('aa')");
}
}
public interface PersonService {
public void savePerson();
}
public class PersonServiceImpl implements PersonService{
private PersonDao personDao;
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
@Override
public void savePerson() {
this.personDao.savePerson();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 产生dataSource -->
<bean
class="org.springframework.beans.factory.
config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 声明事务通知 id事务标识 transaction-manager -->
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.
DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<!-- 声明目标方法中哪些方法需要事务,哪些不需要事务 -->
<tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes>
<!-- name 限定方法的名称 isolation 隔离机制 propagation 传播机制 ready-only 只读 -->
<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED"
read-only="false" />
</tx:attributes>
</tx:advice>
<!-- spring容器做的事情 -->
<aop:config>
<aop:pointcut
expression="execution(* cn.itcast.spring0909.jdbc.transaction.
PersonServiceImpl.*(..))"
id="perform" />
<aop:advisor advice-ref="tx" pointcut-ref="perform"/>
</aop:config>
<!-- 程序员做的事情 -->
<bean id="personDao" class="cn.itcast.spring0909.jdbc.
transaction.PersonDaoImpl">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="personService" class="cn.itcast.spring0909.jdbc.transaction.
PersonServiceImpl">
<property name="personDao">
<ref bean="personDao" />
</property>
</bean>
</beans>
另一种导入datasource的方式
jdbc.porproties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/hibernate0909
jdbc.username=root
jdbc.password= root
jdbc.url=jdbc\:mysql\://localhost\:3306/hibernate0909
jdbc.username=root
jdbc.password= root
<!-- 产生dataSource -->
<bean
class="org.springframework.beans.factory.
config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
注意 applicationContext.xml配置文件换行 会造成错误