框架整合____SSH框架整合(主流整合方式,最易懂整合方式)
2017-08-05 13:11
405 查看
====================添加依赖()上一篇已讲==============
//添加依赖已和jar包包括oracle驱动ojdbc14.jar
系统架构图
//配置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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 加载多个资源配置文件 放到最顶层 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:frame_jdbc.properties</value>
</list>
</property>
</bean>
<!-- 配置dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 使用注解的方式装配置bean -->
<context:annotation-config />
<context:component-scan base-package="com.frame"></context:component-scan>
<!-- 通过注解,把URL映射到Controller上,该标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
<mvc:annotation-driven />
<!-- 声明hibernate的sessionfactory交由Spring管理 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 声明事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 声明使用注解式事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 声明注解式事务处理方式 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
</beans>//配置jdbc文件
################Oracle_JDBC################
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
jdbc.username=oracle
jdbc.password=oracle2017
//配置baseaction
package com.frame.base.action;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class BaseAction extends ActionSupport{
/**
* SERID
*/
private static final long serialVersionUID = 3591490585451768338L;
public HttpServletRequest request = ServletActionContext.getRequest();
public HttpServletResponse response = ServletActionContext.getResponse();
protected final Logger logger = Logger.getLogger(this.getClass());
protected void writeToPage(String respData){
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
try {
response.getWriter().write(respData);
} catch (IOException e) {
e.printStackTrace();
}
}
}
//配置basedao和实现类
package com.frame.base.dao;
import com.frame.student.bean.Student;
public interface BaseDao {
public void save(Student transientInstance);
}
package com.frame.student.bean;
public class Student {
// PO
private String stuid;
private String stuname;
private String stutime;
// Encap
public String getStuid() {
return stuid;
}
public void setStuid(String stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getStutime() {
return stutime;
}
public void setStutime(String stutime) {
this.stutime = stutime;
}
}
package com.frame.student.service;
import com.frame.student.bean.Student;
public interface StudentService {
public void saveStudent(Student entity);
}
package com.frame.student.action;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.frame.base.action.BaseAction;
import com.frame.student.bean.Student;
import com.frame.student.service.StudentService;
@Controller
@Scope("prototype")
public class StudentAction extends BaseAction{
/**
* SERID
*/
private static final long serialVersionUID = 2365184082552013332L;
@Autowired
private StudentService service;
//日志记录
protected final Logger logger = Logger.getLogger(this.getClass());
public void inserStudent() {
Student entity = new Student();
entity.setStuid("id_" + System.currentTimeMillis());
entity.setStuname(request.getParameter("stuname"));
entity.setStutime(request.getParameter("stutime"));
try {
service.saveStudent(entity);
writeToPage("插入成功!!!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
//配置hibernate配置文件
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="format_sql">true</property>
<property name="show_sql">true</property>
<mapping resource="com/frame/student/bean/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>//配置struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="struts-student" extends="struts-default" namespace="/student">
<action name="inserStudent" class="com.frame.student.action.StudentAction" method="inserStudent"></action>
</package>
</struts>
//配置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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringStrutsHibernate</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
<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>
</web-app>//配置index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form action='<%=basePath%>/student/inserStudent' method="post" >
姓名:<input name="stuname" type="text" />
<br/>
生日:<input name="stutime" type="text" />
<br/>
<input name="insertBtn" type="submit" value="新增">
</form>
</body>
</html>
//创建测试类
package com.frame.student.test;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.frame.student.bean.Student;
import com.frame.student.dao.StudentDao;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class Test {
@Autowired
StudentDao dao;
Student student;
@Before
public void setUp() throws Exception {
//做一些初始化操作 比如创建对象等
student=new Student();
student.setStuid("123");
student.setStuname("zhagnsan");
student.setStutime("2017-1-31");
}
@org.junit.Test
public void insertStudent() {
try {
dao.save(student);
} catch (Exception e) {
e.printStackTrace();
}
}
}
//运行结果
//发布项目运行
新增
//查看数据库
//源码
http://pan.baidu.com/s/1slfSNE1
//添加依赖已和jar包包括oracle驱动ojdbc14.jar
系统架构图
//配置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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 加载多个资源配置文件 放到最顶层 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:frame_jdbc.properties</value>
</list>
</property>
</bean>
<!-- 配置dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 使用注解的方式装配置bean -->
<context:annotation-config />
<context:component-scan base-package="com.frame"></context:component-scan>
<!-- 通过注解,把URL映射到Controller上,该标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
<mvc:annotation-driven />
<!-- 声明hibernate的sessionfactory交由Spring管理 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 声明事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 声明使用注解式事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 声明注解式事务处理方式 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
</beans>//配置jdbc文件
################Oracle_JDBC################
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
jdbc.username=oracle
jdbc.password=oracle2017
//配置baseaction
package com.frame.base.action;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class BaseAction extends ActionSupport{
/**
* SERID
*/
private static final long serialVersionUID = 3591490585451768338L;
public HttpServletRequest request = ServletActionContext.getRequest();
public HttpServletResponse response = ServletActionContext.getResponse();
protected final Logger logger = Logger.getLogger(this.getClass());
protected void writeToPage(String respData){
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
try {
response.getWriter().write(respData);
} catch (IOException e) {
e.printStackTrace();
}
}
}
//配置basedao和实现类
package com.frame.base.dao;
import com.frame.student.bean.Student;
public interface BaseDao {
public void save(Student transientInstance);
}
package com.frame.base.dao; import javax.annotation.Resource; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.transaction.annotation.Transactional; import com.frame.student.bean.Student; @Transactional public class BaseDaoImpl implements BaseDao{ protected static final Logger log = LoggerFactory.getLogger(BaseDaoImpl.class); @Resource private SessionFactory sessionFactory; private Session getCurrentSession() { return sessionFactory.getCurrentSession(); } @Override public void save(Student transientInstance) { log.debug("saving Student instance"); try { getCurrentSession().save(transientInstance); log.debug("save successful"); } catch (RuntimeException re) { re.printStackTrace(); } } }/配置student实体类和hbm映射文件
package com.frame.student.bean;
public class Student {
// PO
private String stuid;
private String stuname;
private String stutime;
// Encap
public String getStuid() {
return stuid;
}
public void setStuid(String stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getStutime() {
return stutime;
}
public void setStutime(String stutime) {
this.stutime = stutime;
}
}
<?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"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.frame.student.bean.Student" table="STUDENT" schema="ORACLE"> <id name="stuid" type="java.lang.String"> <column name="STUID" length="32" /> <generator class="assigned" /> </id> <property name="stuname" type="java.lang.String"> <column name="STUNAME" length="32" /> </property> <property name="stutime" type="java.lang.String"> <column name="STUTIME" length="32" /> </property> </class> </hibernate-mapping>//配置studentdao和实现类
package com.frame.student.dao; import com.frame.base.dao.BaseDao; public interface StudentDao extends BaseDao{ /** * 可以定义子类特有的方法 也可以继承父类公共的'方法' */ }
package com.frame.student.dao; import org.springframework.stereotype.Repository; import com.frame.base.dao.BaseDaoImpl; @Repository public class StudentDaoImpl extends BaseDaoImpl implements StudentDao{ }//配置studentservice和实现类
package com.frame.student.service;
import com.frame.student.bean.Student;
public interface StudentService {
public void saveStudent(Student entity);
}
package com.frame.student.service; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.frame.student.bean.Student; import com.frame.student.dao.StudentDao; @Service public class StudentServiceImpl implements StudentService{ @Autowired private StudentDao dao; private static final Logger log = LoggerFactory.getLogger(StudentServiceImpl.class); @Override public void saveStudent(Student entity) { log.debug("saving Student instance"); try { dao.save(entity); log.debug("save successful"); } catch (RuntimeException re) { re.printStackTrace(); } } }//配置studentaction
package com.frame.student.action;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.frame.base.action.BaseAction;
import com.frame.student.bean.Student;
import com.frame.student.service.StudentService;
@Controller
@Scope("prototype")
public class StudentAction extends BaseAction{
/**
* SERID
*/
private static final long serialVersionUID = 2365184082552013332L;
@Autowired
private StudentService service;
//日志记录
protected final Logger logger = Logger.getLogger(this.getClass());
public void inserStudent() {
Student entity = new Student();
entity.setStuid("id_" + System.currentTimeMillis());
entity.setStuname(request.getParameter("stuname"));
entity.setStutime(request.getParameter("stutime"));
try {
service.saveStudent(entity);
writeToPage("插入成功!!!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
//配置hibernate配置文件
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="format_sql">true</property>
<property name="show_sql">true</property>
<mapping resource="com/frame/student/bean/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>//配置struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="struts-student" extends="struts-default" namespace="/student">
<action name="inserStudent" class="com.frame.student.action.StudentAction" method="inserStudent"></action>
</package>
</struts>
//配置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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringStrutsHibernate</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
<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>
</web-app>//配置index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form action='<%=basePath%>/student/inserStudent' method="post" >
姓名:<input name="stuname" type="text" />
<br/>
生日:<input name="stutime" type="text" />
<br/>
<input name="insertBtn" type="submit" value="新增">
</form>
</body>
</html>
//创建测试类
package com.frame.student.test;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.frame.student.bean.Student;
import com.frame.student.dao.StudentDao;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class Test {
@Autowired
StudentDao dao;
Student student;
@Before
public void setUp() throws Exception {
//做一些初始化操作 比如创建对象等
student=new Student();
student.setStuid("123");
student.setStuname("zhagnsan");
student.setStutime("2017-1-31");
}
@org.junit.Test
public void insertStudent() {
try {
dao.save(student);
} catch (Exception e) {
e.printStackTrace();
}
}
}
//运行结果
//发布项目运行
新增
//查看数据库
//源码
http://pan.baidu.com/s/1slfSNE1
相关文章推荐
- 框架整合____Spring整合Struts2(主流整合方式,最精简整合方式)
- 框架整合____Spring整合Mybatis(主流,最精简整合方式,)
- 框架整合____SSM框架整合(主流整合方式,最精简整合方式)
- Spring框架整合Junit单元测试框架的纯注解方式
- Java框架整合中发生异常情况的处理方式
- spring,hibernate,struts2三大框架注解方式整合案例
- SSH框架整合的其它方式
- 【SSH框架】系列之 Spring 整合 Hibernate 框架
- s2sh框架整合详细配置-xml方式
- SSH框架流程说明(SSH框架各框架整合说明)
- SSH框架学习语录三:Struts2+Spring, 将框架的配置文件整合起来
- SSH框架中spring整合struts2三种技术方式
- SSH框架整合的其它方式
- android主流框架整合
- 边做边学小型封装--利用主流框架进行Dao层通用化,Spring3.0+Hibernate3.3.2通用Dao层整合(二)
- SSH 框架整合(maven版本 xml配置方式 连接池c3p0)
- 一套整合主流HTTP网络、图片加载、MVP(RxJava2+Dagger2)架构的快速高效的开发框架RxEasyAndroid
- SpringTest框架整合JUnit单元测试两种方式
- 基于MyEclipse8.5注解方式整合SSH框架
- 【Mybatis】使用java方式实现SM框架整合