ssh_整合总结
开场白:首先,我先帮大家整理一下思路
准备:
数据库,表,数据
jar 包准备
Hibernate 基本jar 包
C3p0 数据库连接池
Spring AOP 基本包
Spring Ioc 基本包
Spring 事务控制
Spring 整合 junit 用户方便测试
Spring 整合Struts 包
Spring-annotation 包
Spring的json插件包
Struts基本jar包
Spring 整合Hibernate
创建实体类
书写Service 层和Dao层
编写Spring的配置
配置IOC注解
配置Hibernate模板和事务控制
测试Spring和Hibernate是否成功
Spring 整合Struts2
配置前端控制器
书写action
测试
OK,思路屡一下后,下面开始书写详细的步骤,(注意:常用的方法和数据库的创建省略 。。。代码省略,自己写测试)
创建数据库 。。。
创建表 。。。
插入测试数据。。。
前端UI界面 。。。
----------------------------------------------------------------------------------------------------------------------------------
Spring 整合Hibernate
导入Hibernate 和 Spring 以及相关 jar 包





一、创建实体类和映射配置,使用注解 。。。
@Entity //实体类
@Table(name = "数据库表名")
public class Customer{
@Id //主键
@Column(name = "对应数据库中的字段名")
@GeneratedValue(strategy=GenerationType.IDETITY)//主键自增策略,IDETITY = MySql的自增,AUTO = 自适应<<<注意:
如需使用UUID 需要另外一个注解配合使用并且主键类型需要时String类型的:
@GenericGenerator(name = "Myuuid",strategy = "uuid")
@GeneratedValue(generator = "Myuuid")>>>
private Integer 变量名,最好与数据库字段名对应
@Column(name = "对应数据库中的字段名")
private Integer 变量名,最好与数据库字段名对应
。。。
}
二、书写Service 和Dao层 。。。
三、编写Spring的配置文件(applicationContext.xml)本次使用的配置我会加入步数,没有加入步数的本步骤不使用
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
"> <!-- 第一步:指定spring在创建容器时要扫描的包 -->
<context:component-scan base-package="cn.ibbidream"></context:component-scan> <!-- 第二步:管理hibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- 配置事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- 配置通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"/>
<tx:method name="query*" propagation="SUPPORTS" read-only="true"></tx:method>
</tx:attributes>
</tx:advice> <!-- 配置AOP -->
<aop:config>
<aop:pointcut id="pt1" expression="execution(* cn.ibbidream.service.impl.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
</aop:config> <!-- 第三步:管理SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 第四步:配置管理数据库连接池 -->
<property name="dataSource" ref="dataSource"></property> <!-- 第六步:配置数据库其他信息 -->
<property name="hibernateProperties">
<props>
<!-- 配置数据库方言 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- 配置是否在控制台打印sql语句 -->
<prop key="hibernate.show_sql">true</prop>
<!-- 配置是否格式化输出sql语句 -->
<prop key="hibernate.format_sql">true</prop>
<!-- 配置生成ddl语句的方式 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property> <!-- 第七步:配置hibernate的映射文件的注解扫描 -->
<property name="packagesToScan">
<array>
<value>cn.ibbidream.entity</value>
</array>
</property>
</bean> <!-- 第五步:管理dataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/crm"></property>
<property name="user" value="root"></property>
<property name="password" value="jiajia771"></property>
</bean> </beans>
四、在Service和Dao层中配置AOP注解(Aspect Oriented Programming 面向切面编程。解耦是程序员编码开发过程中一直追求的。AOP也是为了解耦所诞生。) AOP 主要是利用代理模式的技术来实现的。
@Service("customerService")//声明这是一个Service 层的装配Bean//看不懂的 自行百度
@Controller//控制层 视图层
@Repository//持久层
@Autowired //自动对象 注入
@Qualifier("对象注解名称")//如果对象名和属性名一样 可以省略此写法
五、配置Hibernate模板和事务控制(applicationContext.xml)本次使用的配置我会加入步数,没有加入步数的本步骤不使用(其他在上面的配置已经配置完成)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
"> <!-- 指定spring在创建容器时要扫描的包 -->
<context:component-scan base-package="cn.ibbidream"></context:component-scan> <!-- 管理hibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- 第一步:配置事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- 第二步:配置通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"/>
<tx:method name="query*" propagation="SUPPORTS" read-only="true"></tx:method>
</tx:attributes>
</tx:advice> <!-- 第三步:配置AOP -->
<aop:config>
<aop:pointcut id="pt1" expression="execution(* cn.ibbidream.service.impl.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
</aop:config> <!-- 管理SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 配置管理数据库连接池 -->
<property name="dataSource" ref="dataSource"></property> <!-- 配置数据库其他信息 -->
<property name="hibernateProperties">
<props>
<!-- 配置数据库方言 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- 配置是否在控制台打印sql语句 -->
<prop key="hibernate.show_sql">true</prop>
<!-- 配置是否格式化输出sql语句 -->
<prop key="hibernate.format_sql">true</prop>
<!-- 配置生成ddl语句的方式 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property> <!-- 配置hibernate的映射文件的注解扫描 -->
<property name="packagesToScan">
<array>
<value>cn.ibbidream.entity</value>
</array>
</property>
</bean> <!-- 管理dataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/crm"></property>
<property name="user" value="root"></property>
<property name="password" value="jiajia771"></property>
</bean> </beans>
六、测试,书写main 方法测试
----------------------------------------------------------------------------------------------------------------------------------------------------------
Spring 整合Struts2
导包



