S2SH框架整合(注解)Struts2+Spring+Hibernate+MySql
整合简介
Struts2+Spring4+hibernate4整合,Maven管理jar包,Eclipse工具。注解方式
架构截图
1、Spring整合Hibernate
1.1、创建Hibernate配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/woo0nise?characterEncoding=utf-8</property>
<property name="hibernate.connection.username">root</property>
</session-factory>
</hibernate-configuration>
该配置文件可以删除,下面会讲解到。
1.2、Spring整合Hibernate
创建db.properties文件用于存放数据库配置文件
#数据库配置信息 #数据库链接驱动 jdbc.driver=com.mysql.jdbc.Driver #数据库链接字符串 jdbc.url=jdbc:mysql://localhost:3306/woo0nise?characterEncoding=utf-8 #数据库用户名 jdbc.username=root #数据库密码 jdbc.password=root
创建applicationConext.xml,用于配置dao层
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
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-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<!-- 加载数据库配置文件 -->
<context:property-placeholder location="classpath:resource/db.properties" />
<!-- 配置DataSource (Druid数据源)-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" >
<property name="driverClassName" value="${jdbc.driver}" ></property>
<property name="url" value="${jdbc.url}" ></property>
<property name="username" value="${jdbc.username}" ></property>
<property name="password" value="${jdbc.password}" ></property>
</bean>
<!-- 配置session工厂 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource" ></property>
<!-- 配置hibernate的信息 -->
<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>
<!-- 加载由注解定义的持久化类 -->
<property name="packagesToScan">
<list>
<value>com.test.entity</value>
</list>
</property>
</bean>
<!-- 定义Autowired,自动注入Bean -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" ></bean>
<!-- 配置事务 -->
<!-- 配置事务管理器 -->
<bean
id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" ></bean>
<!-- 使用事务注解方式 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="com.test"></context:component-scan>
</beans>
applicationContext.xml
1、加载db.properties数据库配置文件
2、创建数据源用来创建sessionFctoty,需要数据库配置信息
3、创建sessionFactory用于产生session,需要数据源以及hibernate配置文件还有定义注解实体类的包
4、创建事务管理器,用于管理事务
5、创建事务
6、定义注解扫描器
1.3、创建struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="default" namespace="/" extends="struts-default" > <action name="ok" class="com.test.web.UserAction" method="register" > <result name="success">/index.jsp</result> </action> </package> </struts>
1.4、配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
</filter-mapping>
<!-- 加载spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
web.xml
3、测试整合
3.1、创建实体类
package com.test.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* User generated by hbm2java
*/
@Entity
@Table(name = "user", catalog = "woo0nise")
public class User implements java.io.Serializable {
private Integer id;
private String username;
private String userpwd;
public User() {
}
public User(String username, String userpwd) {
this.username = username;
this.userpwd = userpwd;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "username", nullable = false)
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name = "userpwd", nullable = false)
public String getUserpwd() {
return this.userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
}
3.2、创建Dao层接口以及实现类
IUserDao
package com.test.dao;
import com.test.common.dao.BaseDao;
import com.test.entity.User;
/**
* <p>Title:IUserDao</p>
* <p>Description:</p>
* <p>Company:www.hack-gov.com</p>
* @author 0nise
* @date 2016年12月29日 下午11:41:46
* @Email woo0nise@gmail.com
* @version 1.0
*/
public interface IUserDao extends BaseDao<User> {
}
UserDaoImpl
package com.test.dao.impl;
import org.springframework.stereotype.Repository;
import com.test.common.dao.BaseDaoImpl;
import com.test.dao.IUserDao;
import com.test.entity.User;
/**
* <p>Title:UserDaoImpl</p>
* <p>Description:</p>
* <p>Company:www.hack-gov.com</p>
* @author 0nise
* @date 2016年12月29日 下午11:42:45
* @Email woo0nise@gmail.com
* @version 1.0
*/
@Repository
public class UserDaoImpl extends BaseDaoImpl<User> implements IUserDao {
}
3.3、创建Service层接口以及实现类
UserService
package com.test.service;
import com.test.entity.User;
/**
* <p>Title:UserService</p>
* <p>Description:</p>
* <p>Company:www.hack-gov.com</p>
* @author 0nise
* @date 2016年12月29日 下午11:44:29
* @Email woo0nise@gmail.com
* @version 1.0
*/
public interface UserService {
void register(User user);
}
UserServiceImpl.java
package com.test.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.test.dao.IUserDao;
import com.test.entity.User;
import com.test.service.UserService;
/**
* <p>Title:UserServiceImpl</p>
* <p>Description:</p>
* <p>Company:www.hack-gov.com</p>
* @author 0nise
* @date 2016年12月29日 下午11:45:30
* @Email woo0nise@gmail.com
* @version 1.0
*/
@Service//("userService")
@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT,timeout=5)
public class UserServiceImpl implements UserService {
@Autowired
private IUserDao userDao;
@Override
public void register(User user) {
userDao.save(user);
}
}
3.4、创建Action
UserAction
package com.test.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import com.opensymphony.xwork2.ActionSupport;
import com.test.entity.User;
import com.test.service.UserService;
import com.test.service.impl.UserServiceImpl;
/**
* <p>Title:UserAction</p>
* <p>Description:</p>
* <p>Company:www.hack-gov.com</p>
* @author 0nise
* @date 2016年12月29日 下午11:48:51
* @Email woo0nise@gmail.com
* @version 1.0
*/
public class UserAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = -799685719203073064L;
@Autowired
private UserService userService;
public String register(){
User user = new User("admin25", "admin25");
try {
userService.register(user);
System.out.println("ok");
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
运行结果

工具类
BaseDao.java
package com.test.common.dao;
import java.io.Serializable;
import java.util.List;
/**
* <p>Title:BaseDao</p>
* <p>Description:</p>
* <p>Company:www.hack-gov.com</p>
* @author 0nise
* @date 2016年12月29日 下午11:10:19
* @Email woo0nise@gmail.com
* @version 1.0
*/
public interface BaseDao<T> {
/**
* 获取一个对象
* @author 0nise
* @date 2016年12月29日 下午11:23:48
* @Email woo0nise@gmail.com
* @param entityClazz
* @param id id编号
* @return
*/
T get(Class<T> entityClazz , Serializable id);
/**
* 保存一个对象
* @author 0nise
* @date 2016年12月29日 下午11:24:11
* @Email woo0nise@gmail.com
* @param entity
* @return
*/
Serializable save(T entity);
/**
* 更新对象
* @author 0nise
* @date 2016年12月29日 下午11:24:19
* @Email woo0nise@gmail.com
* @param entity 实体对象
*/
void update(T entity);
/**
* 删除对象
* @author 0nise
* @date 2016年12月29日 下午11:24:32
* @Email woo0nise@gmail.com
* @param entity 实体对象
*/
void delete(T entity);
/**
* 根据id删除对象
* @author 0nise
* @date 2016年12月29日 下午11:24:44
* @Email woo0nise@gmail.com
* @param entityClazz
* @param id id编号
*/
void delete(Class<T> entityClazz , Serializable id);
/**
* 查询该对象集合
* @author 0nise
* @date 2016年12月29日 下午11:24:57
* @Email woo0nise@gmail.com
* @param entityClazz
* @return
*/
List<T> findAll(Class<T> entityClazz);
/**
* 获取该对象的数量
* @author 0nise
* @date 2016年12月29日 下午11:25:12
* @Email woo0nise@gmail.com
* @param entityClazz
* @return
*/
long findCount(Class<T> entityClazz);
/**
* hql查询
* @author 0nise
* @date 2016年12月29日 下午11:25:24
* @Email woo0nise@gmail.com
* @param hql
* @return
*/
List<T> find(String hql);
/**
* 分页查询
* @author 0nise
* @date 2016年12月29日 下午11:25:34
* @Email woo0nise@gmail.com
* @param hql
* @param pageNo
* @param pageSize
* @return
*/
List<T> findByPage(String hql,int pageNo, int pageSize);
}
BaseDao.java
BaseDaoImpl.java
package com.test.common.dao;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
/**
* <p>Title:BaseDaoImpl</p>
* <p>Description:</p>
* <p>Company:www.hack-gov.com</p>
* @author 0nise
* @date 2016年12月29日 下午11:11:50
* @Email woo0nise@gmail.com
* @version 1.0
*/
public class BaseDaoImpl<T> implements BaseDao<T>
{
@Autowired
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory()
{
return this.sessionFactory;
}
/**
* 获取一个对象
* @author 0nise
* @date 2016年12月29日 下午11:23:48
* @Email woo0nise@gmail.com
* @param entityClazz
* @param id id编号
* @return
*/
@SuppressWarnings("unchecked")
public T get(Class<T> entityClazz , Serializable id)
{
return (T)getSessionFactory().getCurrentSession()
.get(entityClazz , id);
}
/**
* 保存一个对象
* @author 0nise
* @date 2016年12月29日 下午11:24:11
* @Email woo0nise@gmail.com
* @param entity
* @return
*/
public Serializable save(T entity)
{
return getSessionFactory().getCurrentSession()
.save(entity);
}
/**
* 更新对象
* @author 0nise
* @date 2016年12月29日 下午11:24:19
* @Email woo0nise@gmail.com
* @param entity 实体对象
*/
public void update(T entity)
{
getSessionFactory().getCurrentSession().update(entity);
}
/**
* 删除对象
* @author 0nise
* @date 2016年12月29日 下午11:24:32
* @Email woo0nise@gmail.com
* @param entity 实体对象
*/
public void delete(T entity)
{
getSessionFactory().getCurrentSession().delete(entity);
}
/**
* 根据id删除对象
* @author 0nise
* @date 2016年12月29日 下午11:24:44
* @Email woo0nise@gmail.com
* @param entityClazz
* @param id id编号
*/
public void delete(Class<T> entityClazz , Serializable id)
{
getSessionFactory().getCurrentSession()
.createQuery("delete " + entityClazz.getSimpleName()
+ " en where en.id = ?0")
.setParameter("0" , id)
.executeUpdate();
}
/**
* 查询该对象集合
* @author 0nise
* @date 2016年12月29日 下午11:24:57
* @Email woo0nise@gmail.com
* @param entityClazz
* @return
*/
public List<T> findAll(Class<T> entityClazz)
{
return find("select en from "
+ entityClazz.getSimpleName() + " en");
}
/**
* 获取该对象的数量
* @author 0nise
* @date 2016年12月29日 下午11:25:12
* @Email woo0nise@gmail.com
* @param entityClazz
* @return
*/
public long findCount(Class<T> entityClazz)
{
List<?> l = find("select count(*) from "
+ entityClazz.getSimpleName());
if (l != null && l.size() == 1 )
{
return (Long)l.get(0);
}
return 0;
}
/**
* hql查询
* @author 0nise
* @date 2016年12月29日 下午11:25:24
* @Email woo0nise@gmail.com
* @param hql
* @return
*/
@SuppressWarnings("unchecked")
public List<T> find(String hql)
{
return (List<T>)getSessionFactory().getCurrentSession()
.createQuery(hql)
.list();
}
/**
* hql查询,条件
* @author 0nise
* @date 2016年12月29日 下午11:25:24
* @Email woo0nise@gmail.com
* @param hql
* @return
*/
@SuppressWarnings("unchecked")
protected List<T> find(String hql , Object... params)
{
Query query = getSessionFactory().getCurrentSession()
.createQuery(hql);
for(int i = 0 , len = params.length ; i < len ; i++)
{
query.setParameter(i + "" , params[i]);
}
return (List<T>)query.list();
}
/**
* 分页查询
* @author 0nise
* @date 2016年12月29日 下午11:25:34
* @Email woo0nise@gmail.com
* @param hql
* @param pageNo
* @param pageSize
* @return
*/
@SuppressWarnings("unchecked")
public List<T> findByPage(String hql,int pageNo, int pageSize)
{
return getSessionFactory().getCurrentSession()
.createQuery(hql)
.setFirstResult((pageNo - 1) * pageSize)
.setMaxResults(pageSize)
.list();
}
/**
* 分页查询,条件
* @author 0nise
* @date 2016年12月29日 下午11:25:34
* @Email woo0nise@gmail.com
* @param hql
* @param pageNo
* @param pageSize
* @return
*/
@SuppressWarnings("unchecked")
protected List<T> findByPage(String hql , int pageNo, int pageSize
, Object... params)
{
Query query = getSessionFactory().getCurrentSession()
.createQuery(hql);
for(int i = 0 , len = params.length ; i < len ; i++)
{
query.setParameter(i + "" , params[i]);
}
return query.setFirstResult((pageNo - 1) * pageSize)
.setMaxResults(pageSize)
.list();
}
}
BaseDaoImpl.java
S2SH框架整合(注解)Struts2+Spring+Hibernate+MySql的更多相关文章
- SSH框架简化(struts2+spring+hibernate)
目的: 通过对ssh框架有了基础性的学习,本文主要是使用注解的方式来简化ssh框架的代码编写. 注意事项: 1.运行环境:Windows 8-64位,Eclipse(开发工具),jdk1.8.0_91 ...
- SSH三大框架的搭建整合(struts2+spring+hibernate)(转)
原文地址:http://blog.csdn.net/kyle0349/article/details/51751913 尊重原创,请访问原文地址 SSH说的上是javaweb经典框架,不能说100% ...
- SSH三大框架的搭建整合(struts2+spring+hibernate)
本文转载自:https://blog.csdn.net/kyle0349/article/details/51751913
- Eclipse下面的Maven管理的SSH框架整合(Struts,Spring,Hibernate)
搭建的环境:eclispe下面的maven web项目 Struts: 2.5.10 Spring: 4.3.8 Hibernate: 5.1.7 .Final MySQL: 5. ...
- SSH(Struts2+Spring+Hibernate)框架搭建流程<注解的方式创建Bean>
此篇讲的是MyEclipse9工具提供的支持搭建自加包有代码也是相同:用户登录与注册的例子,表字段只有name,password. SSH,xml方式搭建文章链接地址:http://www.cnblo ...
- 基于Maven的S2SH(Struts2+Spring+Hibernate)框架搭建
1. 前言 基于Maven的开发方式开发项目已经成为主流.Maven能很好的对项目的层次及依赖关系进行管理.方便的解决大型项目中复杂的依赖关系.S2SH(Struts2+Spring+Hibernat ...
- Spring、Struts2+Spring+Hibernate整合步骤
所使用的Jar包: Hibernate: Spring(使用MyEclipse自动导入框架功能) Struts2: 注解包和MySql驱动包: 1.配置Hibernate和Spring: <be ...
- 整合struts2+spring+hibernate
一.准备struts2+spring+hibernate所须要的jar包: 新建web项目并将jar包引入到project项目中. 二.搭建struts2环境 a.在 ...
- Struts2+Spring+Hibernate 三大框架的合并集成
这次来看看Struts2+Spring+Hibernate三大框架的整合应用,主要是Spring和Hibernate框架的整合,因为前边已经将Strtus2+Spring整合过了基本一样. 首先看一 ...
随机推荐
- svn代码管理的使用工作流程
1. 新建代码库repository. 2. checkout 到workspace. 3. checkin 回 repository. 4. release 一个版本出来(相当于拉出一个branch ...
- margin 如何居中
#id { margin-left: auto; margin-right: auto;width:100px}一定得增加width:100px这一个属性.margin:[N]px auto;widt ...
- nginx 设置进程title
刚好看到nginx设置进程title的源码,因此做一些总结. linux进程实际是以argv[0]处的值来作为进程的title的,因此若需要修改进程的title只需要修改argv[0]处的值即可. 简 ...
- 17.4.3 使用MulticastSocket实现多点广播(4)
17.4.3 使用MulticastSocket实现多点广播(4) 通过UserInfo类的封装,所有客户端只需要维护该UserInfo类的列表,程序就可以实现广播.发送私聊信息等功能.本程序底层通 ...
- 下标脚本(Swift)
下标脚本 可以定义在类(Class).结构体(structure)和枚举(enumeration)这些目标中,可以认为是访问集合(collection),列表(list)或序列(sequence的快捷 ...
- [Unity WWW] 跨域访问解决方法
什么是跨域访问 域(Domain)是Windows网络中独立运行的单位,域之间相互访问则需要建立信任关系(即Trust Relation).信任关系是连接在域与域之间的桥梁.当一个域与其他域建立了信任 ...
- Spring自学教程-声明式事务处理(六)
Spring事务处理分两种: 一.编程式事务:在程序中控制事务开始,执行和提交: 1.1 使用TransactionTemplate, 使用回调函数执行事务,不需要显示开始事务,不需要显示提交事务,但 ...
- 源码解析-knockout源码准备
准备包括心理和资源两方面. 心理 我看过一句话说,当你用一个框架时,不要忙着看一遍使用教程就开始写项目,先去看看框架原理. 这句话我深以为然.现今前端快速发展,很多前端攻城狮都很茫然:框架更新太快了, ...
- RFID射频卡超市购物结算系统
RFID射频卡超市购物结算系统 这段时间在做RFID射频卡超市购物结算系统,这个系统的设想来自于大学研究课题,但是我们在淘宝网上购买设备的时候淘宝店主都认为RF射频技术不好应用在超市购物结算系统,原因 ...
- HDU1429 bfs
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...