Spring、整合Spring+JDBC

首先引入Spring包和JDBC所使用到的包:


配置beans.xml步骤:

1.配置dataSource的属性

2.将DataSource交给DataSourceTransactionManager管理

3.开启事务开关 

4.配置JdbcTemplate工具类

5.将jdbcTemplate注入到PersonServiceImpl中

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-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">

	<!-- 如果使用.properties 属性文件的方式加载配置项的话,使用context:property-placeholder加载 -->
	<!-- <context:property-placeholder location="classpath:jdbc.properties"/> -->
	
	<!-- 1.配置dataSource的属性 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url"
			value="jdbc:mysql://localhost:3306/springjdbc?useUnicode=true&characterEncoding=UTF-8" />
		<property name="username" value="root" />
		<property name="password" value="" />
		<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
		<property name="initialSize" value="1" />
		<!--连接池中保留的最大连接数。Default: 15 -->
		<property name="maxActive" value="500" />
		<!--最大空闲值,当经过一段峰值以后,连接会被释放掉一部分,直到释放到maxidle -->
		<property name="maxIdle" value="2" />
		<!--最小空闲值 当空闲的连接数小于阀值时,连接池会主去的去申请一些连接 -->
		<property name="minIdle" value="1" />
	</bean>
	<!-- 2.将DataSource交给DataSourceTransactionManager管理 -->
 	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 3.标明采用注解的方式配置事务 -->
	<tx:annotation-driven transaction-manager="txManager"/>
	<!-- 4.配置JdbcTemplate工具类 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
    </bean>
    <!-- 5.将jdbcTemplate注入到PersonServiceImpl中 -->
	<bean id="personIService" class="cn.service.impl.PersonServiceImpl">
		<property name="jdbcTemplate">
			<ref bean="jdbcTemplate"/>
		</property>
	</bean>
</beans>

配置PersonServiceImpl:

public interface PersonIService {
	
	void save(Person person);
	void update(Person person);
	Person getById(int id);
	List<Person> getPersons();
	void delete(int id);
}

@Transactional
public class PersonServiceImpl implements PersonIService {

	private JdbcTemplate jdbcTemplate;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public void save(Person person) {
		jdbcTemplate.update("insert into person(name) values(?)"
				,new Object[]{person.getName()}
				, new int[]{java.sql.Types.VARCHAR});
	}

	@Override
	public void update(Person person) {
		jdbcTemplate.update("update person set name=? where id=?"
				,new Object[]{person.getName(),person.getId()}
				, new int[]{ java.sql.Types.VARCHAR,java.sql.Types.INTEGER});
	}
	@Override
	public void delete(int id ) {
		jdbcTemplate.update("delete from person where id=?"
				,new Object[]{id}
				, new int[]{java.sql.Types.INTEGER});
	}

	@Override
	public Person getById(int id) {
		return (Person)jdbcTemplate.queryForObject("select * from person where id=?"
				, new Object[]{id}, new PersonRowMapper());
	}

	@Override
	public List<Person> getPersons() {
		return (List<Person>)jdbcTemplate.query("select * from person"
				, new Object[]{}, new PersonRowMapper());
	}
	
}

public class PersonRowMapper implements RowMapper {
	@Override
	public Object mapRow(ResultSet rs, int arg1) throws SQLException {
		Person person=new Person(rs.getString("name"));
		person.setId(rs.getInt("id"));
		return person;
	}
}

测试代码:

public class test {
	private PersonIService personIService;
	@org.junit.Before
	public void init(){
		ApplicationContext cxt=new ClassPathXmlApplicationContext("beans.xml");
		personIService=(PersonIService)cxt.getBean("personIService");
	}
	@Test
	public void test() {
		personIService.save(new Person("但丁"));
	}
	
	@Test
	public void test2() {
		Person person=personIService.getById(1);
		System.out.println(person.getName());
	}
	
	@Test
	public void test3() {
		Person person=personIService.getById(1);
		System.out.println(person.getName());
		person.setName("克路丝");
		personIService.update(person);
	}
	
	@Test
	public void test4() {
		personIService.delete(1);
	}
	@Test
	public void test5() {
		List<Person> list=personIService.getPersons();
		for(Person per : list){
			System.out.println(per.getName());
		}
	}
}


posted @ 2015-11-17 15:01  Bodi  阅读(246)  评论(0编辑  收藏  举报