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整合过了基本一样. 首先看一 ...
随机推荐
- pe and elf
http://staff.ustc.edu.cn/~sycheng/sst/exp_crack/ELF.pdf https://refspecs.linuxbase.org/elf/TIS1.1.pd ...
- appium通过WiFi连接真机进行测试
http://www.th7.cn/Program/Android/201507/514602.shtml appium通过WiFi连接真机进行测试 2015-07-24 19:43:07CSDN ...
- c语言 inline函数
大学在教科书上学习过inline函数,定义为inline函数之后,会省去函数调用的开销,直接嵌套汇编代码,取代函数调用,提高效率. google的google c++ style guide 1.in ...
- ID3决策树预测的java实现
刚才写了ID3决策树的建立,这个是通过决策树来进行预测.这里主要用到的就是XML的遍历解析,比较简单. 关于xml的解析,参考了: http://blog.csdn.net/soszou/articl ...
- HDU 5650 so easy
n不为1的时候输出a[1],否则输出0 #include<cstdio> #include<cstring> #include<cmath> #include< ...
- 【转】每一个程序员需要了解的10个Linux命令
作为一个程序员,在软件开发职业生涯中或多或少会用到Linux系统,并且可能会使用Linux命令来检索需要的信息.本文将为各位开发者分享10个有用的Linux命令,希望对你会有所帮助. 以下就是今天我们 ...
- 自然语言处理高手_相关资源_开源项目(比如:分词,word2vec等)
(1) 中科院自动化所的博士,用神经网络做自然语言处理:http://licstar.net (2) 分词项目:https://github.com/fxsjy/jieba(3) 清华大学搞的中文分词 ...
- Heka GeoIpDecoder 配置
Prepare: 安装geoip-api-c,确保/usr/include/GeoIP.h存在: 源码编译安装Heka (容易出现问题): 下载GeoLiteCity.dat数据库. 配置文件举例: ...
- java系列--HTTP协议
一.HTTP请求信息 请求行 请求头 空行 消息体 1.防盗链: 枚举类型: 二.中文乱码问题 1.Get提交 String username = request.getParameter(" ...
- iOS workspace 依次编译多个工程
目的:当我封装一个framework的时候,需要检验当前的改动,但是又不想编译完framework,又要编译测试工程. 步骤: 1. 选择测试工程 2. Edit Scheme 3. 选中Build- ...