首先是Spring 和 hibernate 整合
我早就吧ssh 整合所需的jar 包 提取了出来 ,直接导入到WEB-INF 下面的lib 中去
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/NewJxfweb
jdbc.username=root
jdbc.password=fanzhenliang
上面是jdbc.properties 文件 ,放在src 下就行了
beans.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"
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/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx[/url]
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
[url]http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config/> <!-- 开启注解配置项 -->
<context:component-scan base-package="cn.com"></context:component-scan>
<!-- 组建自动扫描交由spring 打理 -->
<bean id="prepertyspaceholder" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<!-- 占位符的配置 -->
<property name="location">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<!-- 数据源的配置 -->
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- sessionFactory 配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan">
<list>
<value>cn.com.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- hibernateTemplate 配置 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事物配置-->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 基于注解的事物管理驱动 配置-->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
这步之后 ,我们需要编写代码了,最简单的model ,仅作入门例子:
package cn.com.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="t_admin")
public class Admin {
private int id;
private String name;
private String password;
private Grounp grounp;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@ManyToOne
public Grounp getGrounp() {
return grounp;
}
public void setGrounp(Grounp grounp) {
this.grounp = grounp;
}
}
//然后我们接着写dao 层和dao 的实现层
package cn.com.dao;
import java.util.List;
import cn.com.model.Information;
public interface InformationDao {
public void save(Information information);
public void update(Information information);
public void delete(Information information);
public Information getInformation( int id);
public List<Information> getInformations();
public List<Information> getInformationsIsReleased();
}
package cn.com.dao.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import cn.com.dao.InformationDao;
import cn.com.model.Information;
@Transactional
@Component
public class InformationDaoImpl implements InformationDao {
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
@Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public void delete(Information information) {
hibernateTemplate.delete(information);
}
@Transactional(readOnly=true)
public Information getInformation( int id) {
return (Information)hibernateTemplate.get(Information.class, id);
}
@SuppressWarnings("unchecked")
@Transactional(readOnly=true)
public List<Information> getInformations() {
return (List<Information>)hibernateTemplate.find("from Information");
}
public void save(Information information) {
hibernateTemplate.save(information);
}
public void update(Information information) {
hibernateTemplate.update(information);
}
public List<Information> getInformationsIsReleased() {
return (List<Information>)hibernateTemplate.find("from Information where isRelease =?" , "yes");
}
}
//到这里, 我们需要测试下了,写个测试类 , 这步是很重要的,如果你不做的, 到时候出了问题你都不不知道在哪里出了问题。切记
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.com.dao.InformationDao;
import cn.com.model.Information;
public class InformationTest {
public static InformationDao informationDaoImpl;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
informationDaoImpl = (InformationDao)ac.getBean("informationDaoImpl");
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testSave() {
Information information = new Information();
information.setTitle("dfs");
informationDaoImpl.save(information);
}
@Test
public void testUpdate() {
Information information = new Information();
information.setId(1);
information.setTitle("liangliang");
informationDaoImpl.update(information);
}
@Test
public void testGet() {
Information information = informationDaoImpl.getInformation(1);
System.out.println(information.getTitle());
}
@Test
public void testGetAll() {
List<Information> informations = informationDaoImpl.getInformations();
for(Information information : informations)
{
System.out.println(information.getTitle());
}
System.out.println(informations.size());
}
@Test
public void testDelete() {
Information information = new Information();
information.setId(1);
informationDaoImpl.delete(information);
}
}
//如果测试没有问题的话, 我们接着下一步 , 集成Struts2 ,因为我在就把jar 包导了进去,现在只需要做两步
1.配置struts.xml 文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 这个是我们把struts创建对象的责任交给spring -->
<constant name="struts.objectFactory" value="spring"></constant>
<package name="information" namespace="" extends="struts-default">
<!-- 登入 -->
<action name="login" class="loginAction">
<result name="success">
/admin/Login.jsp
</result>
<result name="OK" >
/admin/index.html
</result>
<result name="error" >
/admin/Login.jsp
</result>
</action>
<!-- 信息中心 -->
<action name="information_*" class="informationAction" method="{1}">
<result>
/admin/informationRelease/InformationCenter.jsp
</result>
<result name="update">
/admin/informationRelease/UpdateInformation.jsp
</result>
</action>
</package>
</struts> |