<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无码乱码在线观看代蜜桃| 免费国产高清毛不卡片基地| 亚洲精品国产自在久久 | 国产精品视频免费一区二区三区| 久久美女网站免费| 国产成人无码免费网站| 亚洲精华国产精华精华液网站| 内射干少妇亚洲69XXX| 国产亚洲av片在线观看18女人| 四虎成人免费大片在线| 中文字幕免费在线看线人| 两个人看www免费视频| 色多多www视频在线观看免费| 亚洲免费综合色在线视频| 亚洲激情电影在线| 亚洲日韩区在线电影| 国产亚洲无线码一区二区| 亚洲午夜无码AV毛片久久| 全黄性性激高免费视频| 国产精品免费看香蕉| 卡一卡二卡三在线入口免费| 国产三级在线观看免费| 免费无码肉片在线观看| 免费A级毛片无码免费视| 日韩欧毛片免费视频| 99久久99这里只有免费费精品| 日本免费一区二区三区| 免费人成视频在线观看网站 | 亚洲精品成人区在线观看| 国产又大又长又粗又硬的免费视频| 在线成人a毛片免费播放| 思思99re66在线精品免费观看| 国产人在线成免费视频| 国内免费高清在线观看| 午夜成人免费视频| 日本免费中文字幕在线看| 日韩电影免费在线| 国产乱子伦精品免费女| 亚洲区小说区图片区| 亚洲精品制服丝袜四区| 亚洲精品视频在线|