配置db的xml
引用
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
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/context http://www.springframework.org/schema/context/spring-context-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/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<!-- 配置 dataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="initialSize" value="10" />
<property name="maxActive" value="50"/>
<property name="maxIdle" value="10"/>
<property name="minIdle" value="2"/>
<property name="defaultAutoCommit" value="false"/>
<property name="defaultReadOnly" value="false"/>
</bean>
<!-- 配置 sessionFactory 相关的一些Hibernate信息在这里配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<!--
none while loading,do nothing
validate while loading,check the db tables
create while loading,re-create db tables,the old data will be deleted
create-drop while loading create db tables,and drop db tables after exiting
update while loading,create db tables which not exists,and will not delete old tables
-->
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingLocations">
<list>
<value>classpath:modelclass/*.hbm.xml</value>
</list>
</property>
</bean>
<!-- 配置事物管理器-->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 为事物管理器配置增强 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="false"/>
<tx:method name="*" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor pointcut="execution(* service.*.*(..))" advice-ref="txAdvice"/>
</aop:config>
</beans>
配置bean
引用
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
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/context http://www.springframework.org/schema/context/spring-context-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/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<bean id="PersonInfor" class="service.PersonInforImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
实体类Person
引用
package modelclass;
import java.util.UUID;
public class Person {
private String id = UUID.randomUUID().toString();
private String name;
private String age;
private String password;
Person(){
}
public Person(String name,String age,String password){
this.name = name;
this.age =age;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
根据实体类Person ,配置mapping
引用
<?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>
<class name="modelclass.Person">
<id name="id">
<generator class="uuid"/>
</id>
<property name="age" length="5"/>
<property name="name" length="20"/>
<property name="password" length="20"/>
</class>
</hibernate-mapping>
接口类PersonInfor
引用
package service;
import java.util.List;
import modelclass.Person;
public interface PersonInfor {
public void save(Person person);
public void update(Person person);
public Person getPerson(int personid);
public List<Person> queryPerson(String name);
}
接口实现类PersonInforImpl
引用
package service;
import java.util.List;
import modelclass.Person;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class PersonInforImpl extends HibernateDaoSupport implements PersonInfor{
public void save(Person person) {
super.getHibernateTemplate().save(person);
}
public void update(Person person) {
super.getHibernateTemplate().saveOrUpdate(person);
}
public Person getPerson(int personid) {
throw new UnsupportedOperationException("Not supported yet.");
}
public List<Person> queryPerson(String name) {
List<Person> personList = super.getHibernateTemplate().find("from Person p where p.name=?", name);
return personList;
}
}
以上是Spring集合Hibernate的具体实现,现在写个测试类
TestHibernate
引用
package testhibernate;
import java.util.Iterator;
import java.util.List;
import modelclass.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.PersonInfor;
public class TestHibernate {
public static void main(String arg[]){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-*.xml");
PersonInfor person = (PersonInfor)ctx.getBean("PersonInfor");
Person p =new Person("daniel","25","myrain");
//person.save(p);
List<Person> personList = person.queryPerson("daniel");
System.out.println(personList.size());
Iterator<Person> it= personList.listIterator();
while(it.hasNext()){
if(it.next().getName().equals("daniel")){
System.out.println("1");
}
}
}
}