SSH整合框架+mysql简单的实现

1. 框架整合原理:

struts2整合Spring 两种:

    一种struts2自己创建Action,自动装配Service ;

    一种 将Action交给Spring管理,通过Bean依赖,注入Service

    **** 引入jar包,改写Struts2 默认对象工厂, 默认StrutsObjectFactory 改为 StrutsSpringObjectFactory

    spring整合hibernate, 将sessionFactory 由Spring管理,将SessionFactory 注入HibernateTemplate , DAO通过HibernateTemplate 操作Hibernate 。

2. 导入jar包(struts2、spring、hibernate框架需要的包及详细说明)

通过这个过程,了解SSH框架所需要的jar包,以及它们的各自用途。下面是我引入的最小jar包的列表:
Struts2   9个

1.struts2-core-2.2.3.jar struts的核心jar包。
2.freemarker-2.3.16.jar Freemarker是struts2默认的模版语言
3.commons-logging-1.1.1.jar Apache  Commons包中的一个,包含了日志功能,必须使用的jar包
4.ognl-3.0.1.jar Struts2默认的表达式语言OGNL:对象图形化导航语言
5.xwork-core-2.2.3.jar Struts2核心包,毕竟struts2很大部分是来webwork6.commons-io-2.0.1.jar 封装了一些输入输出流的常用操作
7.commons-fileupload-1.2.2.jar 用来实现文件上传
8. struts2-json-plugin-2.2.3.jar JSON 插件提供了一个 "json" 结果类型来把 action 序列化成 JSON
9. struts2-spring-plugin-2.1.6.jar 使struts2能集成到spring中

Spring框架 13个

1.org.springframework.aop.jar 包含在应用中使用Spring的AOP特性时所需的类
2. org.springframework.beans.jar 所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。
3.org.springframework.context.support.jar 包含支持缓存Cache(ehcache)、JCA、JMX、邮件服务(Java Mail、COS Mail)、任务计划Scheduling(Timer、Quartz)方面的类。
4.org.springframework.context.jar 为Spring核心提供了大量扩展。可以找到使用Spring ApplicationContext特性时所需的全部类,JDNI所需的全部类,UI方面的用来与模板(Templating)引擎如 Velocity、FreeMarker、 JasperReports集成的类,以及校验Validation方面的相关类。
5.org.springframework.core.jar 包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心。
6.org.springframework.expression.jar Spring表达式语言
7.org.springframework.jdbc.jar 包含对Spring对JDBC数据访问进行封装的所有类。
8.org.springframework.jms.jar 提供了对JMS 1.0.2/1.1的支持类。
9. org.springframework.orm.jar 包含Spring对DAO特性集进行了扩展,使其支持 iBATIS、JDO、OJB、TopLink,因为Hibernate已经独立成包了,现在不包含在这个包里了。这个jar文件里大部分的类都要依赖spring-dao.jar里的类,用这个包时你需要同时包含spring-dao.jar包。
10.org.springframework.transaction.jar 为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和编程式事务管理。
11.org.springframework.web.struts.jar Struts框架支持,可以更方便更容易的集成Struts框架。
12.org.springframework.web.jar 包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入WebApplicationContext特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。
13. org.springframework.asm.jar Spring独立的asm程序, Spring2.5.6的时候需要asmJar 包,3.0开始提供他自己独立的asmJar

Hibernate3框架10个

1.hibernate3.jar 这个是hibernate3.0的核心jar包,必须的,没的选,像我们常用的Session,Query,Transaction都位于这个jar文件中,必要。

2.cglib-2.1.3.jar CGLIB库,Hibernate用它来实现PO字节码的动态生成,非常核心的库,必要。

3.asm.jar      ASM字节码库 如果使用“cglib”则必要,必要

4.asm-attrs.jar     ASM字节码库 如果使用“cglib”则必要,必要
5.ehcache.jar      EHCache缓存 如果没有其它缓存,则必要,必要
6.antlr.jar     语言转换工,Hibernate利用它实现 HQL 到 SQL。 ANother Tool for Language Recognition是一个工具,必要

7.jta.jar JTA规范,当Hibernate使用JTA的时候需要,不过AppServer都会带上,所以也是多余的。但是为了测试方便建议还是带上。必要

