ssh注解整合
ssh注解整合
导入java包
配置struts2环境
1. 创建struts.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="user" extends="struts-default" namespace="/">
<action name="user_*" class="userAction" method="{1}">
</action>
</package>
</struts>
2. 配置web.xml
<!-- 配置struts2过滤器 --> <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>
3. 创建SuperAction
public class SuperAction extends ActionSupport
implements ServletRequestAware, ServletResponseAware, ServletContextAware {
private static final long serialVersionUID = 1L;
HttpServletRequest request;// 请求对象
HttpServletResponse response;// 相应对象
HttpSession session;// 会话对象
ServletContext application;// 全局对象
public void setServletContext(ServletContext application) {
this.application = application;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
this.session = this.request.getSession();
}
}
4. 创建UserAction
public class UserAction extends SuperAction{
public String execute() throws Exception {
return NONE;
}
}
搭建hibernate环境
1. 创建实体类
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue
private Integer id;
private String username;
private String password;
//get set toString 省略
}
2. 创建hibernate核心映射文件
<!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. --> <!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> <!-- 数据库的方言:根据底层的数据库生成不同的SQL --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 配置显示SQL --> <property name="hibernate.show_sql">true</property> <!-- 配置格式化SQL --> <property name="hibernate.format_sql">true</property> <!-- 配置hbm2ddl --> <property name="hibernate.hbm2ddl.auto">update</property> </session-factory> </hibernate-configuration>
搭建Spring环境
1. 配置spring核心配置文件
<?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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置druid连接池 -->
<!-- 略 -->
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.ityuhao"></context:component-scan>
<!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
<!-- 指定对哪个实体类进行映射配置 -->
<property name="packagesToScan" value="com.ityuhao.domain"></property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 配置hibernate模板对象 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<!-- 注入sessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
2. 配置监听器
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:bean.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
创建action、service和dao,完成注入
1. action对象创建,注入service
@Component(value="userAction")
@Scope(value="prototype")
public class UserAction extends SuperAction{
private static final long serialVersionUID = 1L;
@Resource(name="userService")
private UserService userService;
public String execute() throws Exception {
userService.add();
return NONE;
}
}
2. service对象创建,注入dao
@Service
@Transactional
public class UserService {
@Resource(name = "userDAO")
private UserDAO userDAO;
public void add() {
// TODO Auto-generated method stub
userDAO.add();
}
}
3. dao注入hibernate模板对象
@Repository
public class UserDAO {
@Resource(name="hibernateTemplate")
private HibernateTemplate hibernateTemplate;
public void add() {
User user = new User();
user.setUsername("admin");
user.setPassword("admin");
hibernateTemplate.save(user);
}
}
ssh注解整合的更多相关文章
- Spring+Hibernate+Struts(SSH)框架整合
SSH框架整合 前言:有人说,现在还是流行主流框架,SSM都出来很久了,更不要说SSH.我不以为然.现在许多公司所用的老项目还是ssh,如果改成流行框架,需要成本.比如金融IT这一块,数据库dao层还 ...
- SSH框架整合的其它方式
--------------------siwuxie095 SSH 框架整合的其它方式 1.主要是整合 Spring 框架和 Hibernate 框架时,可以不写 Hibernate 核心配置文件: ...
- SSH框架整合过程总结
---------------------siwuxie095 SSH 框架整合过程总结 (一)导入相关 jar 包(共 ...
- SSH 框架整合总结
1. 搭建Struts2 环境 创建 struts2 的配置文件: struts.xml; 在 web.xml 中配置 struts2 的核心过滤器; // struts.xml <?xml v ...
- FreeMarker与SSH项目整合流程
FreeMarker与SSH项目整合流程 学习了SSH之后,一般为了减少数据库的压力,会使用FreeMarker来生成静态HTML页面.下面简单说一下FreeMarker与SSH项目的整合全过程~ 前 ...
- SSH框架整合
SSH框架整合 一.原理图 action:(struts2) 1.获取表单的数据 2.表单的验证,例如非空验证,email验证等 3.调用service,并把数据传递给service Service: ...
- SSH项目整合教学Eclipse搭建SSH(Struts2+Spring3+Hibernate3)
这篇博文的目的 尝试搭建一个完整的SSH框架项目. 给以后的自己,也给别人一个参考. 读博文前应该注意: 本文提纲:本文通过一个用户注册的实例讲解SSH的整合.创建Struts项目,整合Hiberna ...
- dwr与ssh框架整合教程
(1)dwr与ssh框架整合教程dwr框架介绍. DWR(Direct Web Remoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开 发人员开发包含AJ ...
- 基于注解整合struts2与spring的时候如果不引入struts2-spring-plugin包自动装配无效
基于注解整合struts2与spring的时候如果不引入struts2-spring-plugin包,自动装配将无效,需要spring注入的对象使用时将抛出空指针异常(NullPointerExcep ...
随机推荐
- Servlet--表单、超链接、转发、重定向4种情况的路径
Servlet中相对路径总结 假设web工程使用如下目录结构: 在介绍相对路径和绝对路径前需要先了解几个概念: 服务器的站点根目录:以tomcat服务器为例,tomcat服务器站点根目录就是apach ...
- 关于GPL的一些知识
1.什么是GPL GPL许可协议(GNU General Public License):只要软件中包含有其他GPL协议的产品或代码,那么该软件就必须也采用GPL许可协议且开源及免费.具有以下特点: ...
- 6、SQL Server 数据查询
一.使用SELECT检索数据 数据查询是SQL语言的中心内容,SELECT 语句的作用是让数据库服务器根据客户要求检索出所需要的信息资料,并按照规定的格式进行整理,返回给客户端. SELECT 语句的 ...
- vc 获取网络时间
方式1 : #include <WinSock2.h> #include <Windows.h> #pragma comment(lib, "WS2_32" ...
- rabbitMQ学习(三)
订阅/广播模式 发送端: import java.io.IOException; import com.rabbitmq.client.ConnectionFactory; import com.ra ...
- 两个shell脚本
脚本1停止lampp #!/bin/bash #set -xv 开启调试模式? count=`ps -ef|grep lampp|grep -v "grep"|wc -l` # ...
- centos环境自动化批量安装软件脚本
自动化安装jdk软件部署脚本 准备工作: 1.在执行脚本的服务器上生成免密码公钥: 安装expect命令 yum install -y expect ssh-keygen 三次回车 2.将jdk-7u ...
- VUE 入门基础(8)
十,组件 使用组件 注册 可以通过以下这种方式创建一个Vue实例 new Vue({ el: '#some-element', }) 注册一个全局组件,你可以使用Vue.component(tagNa ...
- css 隐藏超长的文本!!!
overflow:hidden; text-overflow:ellipsis;white-space: nowrap; 一起使用!
- 正则表达式提取string 中的表名
简单版本: Regex reg = new Regex(@"(?i)\bfrom\b(?![^\[\]]*\])\s+(\[[^\[\]]+\]|\S+)"); MatchColl ...