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整合过了基本一样. 首先看一 ...
随机推荐
- egret GUI 文本混排+文本链接的聊天解决方案【取巧法】
ui方面: <e:Scroller verticalScrollPolicy="auto" width="468" height="620&qu ...
- Mysql集群读写分离(Amoeba)
Amoeba原理戳这里:Amoeba详细介绍 实验环境 Master.Amoeba--IP:192.168.1.5 Slave---IP:192.168.1.10 安装JDK JDK下载地址:http ...
- Cocos2dx 学习笔记整理----第一个项目
接上一节, 进入新建的项目下面的proj.win32下面,找到项目名.sln文件(我的是game001.sln),双击会通过VS2010打开.(当然,你装了VS什么版本就是什么版本) 将你的项目设为启 ...
- unity3d热更新解决方案,使用ulua插件开发的框架。
ulua插件下载地址 www.ulua.org,下面要说的是ulua的开发框架. 首先是 LuaLoader 类,它负责把一个 lua 的 table 加载起来,使此 lua 的 table 像一个 ...
- iOS开发——应用图标上显示消息数量
iOS8以前: UIApplication *app = [UIApplication sharedApplication]; app.applicationIconBadgeNumber = num ...
- sqlserver存储过程中,set rowcount 0是什么意思?
一般在语句中使用set rowcount是为了使后续的查询.更新.删除操作只影响指定的行数比如 一起执行如下语句 set rowcount 1SELECT * FROM sysobjects结果只返回 ...
- 如何在微软Hyper-V下发挥SQL Server最大功效
要建设稳定运行的虚拟化SQL Server系统,关键是确保虚拟化管理软件配置能提供数据库所需的资源.SQL Server是CPU密集型技术,因此支撑它的虚拟机需要能获得充足的处理器资源,同时不能引起与 ...
- Web开发中需要了解的东西
在StackExchange上有人问了这样一个问题:What should every programmer know about web development?(关于Web开发,什么是所有程序员需 ...
- matlab unique 顺序不变
对于一个向量,使用unique去重后会自动排序,为了保持原顺序: A = [5,1,8,5,2,8,3,9,6,1];[i,j] = unique(A,'first');B = A(sort(j)); ...
- UIAlertController高级之嵌入其他控件
在编码过程中,我们经常遇到需要这样一个效果,就是弹出框的嵌套; 举个最简单的例子,比如你要选择时间,必然需要一个时间选择器DatePicker.但是这个选择器又是在你点击某按钮时弹出,弹出方式最常见的 ...