一、配置前端控制器(web.xml)
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_1.xsd"
version="2.5"> <!-- 配置前端控制器整合 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!-- 配置Struts 前端控制器 -->
<filter>
<filter-name>Struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>struts.devMode</param-name>
<param-value>true</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
二、发送请求获取数据
127.0.0.1:8080/customer/queryCustomer
三、编写action(动作类必须以action、actions、struts、struts2为结尾的包下)
@Controller//上面解释过了
@Scope("prototype")//配置为多例模式
@ParentPackage("struts-default")//继承父类
@Namespace(“/customer”)//配置名称空间
@Autowired//自动类型注入
package cn.ibbidream.web.action; import cn.ibbidream.entity.Customer;
import cn.ibbidream.service.CustomerService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.*;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller; import java.util.HashMap;
import java.util.List;
import java.util.Map; @Namespace("/customer")
@Controller
@ParentPackage(value = "json-default")
@Scope(value = "prototype")
public class CustomerAction extends ActionSupport{ @Autowired
@Qualifier(value = "customerService")
private CustomerService customerService; @Action(value = "CustomerlistCustomer",results = {@Result(name = "customerlistCustomer",type = "json")})
public String CustomerlistCustomer(){ DetachedCriteria dc = DetachedCriteria.forClass(Customer.class); //业务流程
/*
* 将数据转换成datagrid所需的格式需要两个key:
* 1、total:数据的总记录数
* 2、rows:所有客户的集合
*/
//查询所有客户的总记录数
//统计查询是一种特殊的投影查询
dc.setProjection(Projections.rowCount());
Long count = customerService.queryCountCustomer(dc); List<Customer> customers = customerService.queryListCustomer(dc); Map<String, Object> map = new HashMap<>();
map.put("total",count);
map.put("rows",customers); System.out.println(map.toString()); ActionContext.getContext().getValueStack().push(map);
return "customerlistCustomer";
} }
四、因为我的前端用的esayUi 写的,所以需要返回json 格式的数据(3.5. 将数据转换成json格式)
1、导入json的插件包
2、修改继承的包
3、将数据封装成所需要的格式(即封装到一个map中)
4、将map压入栈顶
5、返回的结果集类型如图

