<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

    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();
    }

    實現(xiàn)類:

    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"/>
      <!-- 最大空閑值.當經(jīng)過一個高峰時間后,連接池可以慢慢將已經(jīng)用不到的連接慢慢釋放一部分,一直減少到maxIdle為止 -->
      <property name="maxIdle" value="2"/>
      <!--  最小空閑值.當空閑的連接數(shù)少于閥值時,連接池就會預(yù)申請去一些連接,以免洪峰來時來不及申請 -->
      <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("張三");//設(shè)置名字
      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 笑口常開、財源滾滾來! 閱讀(241) 評論(0)  編輯  收藏

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


    網(wǎng)站導(dǎo)航:
     
     
    主站蜘蛛池模板: 亚洲一级免费毛片| 日日麻批免费40分钟日本的| 亚洲资源最新版在线观看| 国产亚洲精品岁国产微拍精品| 啦啦啦www免费视频| 最好看的中文字幕2019免费| 国产视频精品免费视频| 精品久久久久亚洲| 亚洲欧美一区二区三区日产| 亚洲男人电影天堂| 亚洲成A人片777777| 久99精品视频在线观看婷亚洲片国产一区一级在线 | 一出一进一爽一粗一大视频免费的| 亚洲午夜福利在线视频| 亚洲午夜久久久精品电影院| 97久久精品亚洲中文字幕无码 | 亚洲欧美日韩中文字幕一区二区三区| 亚洲综合综合在线| 亚洲一区影音先锋色资源| 亚洲国产精品无码av| 国产亚洲综合成人91精品| 亚洲无av在线中文字幕| 国产精品亚洲αv天堂无码| 亚洲av日韩片在线观看| 亚洲第一区在线观看| 亚洲第一网站男人都懂| 成人亚洲综合天堂| 免费v片在线观看无遮挡| 免费A级毛片无码久久版| 又黄又爽一线毛片免费观看 | 亚洲中文字幕一区精品自拍| 亚洲日日做天天做日日谢| 亚洲一区二区三区免费观看| 亚洲欧洲日产专区| 亚洲国产亚洲综合在线尤物| 亚洲一线产区二线产区精华| 亚洲中文无码永久免| 亚洲精品欧美综合四区| 曰批免费视频播放免费| 一级毛片免费视频网站| 三级黄色片免费看|