8.commons-collections.jar ApacheCommons包中的一个,包含了一些Apache开发的集合类,功能比java.util.*强大。必要
9.dom4j 是一个Java的XMLAPI,类似于jdom,用来读写XML文件的。Hibernate用它来读写配置文件。必要
10.C3P0.jar  C3P0数据库连接池提供数据库连接池

其他4个

1. com.springsource.javax.mail-1.4.0.jar Java email组建,提供email的常用方法
2. mysql-connector-java-5.0.8-bin.jar mysql数据库驱动
3. commons-lang-2.3.jar Apache Commons包中的一个,包含了一些数据类型工具类,是java.lang.*的扩展。必须使用的jar包http://commons.apache.org/lang/
4. log4j-1.2.15.jar 提供日志功能http://logging.apache.org/log4j/

单元测试和其他

junit-4.10.jar

xercesImpl-2.8.1.jar   ---这里用到Base64编码(可以不引入)

*网上下载相关包链接:

Struts2:http://struts.apache.org/2.2.3/index.html
hibernate3:http://sourceforge.net/projects/hibernate/files/hibernate3/
spring3:http://www.springsource.org/download

3. 创建web项目与包结构:

 

4. 配置SSH开发框架

一般Java都是三层架构 数据访问层(dao) 业务逻辑层(biz 或者services) 界面层(ui) action 是业务层的一部分,是一个管理器 (总开关)(作用是取掉转)(取出前台界面的数据,调用biz方法,转发到下一个action或者页面)  模型成(model)一般是实体对象(把现实的的事物变成java中的对象)作用是一暂时存储数据方便持久化(存入数据库或者写入文件)而是 作为一个包裹封装一些数据来在不同的层以及各种java对象中使用  dao是数据访问层 就是用来访问数据库实现数据的持久化(把内存中的数据永久保存到硬盘中)
 
Dao主要做数据库的交互工作 Modle 是模型 存放你的实体类 Service 做相应的业务逻辑处理 Action是一个控制器

