千山鸟飞绝 万径人踪灭
勤练内功,不断实践招数。争取早日成为武林高手
接口

package cn.itcast.service;

import java.util.List;

import cn.itcast.bean.Person;

public interface IPersonService {

 public void save(Person person);
 
 public void update(Person person);
 
 public void delete(int personId);
 
 public Person getPerson(int personId);
 
 public List<Person> getPersons();
}

实现类:

package cn.itcast.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bean.Person;
import cn.itcast.service.IPersonService;


@Transactional
public class PersonServiceImpl implements IPersonService {

 private JdbcTemplate jdbcTemplete;

 // private DataSource datasource;
@Resource
 public void setDatasource(DataSource datasource) {
  this.jdbcTemplete = new JdbcTemplate(datasource);
 }

 public void delete(int personId) {
  this.jdbcTemplete
    .update("delete from  person where id=?",
      new Object[] { personId },
      new int[] { java.sql.Types.INTEGER });

 }

 public Person getPerson(int personId) {
  return (Person) this.jdbcTemplete.queryForObject(
    "select * from person where id=?", new Object[] { personId },
    new int[] { java.sql.Types.INTEGER }, new PersonRowMapper());

 }

 @SuppressWarnings("unchecked")
 public List<Person> getPersons() {

  return (List<Person>) this.jdbcTemplete.query("select * from person",
    new PersonRowMapper());

 }

 public void save(Person person) {
  System.out.println(person.getName());

  this.jdbcTemplete.update("insert into person(name) values(?)",
    new Object[] { person.getName() },
    new int[] { java.sql.Types.VARCHAR });

 }

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

 }

}


实体类:

package cn.itcast.bean;

public class Person {

 private int id;
 
 private String name;

 public Person() {
  
 }

 public Person(String name) {
 
  this.name = name;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
}



package cn.itcast.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import cn.itcast.bean.Person;

public class PersonRowMapper implements RowMapper {

 public Object mapRow(ResultSet rs, int index) throws SQLException {
  cn.itcast.bean.Person person=new Person(rs.getString("name"));
  person.setId(rs.getInt("id"));
  
  return person;
 }

}


配置文件:

<?xml version="1.0" encoding="UTF-8"?>

<!--
 - Application context definition for JPetStore's business layer.
 - Contains bean references to the transaction manager and to the DAOs in
 - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 <aop:aspectj-autoproxy proxy-target-class="true"/>

 
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
  <property name="url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&amp;characterEncoding=UTF-8"/>
  <property name="username" value="root"/>
  <property name="password" value=""/>
  <!-- 连接池启动时的初始值 -->
  <property name="initialSize" value="1"/>
  <!-- 连接池的最大值 -->
  <property name="maxActive" value="500"/>
  <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
  <property name="maxIdle" value="2"/>
  <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
  <property name="minIdle" value="1"/>
 </bean>


 <bean id="txManager"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
 </bean>

 <tx:annotation-driven transaction-manager="txManager" />


 <bean id="personServiceImpl"
  class="cn.itcast.service.impl.PersonServiceImpl">
  <property name="datasource" ref="dataSource" />
 </bean>

</beans>

测试类:

package junit.test;


import java.util.List;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.bean.Person;
import cn.itcast.service.IPersonService;

public class TestSpringAndJdbc {

 public static IPersonService iPersonService;
 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
  ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
  iPersonService=(IPersonService)ctx.getBean("personServiceImpl");
 }
 @Test
 public void TestSave(){
  for(int i=1;i<5;i++){
   iPersonService.save(new Person("传智博客"+i));
  }
  
 }
 @Test
 public void TestFindPerson(){
  
  iPersonService.getPerson(1);
  System.out.println(iPersonService.getPerson(1).getName());
 }
 
 @Test
 public void TestUpdate(){
  Person person=iPersonService.getPerson(1);//通过id取得person对象
  person.setName("张三");//设置名字
  iPersonService.update(person);//更新
 }
 
 @Test
 public void TestDelete(){
  Person person=iPersonService.getPerson(2);
  iPersonService.delete(person.getId());
 }

 @Test
 public void testFindAllPeron(){
  List<Person> list=iPersonService.getPersons();
  for(Person person:list){
   System.out.println(person.getId()+"  "+person.getName());
  }
 }
}







posted on 2009-09-03 16:03 笑口常开、财源滚滚来! 阅读(230) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: