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

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

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

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

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

    實(shí)現(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 });

     }

    }


    實(shí)體類:

    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=""/>
      <!-- 連接池啟動(dòng)時(shí)的初始值 -->
      <property name="initialSize" value="1"/>
      <!-- 連接池的最大值 -->
      <property name="maxActive" value="500"/>
      <!-- 最大空閑值.當(dāng)經(jīng)過(guò)一個(gè)高峰時(shí)間后,連接池可以慢慢將已經(jīng)用不到的連接慢慢釋放一部分,一直減少到maxIdle為止 -->
      <property name="maxIdle" value="2"/>
      <!--  最小空閑值.當(dāng)空閑的連接數(shù)少于閥值時(shí),連接池就會(huì)預(yù)申請(qǐng)去一些連接,以免洪峰來(lái)時(shí)來(lái)不及申請(qǐng) -->
      <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>

    測(cè)試類:

    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);//通過(guò)id取得person對(duì)象
      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());
      }
     }
    }








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


    網(wǎng)站導(dǎo)航:
     
     
    主站蜘蛛池模板: 亚洲av无码一区二区乱子伦as| 日韩免费视频在线观看| 亚洲午夜国产片在线观看| 性色av极品无码专区亚洲| 色www永久免费视频| 亚洲日韩精品无码AV海量| 成年在线观看免费人视频草莓| 亚洲另类图片另类电影| 最近免费中文字幕视频高清在线看| 亚洲一区在线视频观看| 久久精品女人天堂AV免费观看| 亚洲最大的黄色网| 岛国大片免费在线观看| 亚洲国产精品自在自线观看| 成年女人永久免费观看片| 特a级免费高清黄色片 | 久久一区二区免费播放| 中文字幕精品亚洲无线码一区| 中文字幕无线码中文字幕免费| 亚洲av之男人的天堂网站| 最近免费mv在线电影| 亚洲砖码砖专无区2023| 婷婷亚洲天堂影院| 国产拍拍拍无码视频免费| 亚洲福利一区二区| 午夜成年女人毛片免费观看| 黄色一级视频免费| 国产精一品亚洲二区在线播放| 69视频免费观看l| 亚洲国产av玩弄放荡人妇| 亚洲成人一区二区| 99在线观看精品免费99| 亚洲AV日韩综合一区尤物| 亚洲精品成人网久久久久久| 日本亚洲欧洲免费天堂午夜看片女人员| 亚洲最大免费视频网| 亚洲成A∨人片天堂网无码| 无码免费一区二区三区免费播放| 国产婷婷综合丁香亚洲欧洲| 亚洲午夜激情视频| 一二三四在线播放免费观看中文版视频|