1.在web.xml中加入如下代码令服务器自动加载Spring ,Struts,Hibernate

   1:  xml version="1.0" encoding="UTF-8"?>
   2:  web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   3:      xmlns="http://java.sun.com/xml/ns/javaee"
   4:      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   5:      id="WebApp_ID" version="2.5">
   6:      
   7:      listener>
   8:          listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
   9:      listener>
  10:   
  11:      
  12:      context-param>
  13:          param-name>contextConfigLocationparam-name>
  14:          param-value>classpath:config/spring/applicationContext.xmlparam-value>
  15:      context-param>
  16:   
  17:      
  18:      filter>
  19:          filter-name>characterEncodingFilterfilter-name>
  20:          filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
  21:          init-param>
  22:          param-name> encodingparam-name>
  23:          param-value>UTF-8param-value>
  24:          init-param>
  25:      filter>
  26:      filter-mapping>
  27:      filter-name>characterEncodingFilterfilter-name>
  28:      url-pattern>/*url-pattern>
  29:      filter-mapping>
  30:   
  31:   
  32:   
  33:   
  34:      
  35:      filter>
  36:          filter-name>OpenSessionInViewFilterfilter-name>
  37:          filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilterfilter-class>
  38:      filter>
  39:      filter-mapping>
  40:          filter-name>OpenSessionInViewFilterfilter-name>
  41:          url-pattern>/*url-pattern>
  42:      filter-mapping>
  43:   
  44:   
  45:      
  46:      filter>
  47:          filter-name>struts2filter-name>
  48:          filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
  49:          
  50:          init-param>
  51:              param-name>configparam-name>
  52:              param-value>struts-default.xml,struts-plugin.xml,config/struts/struts.xmlparam-value>
  53:          init-param>
  54:      filter>
  55:      filter-mapping>
  56:          filter-name>struts2filter-name>
  57:          url-pattern>/*url-pattern>
  58:      filter-mapping>
  59:   
  60:      display-name>SSH_Unitedisplay-name>

  61:      welcome-file-list>
  62:          welcome-file>index.jspwelcome-file>
  63:      welcome-file-list>
  64:   
  65:   
  66:  web-app>

.csharpcode {
font-size: small; font-family: consolas, "Courier New", courier, monospace; color: black; background-color: #ffffff
}
.csharpcode pre {
font-size: small; font-family: consolas, "Courier New", courier, monospace; color: black; background-color: #ffffff
}
.csharpcode pre {
margin: 0em
}
.csharpcode .rem {
color: #08000
}
.csharpcode .kwrd {
color: #000ff
}
.csharpcode .str {
color: #06080
}
.csharpcode .op {
color: #000c0
}
.csharpcode .preproc {
color: #cc6633
}
.csharpcode .asp {
background-color: #ffff00
}
.csharpcode .html {
color: S00000
}
.csharpcode .attr {
color: #ff0000
}
.csharpcode .alt {
width: 100%; margin: 0em; background-color: #f4f4f4
}
.csharpcode .lnum {
color: O06060
}

2.配置主Spring的配置文件用于加载其实的Spring其它的配置文件:

   1:  xml version="1.0" encoding="UTF-8"?>
   2:  beans xmlns="http://www.springframework.org/schema/beans"
   3:      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
   4:      xmlns:tx="http://www.springframework.org/schema/tx"
   5:      xsi:schemaLocation="http://www.springframework.org/schema/beans 
   6:                                http://www.springframework.org/schema/beans/spring-beans.xsd
   7:                                http://www.springframework.org/schema/tx 
   8:                                http://www.springframework.org/schema/tx/spring-tx.xsd
   9:                             http://www.springframework.org/schema/aop 
  10:                             http://www.springframework.org/schema/aop/spring-aop.xsd">
  11:      
  12:      
  13:      
  14:      import resource="applicationContext-*.xml" />
  15:      
  16:      
  22:      
  23:  beans>

.csharpcode {
font-size: small; font-family: consolas, "Courier New", courier, monospace; color: black; background-color: #ffffff
}
.csharpcode pre {
font-size: small; font-family: consolas, "Courier New", courier, monospace; color: black; background-color: #ffffff
}
.csharpcode pre {
margin: 0em
}
.csharpcode .rem {
color: #08000
}
.csharpcode .kwrd {
color: #000ff
}
.csharpcode .str {
color: #06080
}
.csharpcode .op {
color: #000c0
}
.csharpcode .preproc {
color: #cc6633
}
.csharpcode .asp {
background-color: #ffff00
}
.csharpcode .html {
color: S00000
}
.csharpcode .attr {
color: #ff0000
}
.csharpcode .alt {
width: 100%; margin: 0em; background-color: #f4f4f4
}
.csharpcode .lnum {
color: O06060
}

3.配置  applicationContext-tx.xml 文件用于统一管理事务相关的配置

   1:  xml version="1.0" encoding="UTF-8"?>
   2:  beans xmlns="http://www.springframework.org/schema/beans"
   3:         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   4:         xmlns:aop="http://www.springframework.org/schema/aop"
   5:         xmlns:tx="http://www.springframework.org/schema/tx" 
   6:         xsi:schemaLocation="http://www.springframework.org/schema/beans 
   7:                                http://www.springframework.org/schema/beans/spring-beans.xsd
   8:                                http://www.springframework.org/schema/tx 
   9:                                http://www.springframework.org/schema/tx/spring-tx.xsd
  10:                             http://www.springframework.org/schema/aop 
  11:                             http://www.springframework.org/schema/aop/spring-aop.xsd">
  12:      
  13:      
  14:      
  15:      bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
  16:          
  17:          property name="sessionFactory" ref="sessionFactory">property>
  18:      bean>
  19:      
  20:      
  21:      tx:advice id="txAdvice" transaction-manager="transactionManager">
  22:          
  23:          tx:attributes>
  24:              tx:method name="*" read-only="false" propagation="REQUIRED"/>
  25:              tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
  26:          tx:attributes>
  27:      tx:advice>
  28:      
  29:      
  30:      aop:config>
  31:          aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
  32:      aop:config>
  33:  beans>

  4.配置 applicationContext-aop.xml 文件用于统一管理AOP相关的配置

   1:  xml version="1.0" encoding="UTF-8"?>
   2:  beans xmlns="http://www.springframework.org/schema/beans"
   3:      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
   4:      xmlns:tx="http://www.springframework.org/schema/tx"
   5:      xsi:schemaLocation="http://www.springframework.org/schema/beans 
   6:                                http://www.springframework.org/schema/beans/spring-beans.xsd
   7:                                http://www.springframework.org/schema/tx 
   8:                                http://www.springframework.org/schema/tx/spring-tx.xsd
   9:                             http://www.springframework.org/schema/aop 
  10:                             http://www.springframework.org/schema/aop/spring-aop.xsd">
  11:      
  12:      
  13:      aop:config>
  14:          
  15:          aop:pointcut expression="execution(* com.SSH.service.impl.*.*(..))" id="pt1" />
  16:      aop:config>
  17:  beans>

.csharpcode {
font-size: small; font-family: consolas, "Courier New", courier, monospace; color: black; background-color: #ffffff
}
.csharpcode pre {
font-size: small; font-family: consolas, "Courier New", courier, monospace; color: black; background-color: #ffffff
}
.csharpcode pre {
margin: 0em
}
.csharpcode .rem {
color: #08000
}
.csharpcode .kwrd {
color: #000ff
}
.csharpcode .str {
color: #06080
}
.csharpcode .op {
color: #000c0
}
.csharpcode .preproc {
color: #cc6633
}
.csharpcode .asp {
background-color: #ffff00
}
.csharpcode .html {
color: S00000
}
.csharpcode .attr {
color: #ff0000
}
.csharpcode .alt {
width: 100%; margin: 0em; background-color: #f4f4f4
}
.csharpcode .lnum {
color: O06060
}

  5.配置 applicationContext-jdbc.xml 文件用于统一管理所有和连接数据库相关的配置

   1:  xml version="1.0" encoding="UTF-8"?>
   2:  beans xmlns="http://www.springframework.org/schema/beans"
   3:      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
   4:      xmlns:tx="http://www.springframework.org/schema/tx"
   5:      xsi:schemaLocation="http://www.springframework.org/schema/beans 
   6:                                http://www.springframework.org/schema/beans/spring-beans.xsd
   7:                                http://www.springframework.org/schema/tx 
   8:                                http://www.springframework.org/schema/tx/spring-tx.xsd
   9:                             http://www.springframework.org/schema/aop 
  10:                             http://www.springframework.org/schema/aop/spring-aop.xsd">
  11:      
  12:      
  13:      bean id="sessionFactory"
  14:          class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  15:          
  16:          property name="dataSource" ref="dataSource">property>
  17:          
  18:          property name="hibernateProperties">
  19:              props>
  20:                  prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>
  21:                  prop key="hibernate.show_sql">trueprop>
  22:                  prop key="hibernate.format_sql">falseprop>
  23:                  prop key="hibernate.hbm2ddl.auto">updateprop>
  24:                  prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext
  25:                  prop>
  26:              props>
  27:          property>
  28:          
  29:          property name="mappingLocations">
  30:              array>
  31:                  
  32:                  value>classpath:com/SSH/entity/*.hbm.xmlvalue>
  33:              array>
  34:          property>
  35:      bean>
  36:   
  37:   
  38:      
  39:      bean id="dataSource"
  40:          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  41:          property name="driverClassName" value="com.mysql.jdbc.Driver">property>
  42:          property name="url" value="jdbc:mysql://localhost:3306/SSH_Unite">property>
  43:          property name="username" value="root">property>
  44:          property name="password" value="password">property>
  45:      bean>
  46:  beans>

.csharpcode {
font-size: small; font-family: consolas, "Courier New", courier, monospace; color: black; background-color: #ffffff
}
.csharpcode pre {
font-size: small; font-family: consolas, "Courier New", courier, monospace; color: black; background-color: #ffffff
}
.csharpcode pre {
margin: 0em
}
.csharpcode .rem {
color: #08000
}
.csharpcode .kwrd {
color: #000ff
}
.csharpcode .str {
color: #06080
}
.csharpcode .op {
color: #000c0
}
.csharpcode .preproc {
color: #cc6633
}
.csharpcode .asp {
background-color: #ffff00
}
.csharpcode .html {
color: S00000
}
.csharpcode .attr {
color: #ff0000
}
.csharpcode .alt {
width: 100%; margin: 0em; background-color: #f4f4f4
}
.csharpcode .lnum {
color: O06060
}

6.配置单个指定文件 applicationContext-testservice.xml 文件 统一管理持久层,业务层与动作类

   1:  xml version="1.0" encoding="UTF-8"?>
   2:  beans xmlns="http://www.springframework.org/schema/beans"
   3:      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
   4:      xmlns:tx="http://www.springframework.org/schema/tx"
   5:      xsi:schemaLocation="http://www.springframework.org/schema/beans 
   6:                                http://www.springframework.org/schema/beans/spring-beans.xsd
   7:                                http://www.springframework.org/schema/tx 
   8:                                http://www.springframework.org/schema/tx/spring-tx.xsd
   9:                             http://www.springframework.org/schema/aop 
  10:                             http://www.springframework.org/schema/aop/spring-aop.xsd">
  11:      
  12:      bean id="testDao" class="com.SSH.dao.impl.TestDaoImpl">
  13:          
  14:          property name="sessionFactory" ref="sessionFactory">property>
  15:      bean>
  16:   
  17:      
  18:      bean id="testService" class="com.SSH.service.impl.TestServiceImpl">
  19:          
  20:          property name="testDao" ref="testDao">property>
  21:      bean>
  22:      
  23:      
  24:      
  25:      bean id="testAction" class="com.SSH.action.TestAction" scope="prototype">
  26:          
  27:          property name="testService" ref="testService">property>
  28:      bean>
  29:   
  30:   
  31:  beans>

.csharpcode {
font-size: small; font-family: consolas, "Courier New", courier, monospace; color: black; background-color: #ffffff
}
.csharpcode pre {
font-size: small; font-family: consolas, "Courier New", courier, monospace; color: black; background-color: #ffffff
}
.csharpcode pre {
margin: 0em
}
.csharpcode .rem {
color: #08000
}
.csharpcode .kwrd {
color: #000ff
}
.csharpcode .str {
color: #06080
}
.csharpcode .op {
color: #000c0
}
.csharpcode .preproc {
color: #cc6633
}
.csharpcode .asp {
background-color: #ffff00
}
.csharpcode .html {
color: S00000
}
.csharpcode .attr {
color: #ff0000
}
.csharpcode .alt {
width: 100%; margin: 0em; background-color: #f4f4f4
}
.csharpcode .lnum {
color: O06060
}

7.配置struts.xml文件 统一管理struts的公共配置文件

   1:  xml version="1.0" encoding="UTF-8"?>
   2:  DOCTYPE struts PUBLIC
   3:      "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
   4:      "http://struts.apache.org/dtds/struts-2.3.dtd">
   5:  struts>
   6:      
   7:      
   8:      constant name="struts.devMode" value="true">constant>
   9:   
  10:      
  11:      package name="myDefault" extends="struts-default" abstract="true">
  12:          
  13:      package>
  14:   
  15:      
  16:   
  17:      include file="config/struts/struts-*.xml">include>
  18:  struts>

.csharpcode {
font-size: small; font-family: consolas, "Courier New", courier, monospace; color: black; background-color: #ffffff
}
.csharpcode pre {
font-size: small; font-family: consolas, "Courier New", courier, monospace; color: black; background-color: #ffffff
}
.csharpcode pre {
margin: 0em
}
.csharpcode .rem {
color: #08000
}
.csharpcode .kwrd {
color: #000ff
}
.csharpcode .str {
color: #06080
}
.csharpcode .op {
color: #000c0
}
.csharpcode .preproc {
color: #cc6633
}
.csharpcode .asp {
background-color: #ffff00
}
.csharpcode .html {
color: S00000
}
.csharpcode .attr {
color: #ff0000
}
.csharpcode .alt {
width: 100%; margin: 0em; background-color: #f4f4f4
}
.csharpcode .lnum {
color: O06060
}

8.配置 struts-testservice.xml 文件 动作类配置便于统一管理动作类(TestAction)

   1:  xml version="1.0" encoding="UTF-8"?>
   2:  DOCTYPE struts PUBLIC
   3:      "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
   4:      "http://struts.apache.org/dtds/struts-2.3.dtd">
   5:  struts>
   6:      
   7:      constant name="struts.enable.DynamicMethodInvocation" value="true">constant>
   8:      
   9:      package name="test" extends="myDefault" namespace="/test">
  10:          
  15:          
  16:          action name="testAction_*" class="testAction" method="{1}">
  17:              result name="success" >/success.jspresult>
  18:              result name="input">/index.jspresult>
  19:          action>
  20:   
  21:      package>
  22:  struts>

.csharpcode {
font-size: small; font-family: consolas, "Courier New", courier, monospace; color: black; background-color: #ffffff
}
.csharpcode pre {
font-size: small; font-family: consolas, "Courier New", courier, monospace; color: black; background-color: #ffffff
}
.csharpcode pre {
margin: 0em
}
.csharpcode .rem {
color: #08000
}
.csharpcode .kwrd {
color: #000ff
}
.csharpcode .str {
color: #06080
}
.csharpcode .op {
color: #000c0
}
.csharpcode .preproc {
color: #cc6633
}
.csharpcode .asp {
background-color: #ffff00
}
.csharpcode .html {
color: S00000
}
.csharpcode .attr {
color: #ff0000
}
.csharpcode .alt {
width: 100%; margin: 0em; background-color: #f4f4f4
}
.csharpcode .lnum {
color: O06060
}

9.编写控制器action动作类 TestAction.java类

   1: package com.SSH.action;
   2:  

   3: import com.SSH.entity.User;

   4: import com.SSH.service.ITestService;

   5: import com.opensymphony.xwork2.ActionSupport;

   6: import com.opensymphony.xwork2.ModelDriven;

   7:  

   8: /**

   9:  * Test 动作类

  10:  * 

  11:  * @author HRuinger

  12:  *

  13:  */

  14:  

  15: public class TestAction extends ActionSupport implements ModelDriven {

  16:  

  17:     private static final long serialVersionUID = 1L;

  18:  

  19:     private User user = new User();

  20:  

  21:     private ITestService testService;

  22:  

  23:     public void setTestService(ITestService testService) {

  24:         this.testService = testService;

  25:     }

  26:  

  27:     @Override

  28:     public User getModel() {

  29:         // TODO Auto-generated method stub

  30:         return user;

  31:     }

  32:  

  33:     // 用户登录

  34:     public String login() {

  35:         User exUser = testService.findUser(user);

  36:         if (exUser != null) {

  37:             return SUCCESS;

  38:         } else {

  39:             // 说明错误了

  40:             this.addActionError("亲,用户名或者密码错误");

  41:             return INPUT;

  42:         }

  43:     }

  44:  

  45: }

