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

          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 笑口常開、財源滾滾來! 閱讀(443) 評論(0)  編輯  收藏 所屬分類: spring學習
           
          主站蜘蛛池模板: 百色市| 普定县| 元阳县| 福州市| 宜章县| 怀远县| 苏尼特右旗| 九龙城区| 邵阳市| 东乌珠穆沁旗| 巴林左旗| 鱼台县| 宣恩县| 娄烦县| 仁寿县| 庆城县| 沽源县| 临海市| 东乡族自治县| 长葛市| 赤壁市| 阿瓦提县| 凤山市| 林周县| 安龙县| 财经| 响水县| 延寿县| 龙岩市| 开化县| 阜城县| 麻栗坡县| 资兴市| 新河县| 桃江县| 阿荣旗| 竹山县| 江城| 上虞市| 通渭县| 博客|