SSH整合框架+mysql簡單的實現css
struts2整合Spring 兩種: html
一種struts2本身建立Action,自動裝配Service ; java
一種 將Action交給Spring管理,經過Bean依賴,注入Service mysql
**** 引入jar包,改寫Struts2 默認對象工廠, 默認StrutsObjectFactory 改成 StrutsSpringObjectFactory web
spring整合hibernate, 將sessionFactory 由Spring管理,將SessionFactory 注入HibernateTemplate , DAO經過HibernateTemplate 操做Hibernate 。spring
經過這個過程,瞭解SSH框架所須要的jar包,以及它們的各自用途。下面是我引入的最小jar包的列表:
Struts2 9個 sql
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個 express
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 apache
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
通常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.ContextLoaderListener listener-class>
9: listener>
10:
11:
12: <context-param>
13: <param-name>contextConfigLocation param-name>
14: <param-value>classpath:config/spring/applicationContext.xml param-value>
15: context-param>
16:
17:
18: <filter>
19: <filter-name>characterEncodingFilter filter-name>
20: <filter-class>org.springframework.web.filter.CharacterEncodingFilter filter-class>
21: <init-param>
22: <param-name> encoding param-name>
23: <param-value>UTF-8 param-value>
24: init-param>
25: filter>
26: <filter-mapping>
27: <filter-name>characterEncodingFilter filter-name>
28: <url-pattern>/* url-pattern>
29: filter-mapping>
30:
31:
32:
33:
34:
35: <filter>
36: <filter-name>OpenSessionInViewFilter filter-name>
37: <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter filter-class>
38: filter>
39: <filter-mapping>
40: <filter-name>OpenSessionInViewFilter filter-name>
41: <url-pattern>/* url-pattern>
42: filter-mapping>
43:
44:
45:
46: <filter>
47: <filter-name>struts2 filter-name>
48: <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter filter-class>
49:
50: <init-param>
51: <param-name>config param-name>
52: <param-value>struts-default.xml,struts-plugin.xml,config/struts/struts.xml param-value>
53: init-param>
54: filter>
55: <filter-mapping>
56: <filter-name>struts2 filter-name>
57: <url-pattern>/* url-pattern>
58: filter-mapping>
59:
60: <display-name>SSH_Unite display-name>
61: <welcome-file-list>
62: <welcome-file>index.jsp welcome-file>
63: welcome-file-list>
64:
65:
66: web-app>
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>
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>
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.MySQLDialect prop>
21: <prop key="hibernate.show_sql">true prop>
22: <prop key="hibernate.format_sql">false prop>
23: <prop key="hibernate.hbm2ddl.auto">update prop>
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.xml value>
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>
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>
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>
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.jsp result>
18: <result name="input">/index.jsp result>
19: action>
20:
21: package>
22: struts>
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: }
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: Listlist = (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>
<-- 1四、15 兩步能夠用eclipse的插件來生成對應的文件,依據對應的數據庫 -->
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');
效果圖片:
有須要源代碼 的請留言,到時 再上傳。