10.编写Service层 接口ITestService.java

   1: package com.SSH.service;
   2:  

   3: import com.SSH.entity.User;

   4:  

   5: public interface ITestService {

   6:  

   7:     User findUser(User user);

   8:  

   9: }

  11.编写Service层实现类TestServiceImpl.java

   1: package com.SSH.service.impl;
   2:  

   3: import com.SSH.dao.ITestDao;

   4: import com.SSH.entity.User;

   5: import com.SSH.service.ITestService;

   6:  

   7: /**

   8:  * 业务实现层

   9:  * 

  10:  * @author HRuinger

  11:  *

  12:  */

  13:  

  14: public class TestServiceImpl implements ITestService {

  15:  

  16:     private ITestDao testDao;

  17:  

  18:     public void setTestDao(ITestDao testDao) {

  19:         this.testDao = testDao;

  20:     }

  21:  

  22:     @Override

  23:     public User findUser(User user) {

  24:  

  25:         return testDao.find(user);

  26:     }

  27:  

  28: }

  12.编写数据访问层(Dao层)接口 ITestDao.java类

   1: package com.SSH.dao;
   2:  

   3: import com.SSH.entity.User;

   4:  

   5: /**

   6:  * 

   7:  * @author HRuinger

   8:  *

   9:  */

  10:  

  11:  

  12: public interface ITestDao {

  13:  

  14:  

  15:     User find(User user);

  16:  

  17: }

