<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.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>

    定義業(yè)務(wù)接口

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

    }


    業(yè)務(wù)實現(xiàn)類

    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;

    /**
     * 業(yè)務(wù)層,采用注解聲明事務(wù)
     *
     * @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 />
     <!-- 配置數(shù)據(jù)源 -->
     <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" />
      <!-- 最大空閑值.當(dāng)經(jīng)過一個高峰時間后,連接池可以慢慢將已經(jīng)用不到的連接慢慢釋放一部分,一直減少到maxIdle為止 -->
      <property name="maxIdle" value="2" />
      <!--  最小空閑值.當(dāng)空閑的連接數(shù)少于閥值時,連接池就會預(yù)申請去一些連接,以免洪峰來時來不及申請 -->
      <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>

     <!--  通過事務(wù)管理 管理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學(xué)習(xí)
     
    主站蜘蛛池模板: 国产又黄又爽又大的免费视频 | 国产又黄又爽又刺激的免费网址 | 久草视频在线免费看| 亚洲日韩国产精品乱| 粉色视频成年免费人15次| 国产精品成人四虎免费视频| 亚洲中文字幕无码av永久| 午夜dj免费在线观看| 亚洲欧美乱色情图片| 国产无遮挡又黄又爽免费视频| 久久亚洲精品高潮综合色a片| 四虎影视免费永久在线观看| 牛牛在线精品观看免费正| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 日韩免费的视频在线观看香蕉| 日韩亚洲一区二区三区| 中文字幕亚洲免费无线观看日本| 91亚洲精品视频| 无码国产精品一区二区免费I6| 亚洲AV永久无码精品一福利 | 免费看一级高潮毛片| 亚洲精品无码久久久久| 最近2019免费中文字幕视频三| 国产精品高清视亚洲精品| 免费人成视频在线观看视频| 国精产品一区一区三区免费视频| 亚洲最新永久在线观看| 在线A级毛片无码免费真人| 高潮毛片无遮挡高清免费| 亚洲欧洲无码AV电影在线观看| 亚洲一区二区三区免费视频| 亚洲国产欧美日韩精品一区二区三区| 亚洲女人被黑人巨大进入| 无码日韩精品一区二区三区免费| 午夜在线a亚洲v天堂网2019| 亚洲一级片内射网站在线观看| 精品一区二区三区无码免费视频| 亚洲国产成人久久精品软件| 亚洲AV综合色一区二区三区| 啦啦啦手机完整免费高清观看| 波霸在线精品视频免费观看|