千山鳥飛絕 萬徑人蹤滅
          勤練內功,不斷實踐招數。爭取早日成為武林高手
          接口

          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 笑口常開、財源滾滾來! 閱讀(242) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
           
          主站蜘蛛池模板: 龙州县| 平湖市| 静海县| 城口县| 乃东县| 渝中区| 大竹县| 英山县| 赤城县| 大同县| 长顺县| 尼勒克县| 县级市| 嘉义县| 金乡县| 六盘水市| 石林| 若羌县| 瑞昌市| 英山县| 镇平县| 宜宾市| 潮州市| 乐清市| 昌邑市| 大冶市| 景德镇市| 梁河县| 安义县| 西丰县| 营口市| 蒲城县| 德安县| 山丹县| 潮安县| 宽甸| 延津县| 崇文区| 唐河县| 加查县| 突泉县|