国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

SSH框架整合——基于注解

2019-11-11 01:13:21
字體:
來源:轉載
供稿:網友

SSH框架整合——基于注解

@(SPRing)[Spring, hibernate, struts2, 框架整合]

SSH框架整合基于注解SSH框架整合第一步導入Jar包第二步導入配置文件第三步創建相關的包和類第四步創建界面第五步配置組件注解掃描第六步配置Action注解第七步配置業務層接口和實現類第八步配置實體類和映射注解第九步配置會話工廠第十步配置Hibernate模板第十一步配置事務管理器和事務注解掃描第十二步配置持久層接口和實現類配置OpensessionInView過濾器配置全站編碼過濾器

SSH框架整合

第一步導入Jar包

導入的jar包在《SSH框架整合——基于xml配置文件》博文中都有所介紹,這里不再贅述。唯一不同的是,基于注解的SSH整合還需要導入Struts2注解整合包struts2-convention-plugin-x.x.x.jar

第二步導入配置文件

db.propertiesjdbc.driverClass=com.MySQL.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/testjdbc.user=rootjdbc.passWord=123456applicationContext.xml<?xml version="1.0" encoding="UTF-8"?><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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置外部屬性持有對象 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置c3p0數據源--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> </bean></beans>web.xml<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> <!-- 配置Struts2核心過濾器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置Spring核心監聽器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener></web-app>log4j.properties### direct log messages to stdout ###log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.errlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### direct messages to file mylog.log ###log4j.appender.file=org.apache.log4j.FileAppenderlog4j.appender.file.File=c/:mylog.loglog4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### set log levels - for more verbose logging change 'info' to 'debug' ###log4j.rootLogger=info, stdout

第三步創建相關的包和類

