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

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

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

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

    package cn.itcast.bean;

    public class Person {

     private Integer id;
     private String name;
     
     public Person(){
      
     }
     
     public Person(String name) {
      this.name=name;
     }
     
       getter&&setter方法 
    }


    Person.hbm.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="cn.itcast.bean">
     <class name="Person" table="person">
     
      <id name="id" type="integer">
       <generator class="native"></generator>
      </id>
      <property name="name" length="10" not-null="true">
      </property>
     </class>

    </hibernate-mapping>

    定義業務接口

    package cn.itcast.service;

    import java.util.List;

    import cn.itcast.bean.Person;

    public interface IPersonService {

     /**
      * 保存人員信息
      * @param person
      */
     public abstract void save(Person person);

     /**
      * 更新信息
      * @param person
      */
     public abstract void update(Person person);

     /**
      * 獲取人員
      * @param personId
      * @return
      */
     public abstract Person getPerson(Integer personId);

     /**
      * 刪除人員信息
      * @param personId
      */
     public abstract void delete(Integer personId);

     /**
      * 獲取人員列表
      * @return
      */
     public abstract List<Person> getPersons();

    }


    業務實現類

    package cn.itcast.service.impl;

    import java.util.List;

    import javax.annotation.Resource;

    import org.hibernate.SessionFactory;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;

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

    /**
     * 業務層,采用注解聲明事務
     *
     * @author Administrator
     *
     */
    @Transactional
    public class PersonServiceBean implements IPersonService {

     @Resource
     private SessionFactory sessionFactory;

     public void save(Person person) {

      // 從spring 容器中得到正在管理的sessionFactory,persist方法用于保存實體

      sessionFactory.getCurrentSession().persist(person);

     }

     public void update(Person person) {
      sessionFactory.getCurrentSession().merge(person);
     }
    @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
     public Person getPerson(Integer personId) {
      return (Person) sessionFactory.getCurrentSession().get(Person.class,
        personId);
     }

     public void delete(Integer personId) {
      sessionFactory.getCurrentSession()
        .delete(
          sessionFactory.getCurrentSession().load(Person.class,
            personId));
     }
     @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
     @SuppressWarnings("unchecked")
     public List<Person> getPersons() {
      return sessionFactory.getCurrentSession().createQuery("from Person")
        .list();
     }

    }


    hibernate && spring的配置文件
    beans.xml

    <?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">

     <context:annotation-config />
     <!-- 配置數據源 -->
     <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="sessionFactory"
      class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      <property name="dataSource" ref="dataSource" /><!-- 將datasource注入到sessionFactory -->
      <property name="mappingResources">
       <list>
        <value>cn/itcast/bean/Person.hbm.xml</value>
       </list>
      </property>
      <property name="hibernateProperties">
       <value>
        hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
        hibernate.hbm2ddl.auto=update hibernate.show_sql=false
        hibernate.format_sql=false
       </value>
      </property>
     </bean>

     <!--  通過事務管理 管理sessionFactory -->
     <bean id="txManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager">

      <property name="sessionFactory" ref="sessionFactory" />
     </bean>

     <tx:annotation-driven transaction-manager="txManager" />
     <bean id="personServiceBean"
      class="cn.itcast.service.impl.PersonServiceBean">
     </bean>
    </beans>



    /**
    測試類**/

    package junit;


    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 IPersonServiceTest {

     private static IPersonService ipersonservice;
     @BeforeClass
     public static void setUpBeforeClass() throws Exception {
      try {
       ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
       ipersonservice=(IPersonService)ctx.getBean("personServiceBean");
      } catch (RuntimeException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
     }
     
     @Test
     public void TestSave(){
      
      ipersonservice.save(new Person("小張"));
      System.out.println("保存成功");
     }

     @Test public void testGetPerson(){
      Person person=ipersonservice.getPerson(1);
      System.out.println(person.getName());
     }
     
     @Test public void testUpdate(){
      Person person=ipersonservice.getPerson(1);
      person.setName("小麗");
      ipersonservice.update(person);
     }
     
     @Test public void testGetPersons(){
      List<Person> persons=ipersonservice.getPersons();
      for(Person person : persons){
       System.out.println(person.getId()+"  :" +person.getName());
      }
     }
     
     @Test public void testDelete(){
      ipersonservice.delete(1);
     }
    }

    table :person
    id  int
    name varchar

    posted on 2009-09-13 16:38 笑口常開、財源滾滾來! 閱讀(440) 評論(0)  編輯  收藏 所屬分類: spring學習
     
    主站蜘蛛池模板: 亚洲精品亚洲人成人网| 亚洲三级在线免费观看| 精品成人一区二区三区免费视频| 亚洲日韩乱码中文无码蜜桃 | 色多多www视频在线观看免费| 在线观看亚洲AV每日更新无码| 久久亚洲最大成人网4438| 亚洲av无码国产综合专区| 亚洲午夜电影在线观看| 亚洲噜噜噜噜噜影院在线播放 | 国产aⅴ无码专区亚洲av麻豆 | 亚洲一级毛片免费观看| 在线精品一卡乱码免费| 特级做A爰片毛片免费69| 日韩亚洲国产高清免费视频| 一二三四免费观看在线电影 | 久久永久免费人妻精品下载 | 国产亚洲视频在线播放大全| 色偷偷尼玛图亚洲综合| 免费国产黄网站在线观看动图| a免费毛片在线播放| 西西人体免费视频| 69视频免费观看l| 免费中文熟妇在线影片 | 99国产精品免费视频观看| 亚洲免费福利视频| 免费无码一区二区三区蜜桃大| 国产一级淫片视频免费看| 亚洲日本一区二区一本一道| 国产v亚洲v天堂无码网站| 久久亚洲精精品中文字幕| 亚洲xxxx视频| 日本黄页网址在线看免费不卡 | 亚洲第一综合天堂另类专| 特级毛片aaaa免费观看| 久久免费公开视频| 成人人观看的免费毛片| 亚洲一区二区视频在线观看 | 国产精品免费电影| 亚洲乱色熟女一区二区三区丝袜| 久久精品国产亚洲AV麻豆网站|