SSH框架搭建教程
- 新建一個maven專案
- 開啟pom.xml檔案配置基礎依賴
<!-- 引用junit依賴 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- 引用
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<!-- 配置hibernate依賴包 -->
<dependency>
<
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!-- 配置mysql驅動包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId
<version>5.1.43</version>
</dependency>
- 建立所需要的包
- 建立資料庫表
在MySQL中建立資料庫表:
Create table t_user( user_id int primary key auto_increment, user_name varchar(50), pass_word varchar(50) ); |
新增一條資料:
insert into t_user(user_name,pass_word) values('admin',MD5('123')); |
- 建立對應實體類和其對應的實體類
- 配置hibernate.cfg.xml核心配置檔案
- 進行測試,成功進行下一步
整合Spring
- 引入依賴spring
<!-- spring依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
2.配置Spring核心配置檔案:applicationContext.xml
對藍色的部分進行修改
替換程式碼
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx " >http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
- 在rescources中建立資料庫連線池配置檔案:db.properties
配置檔案程式碼
driver_Class=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/ssh?useUnicode=true&characterEncoding=UTF-8
uname=root
upwd=root
initPoolSize=3
maxPoolSize=20
- 在pom.xml中引入資料庫連線池技術C3PO依賴:
<!-- 引入C3P0資料庫連線池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
- 配置applicationContext.xml(Spring整合重點)
先匯入所要用的依賴
<!-- spring對orm的支援依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- 引入Spring 的AspectJ依賴,解析事務切點 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.5.13</version>
</dependency>
<!-- spring管理資料來源 -->
<!-- 1.先載入db.properties -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 2.再配置資料來源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver_Class}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="user" value="${uname}"></property>
<property name="password" value="${upwd}"></property>
<property name="initialPoolSize" value="${initPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
</bean>
<!-- 3.配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="mappingLocations" value="classpath:com/lzx/pojo/*.hbm.xml"></property>
</bean>
<!-- 4. 配置事務 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事務(增強) -->
<tx:advice id="myAdivce" transaction-manager="transactionManager">
<!-- 配置增強屬性 -->
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 5.掃描com.zking.ssh.dao包下面所有的類,類中所有的方法 -->
<aop:pointcut expression="execution(* com.lzx.dao.*.*(..))" id="myCut"/>
<aop:advisor advice-ref="myAdivce" pointcut-ref="myCut"/>
</aop:config>
- 配置Dao方法
編寫介面:IUserDao
編寫實現類:UserDaoImpl,實現介面IUserDao
在applicationContext-dao.xml中配置物件
<!--配置UserDaoImpl-->
<bean id="userDaoImpl" class="com.lzx.dao.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
測試
整合Struts2
- 在pom.xml中匯入struts依賴
<!-- 引用struts2框架核心依賴 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.13</version>
</dependency>
- 配置web.xml檔案
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- Web層頁面
- 配置biz包
同時需要在applicationContext-biz.xml中配置UserBizImpl,為iUserDao注入一個物件。
<bean id="UserBizImpl" class="com.lzx.biz.UserBizImpl">
<!-- ref="UserDaoImpl"為applicationContext-dao.xml中的物件-->
<property name="iUserDao" ref="UserDaoImpl"></property>
</bean>
編寫Action
配置applictaionContext-action.xml
<bean id="addAction" class="com.lzx.action.AddAction" scope="prototype">
<property name="iuserDao" ref="userDaoImpl"></property>
</bean>
配置struts.xml
在web.xml中載入Spring
<!--載入Spring-->
<context-param>
<param-name>contextConfigLocation</param-name>
如果你只有一個檔案
就把applicationContext-*.xml
改為applicationContext.xml
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<!--新增監聽-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 引入web層-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>