Spring与hibernate整合例子
1、引入一下jar包:
2、Spring核心配置文件*.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!--连接池-->
<bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource"/>
<property name="mappingResources">
<list>
<value>vo/Tbuser.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<bean id="userDao" class="dao.UserDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
3、相关java类:
public class Tbuser implements java.io.Serializable {
private Integer id;
private String username;
private Integer age;
private String address;
/** default constructor */
public Tbuser() {
}
/** full constructor */
public Tbuser(String username, Integer age, String address) {
this.username = username;
this.age = age;
this.address = address;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return this.age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
}
<?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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="vo.Tbuser" table="tbuser" catalog="test">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="username" type="java.lang.String">
<column name="username" length="20" />
</property>
<property name="age" type="java.lang.Integer">
<column name="age" />
</property>
<property name="address" type="java.lang.String">
<column name="address" length="20" />
</property>
</class>
</hibernate-mapping>
package dao;
import java.sql.Statement;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import vo.Tbuser;
public class UserDao {
private SessionFactory sessionFactory;
public void save(Tbuser user){
Session session=sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
}
public void savebysql(){
try {
Session session=sessionFactory.openSession();
Statement st=session.connection().createStatement();
String sql="insert into tbuser(username,address,age) values('kimmin','台北',3)";
st.executeUpdate(sql);
st.close();
} catch (Exception e) {
e.printStackTrace();
}
}
4、测试类
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import vo.Tbuser;
import dao.UserDao;
public class Test {
@org.junit.Test
public void testsave(){
try {
ApplicationContext cfg=new ClassPathXmlApplicationContext("beans.xml");
UserDao dao=(UserDao)cfg.getBean("userDao");
Tbuser vo=new Tbuser();
vo.setUsername("cinday");
vo.setAddress("北京");
vo.setAge(4);
dao.save(vo);
} catch (Exception e) {
e.printStackTrace();
}
}
@org.junit.Test
public void testsavebysql(){
try {
ApplicationContext cfg=new ClassPathXmlApplicationContext("beans.xml");
UserDao dao=(UserDao)cfg.getBean("userDao");
dao.savebysql();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}