13.编写访问层实现类TestDaoImpl.java

   1: package com.SSH.dao.impl;
   2:  

   3: import java.util.List;

   4:  

   5: import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

   6:  

   7: import com.SSH.dao.ITestDao;

   8: import com.SSH.entity.User;

   9:  

  10: /**

  11:  * 

  12:  * @author HRuinger

  13:  *

  14:  */

  15:  

  16: public class TestDaoImpl extends HibernateDaoSupport implements ITestDao {

  17:  

  18:     @Override

  19:     public User find(User user) {

  20:         String username =user.getUsername();

  21:         String password = user.getPassword();

  22:         String hql = "from User u where  u.username = ? and u.password = ?";

  23:         List list = (List) this.getHibernateTemplate().find(hql, username,password);

  24:         if(list != null && list.size() > 0){

  25:             return list.get(0);

  26:         }

  27:         return null;

  28:     }

  29:  

  30: }

14.编写实体类(模型)Userjava类

   1: package com.SSH.entity;
   2: // Generated 2016-9-26 17:31:58 by Hibernate Tools 5.1.0.Beta1

   3:  

   4: /**

   5:  * User generated by hbm2java

   6:  */

   7: public class User implements java.io.Serializable {

   8:  

   9:     private Integer id;

  10:     private String username;

  11:     private String password;

  12:  

  13:     public User() {

  14:     }

  15:  

  16:     public User(String username, String password) {

  17:         this.username = username;

  18:         this.password = password;

  19:     }

  20:  

  21:     public Integer getId() {

  22:         return this.id;

  23:     }

  24:  

  25:     public void setId(Integer id) {

  26:         this.id = id;

  27:     }

  28:  

  29:     public String getUsername() {

  30:         return this.username;

  31:     }

  32:  

  33:     public void setUsername(String username) {

  34:         this.username = username;

  35:     }

  36:  

  37:     public String getPassword() {

  38:         return this.password;

  39:     }

  40:  

  41:     public void setPassword(String password) {

  42:         this.password = password;

  43:     }

  44:  

  45:     @Override

  46:     public String toString() {

  47:         return "User [id=" + id + ", username=" + username + ", password=" + password + "]";

  48:     }

  49:  

  50: }

