Spring笔记-spring集合hibernate

配置db的xml
[quote]
<?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>
[/quote]
配置bean
[quote]<?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>
[/quote]
实体类Person
[quote]


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;
}
}
[/quote]
根据实体类Person ,配置mapping
[quote]
<?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>
[/quote]
接口类PersonInfor
[quote]


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);
}
[/quote]
接口实现类PersonInforImpl
[quote]
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;
}
}
[/quote]
以上是Spring集合Hibernate的具体实现,现在写个测试类
TestHibernate
[quote]


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");
}

}
}
}

[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值