您的位置:首页 > 编程语言 > Java开发

SSH整合(spingmvc,spring,hibernate)以及Dao层抽取方法

2017-07-23 14:57 337 查看
拯救地球已经很累了,我不想再说废话.

pom文件,详见该分类下pom坐标

springmvc

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
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-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<description>Spring MVC Configuration</description>
<!-- 加载国际化配置文件 -->
<context:property-placeholder location="classpath:ssh.properties" />
<!-- springmvc配置 -->
<!-- 扫描controller -->
<context:component-scan base-package="com.my.controller" />

<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>

<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter"
/> JSON转换器 </list> </property> </bean> -->

<!-- springmvc jsp视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 以viewNames和order来搭配,从来进行多视图的配置 -->
<property name="viewNames">
<list>
<value>*jsp</value>
</list>
</property>
<property name="prefix" value="${jsp.prefix}" />
<property name="suffix" value="${jsp.suffix}" />
<property name="order" value="0" />
</bean>

<!-- springmvc配置器映射器 -->
<mvc:annotation-driven />

<!-- multipart解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="100000000000" />
</bean>
</beans>


spring-context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
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-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">

<description>Spring Configuration</description>

<!-- 加载配置属性文件 -->
<context:property-placeholder location="classpath:ssh.properties" />

<!-- 使用Annotation自动注册Bean,解决事物失效问题:在主容器中不扫描@Controller注解,在SpringMvc中只扫描@Controller注解。 -->
<context:component-scan base-package="com.my"><!-- base-package
如果多个,用“,”分隔 -->
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${oracle.driver}" />
<property name="url" value="${oracle.constr}" />
<property name="username" value="${oracle.name}" />
<property name="password" value="${oracle.password}" />
</bean>

<!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${oracle.driver}"/> <property name="jdbcUrl"
value="${oracle.constr}"/> <property name="user" value="${oracle.name}"/>
<property name="password" value="${oracle.password}"/> </bean> -->

<!-- 配置本地会话工厂bean -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 注入hibernate相关属性 -->
<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="javax.persistence.validation.mode">none</prop>
<prop key="hibernate.dbcp.ps.maxActive">100 </prop>
<prop key="hibernate.dbcp.ps.whenExhaustedAction">1 </prop>
<prop key="hibernate.dbcp.ps.maxWait">1200 </prop>
<prop key="hibernate.dbcp.ps.maxIdle">10 </prop>
</props>
</property>
<!-- 注入hbm映射文件 -->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/my/bean</value>
</list>
</property>
</bean>

<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 支持事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager" />

</beans>


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

<display-name>Archetype Created Web Application</display-name>

<!-- spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-context*.xml</param-value>
</context-param>
<!-- spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- sessoin过滤器 -->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
<!-- spring字符过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- springmvc的servlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>


Dao层抽取

package com.my.dao;

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

import org.hibernate.criterion.DetachedCriteria;

public interface BaseDao<T> {

/**
* 增
*/
public void save(T entity);

/**
* 改
*/
public void update(T entity);

/**
* 删
*/
public void delete(T entity);

/**
* 查
*/
public T find(Serializable id);

/**
* 查询所有
*/
public List<T> findAll();

/**
* 离线条件查询
*/
public List<T> findByConditions(DetachedCriteria criteria);

/**
* 命名空间查询
*/
public List<T> findByNamedQuery(String queryName, Object...objs);

/**
* 命名空间修改
*/
public void executeNamedQuery(String queryName, Object...ojbs);

}


package com.my.dao;

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

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.criterion.DetachedCriteria;
//spring提供的简化持久层操作的类
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> {

/**
* 父类需要注入sessionFacotry注解
* @param sessionFactory
*/
@Resource
public void setSF(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}

private Class<T> beanClass;
public BaseDaoImpl() {
ParameterizedType parameterizedType = (ParameterizedType) this.getClass().getGenericSuperclass();
Type[] types = parameterizedType.getActualTypeArguments();
beanClass = (Class<T>) types[0];
}

public void save(T entity) {
this.getHibernateTemplate().save(entity);
}

public void update(T entity) {
this.getHibernateTemplate().update(entity);
}

public void delete(T entity) {
this.getHibernateTemplate().delete(entity);
}

public T find(Serializable id) {
return this.getHibernateTemplate().get(beanClass, id);
}

public List<T> findAll() {
String hql = "FROM " +beanClass.getSimpleName();
return (List<T>) this.getHibernateTemplate().find(hql);
}

public List<T> findByConditions(DetachedCriteria criteria) {
return (List<T>) this.getHibernateTemplate().findByCriteria(criteria);
}

public List<T> findByNamedQuery(String queryName, Object... objs) {
return (List<T>) this.getHibernateTemplate().findByNamedQuery(queryName, objs);
}

public void executeNamedQuery(String queryName, Object... objs) {
Session session = this.getSessionFactory().openSession();
Query query = session.getNamedQuery(queryName);
if (objs != null && objs.length > 0) {
int i = 0;
for (Object object : objs) {
query.setParameter(i++, object);
}
}
query.executeUpdate();
}

}


package com.my.dao;

import com.my.bean.User;

public interface UserDao extends BaseDao<User>{

}


package com.my.dao;

import org.springframework.stereotype.Repository;

import com.my.bean.User;

@Repository
public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao{

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: