spring+hibernate+struts+mysql

第一步:配置web.xml

      <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <!-- spring配置文件位置 -->
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:beans.xml</param-value>
  </context-param>
  <!-- spring核心监听器 -->
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- struts核心控制器 -->
  <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  <dispatcher>REQUEST</dispatcher>
  <dispatcher>FORWARD</dispatcher>
  </filter-mapping>
  <!-- 欢迎页面 -->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

第二步:配置sping 的配置文件 bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">


 <!-- 引入jdbc.properties -->
 <context:property-placeholder location="classpath:jdbc.properties"/>
 <!--自动扫描-->
 <context:component-scan base-package="com.acrel.bos"/>
<!-- c3p0数据源  -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置sessionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="dataSource" ref="dataSource"></property>
  <property name="hibernateProperties">
    <props>
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
    </props>
  </property>
  <!--配置自动扫描 -->
  <property name="packagesToScan">
     <list>
         <value>com.acrel.bos.domain</value>
     </list>
  </property>
</bean>
<!--配置事物声明  -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
 <property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 注解驱动事物管理 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--配置hibernateTemplate-->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
 <property name="sessionFactory" ref="sessionFactory"></property>
 <property name="cacheQueries">
   <value>true</value>
 </property>
</bean>
</beans>

 外部文件:jdbc.properties

jdbc.driverClass= com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/bos?useUnicode=true&characterEncoding=UTF-8
jdbc.user= root
jdbc.password=liailiang

log4j.properties的文件

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n


### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c:/mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n


### set log levels - for more verbose logging change 'info' to 'debug' ###


log4j.rootLogger=info, stdout

struts2的文件

 <?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>
<constant name="struts.devMode" value="true" />
<!-- 整合spring -->
<constant name="struts.objectFactory" value="spring"></constant>


<package name="basic-struts2" namespace="/" extends="struts-default">
<action name="index">
<result>/index.jsp</result>
</action>

<!-- 通过Action 将请求转发给 JSP,使用默认处理类 -->
<action name="page_*">
<result>/WEB-INF/pages/{1}.jsp</result>
</action>
<action name="pages_*_*">
<result>/WEB-INF/pages/{1}/{2}.jsp</result>
</action>

</package>


</struts>

dao层的接口,dao层的项目实现

package com.acrel.bos.dao;


import java.io.Serializable;
import java.util.List;


import org.hibernate.criterion.DetachedCriteria;


/**
 * 通用dao接口
 * @author Liailiang
 *
 */
public interface GenericDao<T>{
/**
* 保存
* @param entity
*/
public void save(T entity);

/**
* 删除
* @param entity
*/
public void delete(T entity);
/**
* 更新
* @param entity
*/
public void update(T entity);

/**
* 根据ID进行查询
* @param clazz
* @param id
* @return
*/
public T findEntityById(Class<T>clazz,Serializable id);

/**
* 查询所有
* @return
*/
public List<T>findAll();
/**
* 各种各样条件查询, 添加排序
* @param criteria
* @return
*/
public List<T>findByCriteria(DetachedCriteria criteria);

/**
* 分页查询
* @param criteria
* @param firstResult
* @param maxResults
* @return
*/
public List<T> findByCriteria(DetachedCriteria criteria, int firstResult , int maxResults);
/**
* 进行条件查询,使用hql, 在业务层调用NamedQuery名称就可以了
* @param namedQuery
* @param params
* @return
*/
public List<T> findByNamedQuery(String namedQuery, Object... params);
}

package com.acrel.bos.dao.impl;


import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;


import javax.annotation.Resource;


import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.HibernateTemplate;


import com.acrel.bos.dao.GenericDao;


public abstract class GenericDaoImpl<T> implements GenericDao<T> {


private Class<T> clazz;
public GenericDaoImpl() {
System.out.println("GenericDaoImpl我参构造器");
ParameterizedType type=(ParameterizedType) this.getClass().getGenericSuperclass();
this.clazz=(Class<T>) type.getActualTypeArguments()[0];
}
@Resource(name="hibernateTemplate")
private HibernateTemplate hibernateTemplate;
@Override
public void save(T entity) {
this.hibernateTemplate.save(entity);
}


@Override
public void delete(T entity) {
this.hibernateTemplate.delete(entity);

}


@Override
public void update(T entity) {
this.hibernateTemplate.update(entity);

}


@Override
public T findEntityById(Class<T> clazz, Serializable id) {

return this.hibernateTemplate.get(clazz, id);
}


@Override
public List<T> findAll() {

return this.hibernateTemplate.find("from "+clazz.getName());
}


@Override
public List<T> findByCriteria(DetachedCriteria criteria) {

return this.hibernateTemplate.findByCriteria(criteria);
}


@Override
public List<T> findByCriteria(DetachedCriteria criteria, int firstResult, int maxResults) {

return this.hibernateTemplate.findByCriteria(criteria,firstResult,maxResults);
}


@Override
public List<T> findByNamedQuery(String namedQuery, Object... params) {

return this.hibernateTemplate.findByNamedQuery(namedQuery, params);
}


}

本人学习整理内容,若有侵权请联系本人[email protected],本人删除。

猜你喜欢

转载自blog.csdn.net/lalg8094/article/details/51001203
今日推荐