SSH整合
一、导包
Hibernate里面的包:
(1)hibernate核心包
(2)Required里面的包
(3)Jpa里面的包
(4)Bytecode——cglib里面的包
Sl4j里面的:slf4j-1.5.8——slf4j-nop-1.5.8.jar
数据库的连接包:mysql-connector-java-5.0.4-bin.jar
Spring里面的包:
(1)dist里面所有的包
(2)依赖包reqlib——commons-logging.jar
(3)Aop里面所有的包
(4)连接池jdbc的包reqlib——commons-dbcp.jar和
Reqlib——commons-pool.jar
二、applicationContext_stt.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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<context:component-scan base-package="com"></context:component-scan>
<context:annotation-config/>
</bean>
</beans>
三、dbcp配置
<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:///test" />
<property name="username" value="root" />
<property name="password" value="root" />
四、测试连接
package com.shaotingting.test;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext_*.xml");
BasicDataSource bds=ac.getBean("dataSource",BasicDataSource.class);
try {
Connection conn=bds.getConnection();
conn.createStatement().executeUpdate("insert into t_test values(null,'admin','123456')");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
五、配置sessionFactory
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.shaotingting.po">
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
</bean>
六、测试SessionFactory
package com.shaotingting.test;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbcp.BasicDataSource;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.shaotingting.po.Tip;
public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext_*.xml");
SessionFactory sf=ac.getBean("mySessionFactory",SessionFactory.class);
Session session=sf.getCurrentSession();
Transaction ts=session.beginTransaction();
try {
ts.begin();
Tip tip=new Tip();
tip.setName("jack");
tip.setPassword("123456");
session.save(tip);
ts.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
ts.rollback();
e.printStackTrace();
}
}
}