15.编写映射文件User.hbm.xml

   1: xml version="1.0"?>
   2: DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

   3: "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

   4: 

   5: hibernate-mapping>

   6:     class name="com.SSH.entity.User" table="user">

   7:         id name="id" type="java.lang.Integer">

   8:             column name="id" />

   9:             generator class="identity" />

  10:         id>

  11:         property name="username" type="string">

  12:             column name="username" length="20" />

  13:         property>

  14:         property name="password" type="string">

  15:             column name="password" length="20" />

  16:         property>

  17:     class>

  18: hibernate-mapping>

16.用到的对应的数据库文件 db_ssh-unite.sql

首先创建ssh_unite数据库:

create database ssh_unite;

然后运行下面语句便可:

   1: /*
   2: Navicat MySQL Data Transfer

   3: 

   4: Source Server         : Mysql

   5: Source Server Version : 50549

   6: Source Host           : localhost:3306

   7: Source Database       : ssh_unite

   8: 

   9: Target Server Type    : MYSQL

  10: Target Server Version : 50549

  11: File Encoding         : 65001

  12: 

  13: Date: 2016-09-29 15:29:54

  14: */

  15:  

  16: SET FOREIGN_KEY_CHECKS=0;

  17:  

  18: -- ----------------------------

  19: -- Table structure for user

  20: -- ----------------------------

  21: DROP TABLE IF EXISTS `user`;

  22: CREATE TABLE `user` (

  23:   `id` int(11) NOT NULL AUTO_INCREMENT,

  24:   `username` varchar(20) DEFAULT NULL,

  25:   `password` varchar(20) DEFAULT NULL,

  26:   PRIMARY KEY (`id`)

  27: ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

  28:  

  29: -- ----------------------------

  30: -- Records of user

  31: -- ----------------------------

  32: INSERT INTO `user` VALUES (‘1‘, ‘aaa‘, ‘aaa‘);

  33: INSERT INTO `user` VALUES (‘2‘, ‘bb‘, ‘bbb‘);

  34: INSERT INTO `user` VALUES (‘3‘, ‘HRuinger‘, ‘1234‘);

 

  效果图片:

 

 

有需要源代码 的请留言,到时 再上传。

 

 

  .csharpcode {
	font-size: small; font-family: consolas, "Courier New", courier, monospace; color: black; background-color: #ffffff
}
.csharpcode pre {
	font-size: small; font-family: consolas, "Courier New", courier, monospace; color: black; background-color: #ffffff
}
.csharpcode pre {
	margin: 0em
}
.csharpcode .rem {
	color: #08000
}
.csharpcode .kwrd {
	color: #000ff
}
.csharpcode .str {
	color: #06080
}
.csharpcode .op {
	color: #000c0
}
.csharpcode .preproc {
	color: #cc6633
}
.csharpcode .asp {
	background-color: #ffff00
}
.csharpcode .html {
	color: S00000
}
.csharpcode .attr {
	color: #ff0000
}
.csharpcode .alt {
	width: 100%; margin: 0em; background-color: #f4f4f4
}
.csharpcode .lnum {
	color: O06060
}
时间: 2024-10-27 09:40:56

SSH整合框架+mysql简单的实现的相关文章

SSH三大框架的搭建整合(struts2+spring+hibernate)(转)

原文地址:http://blog.csdn.net/kyle0349/article/details/51751913  尊重原创,请访问原文地址 SSH说的上是javaweb经典框架,不能说100%要会SSH框架,但是大部分公司都在用,说到框架,都会提到ssh吧,这次就以很简单的注册例子来整合SSH框架.整合框架要注意的是先每个框架单独测通后再整合,不然整合后出现问题比较难排查. 环境:windows + MyEclipse + JDK1.7 + Tomcat7 + MySQL 代码已经测通,

SSH框架的简单搭建登录

一.SSH架构 1.Struts 2 + Spring + Hibernate 2.以Spring作为核心框架,数据持久化使用Hibernate完成,表现层使用Struts 2 3.Spring提供对象管理.面向切面编程等实用功能 4.通过Spring提供的服务简化编码.降低开发难度.提高开发效率 二.需要导入依赖jar SSH整合体验最不好的就是添加jar包,由于三个框架所依赖的jar包非常多,其中有一些jar包可能冲突,我们应该将冲突的jar包,保留高级版本的,删掉低级版本的. 其中stru

mysql+ssh整合例子,附源码下载

项目引用jar下载:http://download.csdn.net/detail/adam_zs/7262727 项目源码下载地址:http://download.csdn.net/detail/adam_zs/7262749 今天花时间把ssh整合了一下,重新再学习一下,希望对大家有所帮助! 我用的是mysql数据库,建表语句比较简单就不贴出来了,建表的时候记的设置id为自动增加哦. 项目文件位置,项目引用jar包 项目配置文件 web.xml <?xml version="1.0&q

SSH三大框架的整合

SSH三个框架的知识点 一.Hibernate框架 1. Hibernate的核心配置文件 1.1 数据库信息.连接池配置 1.2 Hibernate信息 1.3 映射配置 1.4 Hibernate核心配置文件 如果单纯使用Hibernate框架,核心配置文件名称hibernate.cfg.xml并且一定要放在src下面,而hibernate和spring整合的时候,hibernate核心配置文件名称和位置没有固定要求的(因为到时候会有一个参数指定其位置). 2. Hibernate映射配置文

mysql+ssh整合样例,附源代码下载

项目引用jar下载:http://download.csdn.net/detail/adam_zs/7262727 项目源代码下载地址:http://download.csdn.net/detail/adam_zs/7262749 今天花时间把ssh整合了一下,又一次再学习一下,希望对大家有所帮助! 我用的是mysql数据库,建表语句比較简单就不贴出来了,建表的时候记的设置id为自己主动添加?哦. 项目文件位置,项目引用jar包 项目配置文件 web.xml <?xml version="

框架 day37 Spring事务管理,整合web,SSH整合,SSH整合注解

1     事务管理 1.1   回顾事务     事务:一组业务操作,要么全部成功,要么全部不成功.     事务特性:ACID 原子性:整体 一致性:数据(完整) 隔离性:并发(多个事务) 持久性:结果     隔离问题:脏读.不可重复读.幻读(虚读)     隔离级别:4个 readuncommitted 读未提交,存在3个问题. readcommitted 读已提交,解决:脏读:存在2个. repeatableread 可重复读,解决:脏读.不可重复读:存在1个 serializ

SSH整合简单例子

说明:简单SSH整合,struts版本2.3.32,spring版本3.2.9,hibernate版本3.6.10 一.开发步骤 1 引jar包,创建用户library.使用的包和之前博文相同,可以参考spring和hibernate整合,事务管理,struts和spring整合 jar包如下 //struts_core Struts2.3.32core\commons-fileupload-1.3.2.jar Struts2.3.32core\commons-io-2.2.jar Struts

SSH三大框架注解整合(一)

1.导入jar包,ssh的jar包一共是38个,此时还需要多加一个包,就是struts的注解插件jar. 2.在web.xml文件中配置struts filter 和spring 的listener.代码如下: <!-- spring 监听器 -->  <context-param>   <param-name>contextConfigLocation</param-name>   <param-value>classpath:applicat

SSH三大框架注解整合(二)

5.使用spring注解注入service,DAO action: @ParentPackage(value = "struts-default") @Namespace("/") @Controller @Scope("prototype") public class BookAction extends ActionSupport implements ModelDriven<Book>{ //模型驱动 public Book b