SSH框架整合
目录
resource
applicationContext.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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--导入hibernate相关的配置文件-->
<import resource="hibernateApplication.xml"/>
<!--action scope="prototype"-->
<bean id="accountAction" class="com.helong.web.AccountAction" scope="prototype">
<property name="accountService" ref="accountService"/>
</bean>
<!--service-->
<bean id="accountService" class="com.helong.service.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!--dao-->
<bean id="accountDao" class="com.helong.dao.AccountDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
hibernateApplication.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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置hibernate-->
<!--引入属性文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClass}" />
<!--属性文件当中的名称不能和name名称一样-->
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- Spring整合Hibernate -->
<!-- 引入Hibernate的配置的信息=======使用这种方式的前提是导入spring-orm.jar======== -->
<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.MySQL5Dialect</prop>
<prop key="hibernate.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>com/helong/domain/Account.hbm.xml</value>
</list>
</property>
</bean>
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--开启注解 增强-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///spring
jdbc.username=root
jdbc.password=123456
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
-->
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="com.opensymphony.xwork2" level="info"/>
<Logger name="org.apache.struts2" level="info"/>
<Logger name="org.demo.rest" level="debug"/>
<Root level="warn">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="struts" extends="struts-default">
<!--现在的class为Spring中id 将类交给Spring来管理-->
<action name="account_*" class="accountAction" method="{1}"/>
</package>
</struts>
src
com.helong
dao
AccountDao
package com.helong.dao;
import com.helong.domain.Account;
import java.util.List;
public interface AccountDao {
public void save(Account account);
public void update(Account account);
public void delete(Account account);
public Account getById(Integer id);
public List<Account> getAllAccount();
}
AccountDaoImpl
package com.helong.dao;
import com.helong.domain.Account;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Transactional
public class AccountDaoImpl extends HibernateDaoSupport implements AccountDao{
@Override
public void save(Account account) {
System.out.println("AccountDaoImpl 报存到数据库当中");
this.getHibernateTemplate().save(account);
}
@Override
public void update(Account account) {
this.getHibernateTemplate().update(account);
}
@Override
public void delete(Account account) {
this.getHibernateTemplate().delete(account);
}
@Override
public Account getById(Integer id) {
Account account = this.getHibernateTemplate().get(Account.class, id);
return account;
}
@Override
public List<Account> getAllAccount() {
/*//HQL查询方式
List<Account> list = (List<Account>)this.getHibernateTemplate().find("from Account");
return list;*/
/*QBC查询*/
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Account.class);
List<Account> list = (List<Account>)this.getHibernateTemplate().findByCriteria(detachedCriteria);
return list;
}
}
AccountDaoTest
package com.helong.dao;
import com.helong.domain.Account;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AccountDaoTest {
@Resource(name="accountDao")
private AccountDao accountDao;
@Test
public void test(){
Account account = this.accountDao.getById(8);
System.out.println(account);
}
}
domian
Account
package com.helong.domain;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Setter@Getter@ToString
public class Account {
private Integer id;
private String name;
private Double money;
}
Account.hbm.xml
<?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="com.helong.domain.Account" table="account">
<!--建立属性哪一个是主键,还要跟数据库当中主键进行对应-->
<id name="id" column="id">
<generator class="native"/>
</id>
<!--建立类当中普通属性与数据库当中字段进行关联-->
<property name="name" column="name"/>
<property name="money" column="money"/>
</class>
</hibernate-mapping>
service
AccountService
package com.helong.service;
import com.helong.domain.Account;
public interface AccountService {
public void save(Account account);
}
AccountServiceImpl
package com.helong.service;
import com.helong.dao.AccountDao;
import com.helong.dao.AccountDaoImpl;
import com.helong.domain.Account;
public class AccountServiceImpl implements AccountService{
/*注入dao*/
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void save(Account account) {
System.out.println("已经来到了业务类"+account);
//调用dao层保存到数据库当中
accountDao.save(account);
}
}
web
AccountAction
package com.helong.web;
import com.helong.domain.Account;
import com.helong.service.AccountService;
import com.helong.service.AccountServiceImpl;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContext;
public class AccountAction extends ActionSupport implements ModelDriven<Account> {
private Account account = new Account();
@Override
public Account getModel() {
return account;
}
//自动注入添加服务层类的属性,并添加set方法
private AccountService accountService;
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
//自动注入
public String save(){
System.out.println("已经来到了web层");
/*System.out.println("AccountAction/save");
System.out.println(account);*/
/*调用业务层的原始方法*/
/*AccountServiceImpl accountService = new AccountServiceImpl();
accountService.save(account);*/
/*通过Spring获取业务层*//*
ServletContext servletContext = ServletActionContext.getServletContext();
*//*获取工厂 程序启动的时候 保存到servletContext*//*
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
//获取对象
AccountServiceImpl accountService = (AccountServiceImpl) applicationContext.getBean("accountService");
accountService.save(account);*/
/*自动注入Spring对象 使用之前要引入jar包*/
accountService.save(account);
return null;
}
}
web
WEB-INF
lib
web.xml
<?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_4_0.xsd"
version="4.0">
<!--配置核心过滤器-->
<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>
<!-- Spring的核心监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加载Spring的配置文件的路径的,默认加载的/WEB-INF/applicationContext.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
index.jsp
<%--
Created by IntelliJ IDEA.
User: Dell
Date: 2019/9/26
Time: 22:30
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/account_save.action">
name:
<input type="text" name="name">
money:
<input type="text" name="money">
<input type="submit" value="提交">
</form>
</body>
</html>