将ParentPackage 继承json-default、json-default继承struts-default ,我上边已经改好了
将返回集类型改为json,三种也做好了
书写一个方法可以查询返回数据的个数的
测试,至此案例完成(注意,本篇文章仅仅帮助初学者理思路的,如果完全按照此文章书如果是小白级别可能写不出来)
如有大神看出错误所在,请在评论区指正
ssh_整合总结的更多相关文章
- SSH_框架整合6--修改Edit员工信息
SSH_框架整合6--修改Edit员工信息 1 加上修改Edit键 (1)emp-list.jsp <td> <a href="emp-input?id=${id }&qu ...
- SSH_框架整合5--验证用户名是否可用
SSH_框架整合5--验证用户名是否可用 1 emp-input.jsp中编写ajax验证用户名是否可用: <script type="text/javascript" SR ...
- SSH_框架整合4--添加员工信息
SSH_框架整合4--添加员工信息 一. 1 index.jsp:添加:<a href="emp-input">添加员工向信息:Add Employees' Infor ...
- SSH_框架整合2—查询显示
4. 完成功能. (1)com.atguigu.ssh.actions包下新建EmployeeAction.java package com.atguigu.ssh.actions; import j ...
- SSH_框架整合1
1 WEB环境下配置Spring 因为是在WEB环境中应用Spring,所以要先配置web.xml: (1)WebContent-WEB-INF-lib包中,加入Spring包下的required ...
- SSH_框架整合7--整个项目CODE
一 架构 1Action类 2 配置文件 3 View页面 二 Code 1 src (1)com.atguigu.ssh.actions >EmployeeAction.java packa ...
- SSH_框架整合3-删除
一.普通删除 1 完善src中 类: (1)EmployeeDao.java中: //2 删除 public void delete(Integer id){ String hql="DEL ...
- [原创]mybatis中整合ehcache缓存框架的使用
mybatis整合ehcache缓存框架的使用 mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓 ...
- kindeditor4整合SyntaxHighlighter,让代码亮起来
这一篇我将介绍如何让kindeditor4.x整合SyntaxHighlighter代码高亮,因为SyntaxHighlighter的应用非常广泛,所以将kindeditor默认的prettify替换 ...
随机推荐
- 使用WCF进行跨平台开发之一(WCF的实现、控制台托管与.net平台的调用)
WCF是Windows Communication Foundation的缩写,是微软发展的一组数据通信的应用程序开发接口,它是.NET框架的一部分,是WinFx的三个重要开发类库之一,其它两个是WP ...
- RESTful设计原则和样例(开发前后台接口)
摘要 REST(表征性状态传输)设计风格;REST通常基于使用HTTP,URI协议和标准.使用URL标识资源,开发前后台接口.主要使用post,get方式 参考博文: http://www.cnblo ...
- 修改select默认样式
http://www.qkzone.com/code/2015-11-26/1.html
- C++11系列-什么是C++11
什么是C++0x? C++0x是C++最新标准标准化过程中的曾用名,在这一系列文章中我们将介绍最新标准添加的一系列新的语言特性.在2011年9月份,C++0x正式由官方发布并命名C++11,现在很多编 ...
- 57.部门职位管理 ExtJs 展示
1.jobInfo.jsp <%@ page language="java" pageEncoding="UTF-8"%> <script t ...
- handbook/CentOS/使用免费SSL证书让网站支持HTTPS访问.md
- ArgumentError: You need to supply at least one validatio
创建: 2017/10/02 意思: validate没有内容 例: validates :title
- 洛谷P1330 封锁阳光大学(二分图染色)
P1330 封锁阳光大学 题目描述 曹是一只爱刷街的老曹,暑假期间,他每天都欢快地在阳光大学的校园里刷街.河蟹看到欢快的曹,感到不爽.河蟹决定封锁阳光大学,不让曹刷街. 阳光大学的校园是一张由N个点构 ...
- [Swift通天遁地]八、媒体与动画-(6)使用开源类库快速实现滑入动画
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- glances内存分析工具使用
glances -b 以字节为单位显示网络流量 glances 是一个命令行工具包括如下命令选项:-b:显示网络连接速度 Byte/ 秒-B @IP|host :绑定服务器端 IP 地址或者主机名称- ...