package com.pc.crm.domain;import java.io.Serializable;/** * 客戶實體類 * Created by Switch on 2016/12/3. */public class Customer implements Serializable {}package com.pc.crm.dao;/** * 客戶持久層接口 * Created by Switch on 2016/12/3. */public interface CustomerDao {}package com.pc.crm.dao.impl;import com.pc.crm.dao.CustomerDao;/** * 客戶持久層接口實現類 * Created by Switch on 2016/12/3. */public class CustomerDaoImpl implements CustomerDao {}package com.pc.crm.service;/** * 客戶服務接口 * Created by Switch on 2016/12/3. */public interface CustomerService {}package com.pc.crm.service.impl;import com.pc.crm.service.CustomerService;/** * 客戶服務接口實現類 * Created by Switch on 2016/12/3. */public class CustomerServiceImpl implements CustomerService {}package com.pc.crm.web.action;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import com.pc.crm.domain.Customer;/** * 客戶Action * Created by Switch on 2016/12/3. */public class CustomerAction extends ActionSupport implements ModelDriven<Customer> { // 模型驅動對象 private Customer customer = new Customer(); @Override public Customer getModel() { return this.customer; }}

第四步創建界面

<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE HTML><html> <head> <title>首頁</title> <meta charset="utf-8"> </head> <body> <s:form action="customer_save" namespace="/customer" method="post"> <s:textfield name="custName" maxLength="50" CSSStyle="WIDTH: 180px" label="客戶名稱"/> <s:textfield name="custPhone" maxLength="50" cssStyle="WIDTH: 180px" label="固定電話"/> <s:textfield name="custMobile" maxLength="50" cssStyle="WIDTH: 180px" label="移動電話"/> <s:submit value="保存"/> </s:form> </body></html>

第五步配置組件注解掃描

applicationContext.xml中添加<!-- 開啟注解掃描 --><context:component-scan base-package="com.pc.crm.web.action"/><context:component-scan base-package="com.pc.crm.*.impl"/>

第六步配置Action注解

package com.pc.crm.web.action;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import com.pc.crm.domain.Customer;import com.pc.crm.service.CustomerService;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Namespace;import org.apache.struts2.convention.annotation.ParentPackage;import org.springframework.beans.factory.config.ConfigurableBeanFactory;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import javax.annotation.Resource;/** * 客戶Action * Created by Switch on 2016/12/3. */// 控制器組件注解,用于Web層@Controller("customerAction")// Action是多例的,所以需要配置為Prototype范圍@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)// Struts2的注解// 等價于Struts2配置文件中的package元素的extends屬性@ParentPackage("struts-default")// 等價于Struts2配置文件中的package元素的namespace屬性@Namespace("/customer")public class CustomerAction extends ActionSupport implements ModelDriven<Customer> { @Resource(name = "customerService") // 注入客戶服務對象 private CustomerService customerService; // 模型驅動對象 private Customer customer = new Customer(); @Override public Customer getModel() { return this.customer; } // 等價于Struts2配置文件中的action元素的name屬性和method屬性的復合 @Action("customer_save") public String save() { System.out.println("Action的Save方法執行了"); customerService.save(customer); return NONE; }}

第七步配置業務層接口和實現類

package com.pc.crm.service;import com.pc.crm.domain.Customer;/** * 客戶服務接口 * Created by Switch on 2016/12/3. */public interface CustomerService { /** * 保存客戶 * * @param customer */ void save(Customer customer);}package com.pc.crm.service.impl;import com.pc.crm.dao.CustomerDao;import com.pc.crm.domain.Customer;import com.pc.crm.service.CustomerService;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import javax.annotation.Resource;/** * 客戶服務接口實現類 * Created by Switch on 2016/12/3. */// 服務層組件注解,用于服務層@Service("customerService")// 配置事務,使用默認配置@Transactionalpublic class CustomerServiceImpl implements CustomerService { @Resource(name = "customerDao") // 注入客戶持久層對象 private CustomerDao customerDao; @Override public void save(Customer customer) { System.out.println("Service中的save方法執行了"); customerDao.save(customer); }}

第八步配置實體類和映射注解

package com.pc.crm.domain;import javax.persistence.*;import java.io.Serializable;/** * 客戶實體類 * Created by Switch on 2016/12/3. */// 配置為實體類@Entity// 配置對應的表名@Table(name = "cst_customer")public class Customer implements Serializable { // 配置ID,主鍵 @Id // 配置主鍵生成策略為由底層數據庫生成 @GeneratedValue(strategy = GenerationType.IDENTITY) // 配置數據庫中字段的名字等屬性 @Column(name = "cust_id") private Long custId; // 客戶編號(主鍵) @Column(name = "cust_name") private String custName; // 客戶名稱(公司名稱) @Column(name = "cust_phone") private String custPhone; // 固定電話 @Column(name = "cust_mobile") private String custMobile; // 移動電話 public Long getCustId() { return custId; } public void setCustId(Long custId) { this.custId = custId; } public String getCustName() { return custName; } public void setCustName(String custName) { this.custName = custName; } public String getCustPhone() { return custPhone; } public void setCustPhone(String custPhone) { this.custPhone = custPhone; } public String getCustMobile() { return custMobile; } public void setCustMobile(String custMobile) { this.custMobile = custMobile; } @Override public String toString() { return "Customer{" + "custId=" + custId + ", custName='" + custName + '/'' + ", custPhone='" + custPhone + '/'' + ", custMobile='" + custMobile + '/'' + '}'; }}

第九步配置會話工廠

applicationContext.xml中添加<!-- 配置Hibernate的SessionFactory,無hibernate配置文件 --><bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 配置數據源 --> <property name="dataSource" ref="dataSource"/> <!-- 配置hibernate屬性--> <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> --> <!-- 采用何種策略來創建表結構: --> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- 配置實體注解掃描 --> <property name="packagesToScan"> <list> <value>com.pc.crm.domain</value> </list> </property></bean>

第十步配置Hibernate模板

applicationContext.xml中添加<!-- 配置hibernate模板 --><bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"/></bean>

第十一步配置事務管理器和事務注解掃描

applicationContext.xml中添加<!-- 配置事務管理器 --><bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/></bean><!-- 配置事務注解掃描 --><tx:annotation-driven transaction-manager="transactionManager"/>

第十二步配置持久層接口和實現類

package com.pc.crm.dao;import com.pc.crm.domain.Customer;/** * 客戶持久層接口 * Created by Switch on 2016/12/3. */public interface CustomerDao { /** * 保存客戶 * * @param customer */ void save(Customer customer);}package com.pc.crm.dao.impl;import com.pc.crm.dao.CustomerDao;import com.pc.crm.domain.Customer;import org.springframework.orm.hibernate5.HibernateTemplate;import org.springframework.stereotype.Repository;import javax.annotation.Resource;/** * 客戶持久層接口實現類 * Created by Switch on 2016/12/3. */// 持久層組件注解,用于持久層@Repository("customerDao")public class CustomerDaoImpl implements CustomerDao { @Resource(name = "hibernateTemplate") // 注入Hibernate模板 private HibernateTemplate hibernateTemplate; @Override public void save(Customer customer) { System.out.println("Dao中的save方法執行了"); this.hibernateTemplate.save(customer); }}

配置OpenSessionInView過濾器

web.xml中添加,注意要放在Struts2核心過濾器上面<!-- 配置OpenSessionInview過濾器 --><filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class></filter><filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>*.action</url-pattern></filter-mapping>

配置全站編碼過濾器

web.xml中添加,注意要放在Struts2核心過濾器上面<!-- 配置Spring的字符編碼過濾器 --><filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param></filter><filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping>
上一篇:算法訓練 Anagrams問題

下一篇:poj1207

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江华| 光泽县| 韩城市| 墨竹工卡县| 蓬溪县| 和静县| 景洪市| 阜阳市| 平顶山市| 崇仁县| 化隆| 滨海县| 荆门市| 商南县| 德令哈市| 玛曲县| 疏勒县| 滕州市| 海伦市| 玉田县| 普格县| 铁岭县| 马山县| 句容市| 安宁市| 柏乡县| 奉贤区| 闽清县| 漯河市| 临澧县| 常山县| 林口县| 靖江市| 山西省| 南郑县| 化州市| 卓尼县| 商都县| 赫章县| 彰化市| 陈巴尔虎旗|