开场白:首先,我先帮大家整理一下思路

    准备:

      数据库,表,数据 

      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_整合总结的更多相关文章

  1. SSH_框架整合6--修改Edit员工信息

    SSH_框架整合6--修改Edit员工信息 1 加上修改Edit键 (1)emp-list.jsp <td> <a href="emp-input?id=${id }&qu ...

  2. SSH_框架整合5--验证用户名是否可用

    SSH_框架整合5--验证用户名是否可用 1 emp-input.jsp中编写ajax验证用户名是否可用: <script type="text/javascript" SR ...

  3. SSH_框架整合4--添加员工信息

    SSH_框架整合4--添加员工信息 一. 1 index.jsp:添加:<a href="emp-input">添加员工向信息:Add Employees' Infor ...

  4. SSH_框架整合2—查询显示

    4. 完成功能. (1)com.atguigu.ssh.actions包下新建EmployeeAction.java package com.atguigu.ssh.actions; import j ...

  5. SSH_框架整合1

    1 WEB环境下配置Spring   因为是在WEB环境中应用Spring,所以要先配置web.xml: (1)WebContent-WEB-INF-lib包中,加入Spring包下的required ...

  6. SSH_框架整合7--整个项目CODE

    一 架构 1Action类 2 配置文件 3 View页面 二  Code 1 src (1)com.atguigu.ssh.actions >EmployeeAction.java packa ...

  7. SSH_框架整合3-删除

    一.普通删除 1 完善src中 类: (1)EmployeeDao.java中: //2 删除 public void delete(Integer id){ String hql="DEL ...

  8. [原创]mybatis中整合ehcache缓存框架的使用

    mybatis整合ehcache缓存框架的使用 mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓 ...

  9. kindeditor4整合SyntaxHighlighter,让代码亮起来

    这一篇我将介绍如何让kindeditor4.x整合SyntaxHighlighter代码高亮,因为SyntaxHighlighter的应用非常广泛,所以将kindeditor默认的prettify替换 ...

随机推荐

  1. css样式之vertical-align垂直居中的应用

    css样式之vertical-align垂直居中的应用:将图片垂直左右居中 元素垂直居中 1:必须给容器父元素加上text-align:center 2:必须给当前元素转换成行内块元素,display ...

  2. linux top 的用法

    本篇博文主要讲解有关top命令,top命令的主要功能是查看进程活动状态以及一些系统状况. TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户终 ...

  3. 《Typecript 入门教程》 2、访问控制符:public、private、protected、readonly

    声明类的属性和方法时可以设置使用访问控制符,访问控制符设置类的属性和方法能不能在类的外部被访问 1. 默认为 public,使用public定义的属性和方法在类的内部和外部都可以访问 2. priva ...

  4. 347 Top K Frequent Elements 前K个高频元素

    给定一个非空的整数数组,返回其中出现频率前 k 高的元素.例如,给定数组 [1,1,1,2,2,3] , 和 k = 2,返回 [1,2].注意:    你可以假设给定的 k 总是合理的,1 ≤ k ...

  5. scala的枚举

    package com.test.scala.test /** * 枚举 */ object Enum extends Enumeration { val Red,Yellow,Green=Value ...

  6. SublimeText学习(二)-基本操作

    1.查看已安装的插件 看到已经安装的插件,看到了在上一篇中安装的Emmet 2.设置主题与字体 方法一: 方法二: 工具栏中 [Preference-浏览程序包]找到[Default文件夹]-用Sub ...

  7. J2EE集群原理(摘录)

    J2EE集群原理 什么是集群呢?总的来说,集群包括两个概念:“负载均衡”(load balancing)和“失效备援”(failover)  图一:负载均衡 多个客户端同时发出请求,位于前端的负载均衡 ...

  8. PHP面相对象中的重载与重写

    重写Overriding是父类与子类之间多态性的一种表现,重载Overloading是一个类中多态性的一种表现.Overloaded的方法是可以改变返回值的类型.也就是说,重载的返回值类型可以相同也可 ...

  9. 01--TCP状态转换

    参考大牛文章: http://www.cnblogs.com/qlee/archive/2011/07/12/2104089.html

  10. 控制台——对WIN32 API的使用(user32.dll)

    Win32 API概念:即为Microsoft 32位平台的应用程序编程接口(Application Programming Interface).所有在Win32平台上运行的应用程序都可以调用这些函 ...