千山鸟飞绝 万径人踪灭
勤练内功,不断实践招数。争取早日成为武林高手

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 笑口常开、财源滚滚来! 阅读(439) 评论(0)  编辑  收藏 所属分类: spring学习

只有注册用户登录后才能发表评论。


网站导航: