SSH整合方案二(不带hibernate.cfg.xml)
生活随笔
收集整理的這篇文章主要介紹了
SSH整合方案二(不带hibernate.cfg.xml)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
整體結構:
1.引入相關jar包
2.編寫實體類和映射文件
package cn.zqr.domain;public class Customer {private Long cust_id;private String cust_name;private Long cust_user_id;private Long cust_create_id;private String cust_source;private String cust_industry;private String cust_level;private String cust_linkman;private String cust_phone;private String cust_mobile;public Long getCust_id() {return cust_id;}public void setCust_id(Long cust_id) {this.cust_id = cust_id;}public String getCust_name() {return cust_name;}public void setCust_name(String cust_name) {this.cust_name = cust_name;}public Long getCust_user_id() {return cust_user_id;}public void setCust_user_id(Long cust_user_id) {this.cust_user_id = cust_user_id;}public Long getCust_create_id() {return cust_create_id;}public void setCust_create_id(Long cust_create_id) {this.cust_create_id = cust_create_id;}public String getCust_source() {return cust_source;}public void setCust_source(String cust_source) {this.cust_source = cust_source;}public String getCust_industry() {return cust_industry;}public void setCust_industry(String cust_industry) {this.cust_industry = cust_industry;}public String getCust_level() {return cust_level;}public void setCust_level(String cust_level) {this.cust_level = cust_level;}public String getCust_linkman() {return cust_linkman;}public void setCust_linkman(String cust_linkman) {this.cust_linkman = cust_linkman;}public String getCust_phone() {return cust_phone;}public void setCust_phone(String cust_phone) {this.cust_phone = cust_phone;}public String getCust_mobile() {return cust_mobile;}public void setCust_mobile(String cust_mobile) {this.cust_mobile = cust_mobile;}@Overridepublic String toString() {return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_user_id=" + cust_user_id+ ", cust_create_id=" + cust_create_id + ", cust_source=" + cust_source + ", cust_industry="+ cust_industry + ", cust_level=" + cust_level + ", cust_linkman=" + cust_linkman + ", cust_phone="+ cust_phone + ", cust_mobile=" + cust_mobile + "]";}} Customer 實體類 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="cn.zqr.domain.Customer" table="cst_customer"><id name="cust_id" column="cust_id"><generator class="native"/></id><property name="cust_name" column="cust_name"/><property name="cust_user_id" column="cust_user_id"/><property name="cust_create_id" column="cust_create_id"/><property name="cust_source" column="cust_source"/><property name="cust_industry" column="cust_industry"/><property name="cust_level" column="cust_level"/><property name="cust_linkman" column="cust_linkman"/><property name="cust_phone" column="cust_phone"/><property name="cust_mobile" column="cust_mobile"/></class></hibernate-mapping> 實體類映射文件3.配置struts核心過濾器,和Spring文件加載的監聽器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1"><!--配置Spring整合web的監聽器--><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>struts</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts</filter-name><url-pattern>/*</url-pattern></filter-mapping> </web-app> struts核心過濾器的配置和Spring文件加載監聽器4.編寫Action并配置,采用模型驅動的方式對表單進行封裝,采用Spring注入的方式獲取serice實例,并配置Action
/*** 客戶的控制層*/ public class CustomerAction extends ActionSupport implements ModelDriven{private CustomerService customerService;public void setCustomerService(CustomerService customerService) {this.customerService = customerService;}//必須手動newprivate Customer customer=new Customer();//模型和屬性驅動/*** 保存客戶* @return*/public String add(){customerService.save(customer);return NONE;}@Overridepublic Object getModel() {return customer;} } Action <!--配置service--><bean id="customerService" class="cn.zqr.service.impl.CustomerServiceImpl"><property name="customerDao" ref="customerDao"/></bean>?
使用Spring管理Action
<!--配置Action必須多例--><bean id="customerAction" class="cn.zqr.action.CustomerAction" scope="prototype"><property name="customerService" ref="customerService"/></bean> Spring配置文件中配置Action在struts中引入Action
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><package name="crm" namespace="/" extends="struts-default"><!--Action由Spring管理,class值為Spring的id--><action name="customer_*" class="customerAction" method="{1}"></action></package></struts> struts.xml中引入Action?
5.配置service
service層代碼:
/*** 客戶業務層*/ @Transactional public class CustomerServiceImpl implements CustomerService {private CustomerDao customerDao;public void setCustomerDao(CustomerDao customerDao) {this.customerDao = customerDao;}/*** 保存客戶信息* @param customer*/@Overridepublic void save(Customer customer) {customerDao.add(customer);} } Service層代碼 ??
service需要開事務,在Spring中配置事務,開啟事務需要使用session,所以首先配置SessionFactory,創建session工廠需要數據源,所以需要優先配置數據源
<!--配置連接池--><bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql:///ssh"/><property name="user" value="root"/><property name="password" value="admin"/></bean> 配置數據源C3P0 <bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" id="sessionFactory"><property name="dataSource" ref="dataSource"/><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="show_sql">true</prop><prop key="hibernate.format_sql">true</prop><prop key="hibernate.hbm2ddl.auto">update</prop></props></property><property name="mappingResources"><list><value>Customer.hbm.xml</value></list></property></bean> 配置SessionFactory以及hibernate相關屬性最后配置事務并開啟
<!--配置事務--><bean class="org.springframework.orm.hibernate5.HibernateTransactionManager" id="transactionManager"><property name="sessionFactory" ref="sessionFactory"/></bean><!--開始事務的注解--><tx:annotation-driven/> 配置事務并開啟6.編寫dao層,dao層需要使用Hibernate模板(HibernateTemplate),為了簡化開發,可以直接繼承HibernateDaoSupport,然后向其注入sessionFactory
/*** 客戶dao層*/public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {/*** 保存客戶*/@Overridepublic void add(Customer customer) {System.out.println("持久層Customer customer"+customer);this.getHibernateTemplate().save(customer);} } dao層代碼 <!--配置dao--><bean id="customerDao" class="cn.zqr.dao.impl.CustomerDaoImpl"><property name="sessionFactory" ref="sessionFactory"/></bean> dao層的配置?
轉載于:https://www.cnblogs.com/zqr99/p/8046987.html
總結
以上是生活随笔為你收集整理的SSH整合方案二(不带hibernate.cfg.xml)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 逻辑综合——概述与基本概念
- 下一篇: 逻辑综合——工艺库