框架整合____SSM框架整合(主流整合方式,最精简整合方式)
2017-08-05 14:25
417 查看
Spring+Strus2+mybatis框架整合
//添加框架依赖和jar包
框架结构图
//配置spring的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">
<!-- springconfigStart -->
<!-- 加载多个资源配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:frame_jdbc.properties</value>
</list>
</property>
</bean>
<!-- 使用注解的方式装配置bean -->
<context:annotation-config />
<context:component-scan base-package="com.frame"></context:component-scan>
<!-- 通过注解,把URL映射到Controller上,该标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
<mvc:annotation-driven />
<!-- 配置dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<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>
<!-- 配置mybitasSqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- 配置SqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- 配置事物管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 使用annotation注解方式配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- springconfigEnd -->
</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 java.util.List;
public interface BaseDao {
public abstract int insert(String mapperSqlId, Object obj);
public abstract <T> T findObject(String mapperSqlId, Object obj);
public abstract int update(String mapperSqlId, Object obj);
public abstract int delete(String mapperSqlId, Object obj);
public abstract <E> List<E> findList(String mapperSqlId, Object obj);
public abstract int count(String mapperSqlId, Object obj);
}
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.dao;
import com.frame.base.dao.BaseDao;
public interface StudentDao extends BaseDao{
}
package com.frame.student.service;
import com.frame.student.bean.Student;
public interface StudentService {
public void insertStudent(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 insertStudent() {
Student entity = new Student();
entity.setStuid("id_" + System.currentTimeMillis());
entity.setStuname(request.getParameter("stuname"));
entity.setStutime(request.getParameter("stutime"));
try {
service.insertStudent(entity);
writeToPage("插入成功!!!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
//配置mybatis-config配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="alias_student" type="com.frame.student.bean.Student"/>
</typeAliases>
<mappers>
<mapper resource="com/frame/student/bean/Student.xml" />
</mappers>
</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="insertStudent" class="com.frame.student.action.StudentAction" method="insertStudent"></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>SpringStrutsMybatis</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/insertStudent' method="post" >
姓名:<input name="stuname" type="text" />
<br/>
生日:<input name="stutime" type="text" />
<br/>
<input name="insertBtn" type="submit" value="新增">
</form>
</body>
</html>
//发布项目启动运行
//新增查看数据库
//ok项目源码:
http://pan.baidu.com/s/1c2D3nUC
//添加框架依赖和jar包
框架结构图
//配置spring的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">
<!-- springconfigStart -->
<!-- 加载多个资源配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:frame_jdbc.properties</value>
</list>
</property>
</bean>
<!-- 使用注解的方式装配置bean -->
<context:annotation-config />
<context:component-scan base-package="com.frame"></context:component-scan>
<!-- 通过注解,把URL映射到Controller上,该标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
<mvc:annotation-driven />
<!-- 配置dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<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>
<!-- 配置mybitasSqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- 配置SqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- 配置事物管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 使用annotation注解方式配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- springconfigEnd -->
</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 java.util.List;
public interface BaseDao {
public abstract int insert(String mapperSqlId, Object obj);
public abstract <T> T findObject(String mapperSqlId, Object obj);
public abstract int update(String mapperSqlId, Object obj);
public abstract int delete(String mapperSqlId, Object obj);
public abstract <E> List<E> findList(String mapperSqlId, Object obj);
public abstract int count(String mapperSqlId, Object obj);
}
package com.frame.base.dao; import java.util.List; import org.apache.log4j.Logger; import org.mybatis.spring.SqlSessionTemplate; import org.springframework.beans.factory.annotation.Autowired; public class BaseDaoImpl implements BaseDao{ @Autowired protected SqlSessionTemplate template; //日志记录 protected final Logger logger = Logger.getLogger(this.getClass()); @Override public int insert(String mapperSqlId, Object obj) { return template.insert(mapperSqlId, obj); } @Override public <T> T findObject(String mapperSqlId, Object obj) { return template.selectOne(mapperSqlId, obj); } @Override public int update(String mapperSqlId, Object obj) { return template.update(mapperSqlId, obj); } @Override public int delete(String mapperSqlId, Object obj) { return template.delete(mapperSqlId, obj); } @Override public <E> List<E> findList(String mapperSqlId, Object obj) { return template.selectList(mapperSqlId, obj); } @Override public int count(String mapperSqlId, Object obj) { return (Integer) template.selectOne(mapperSqlId, obj); } }//创建student实体类和映射文件
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 mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.frame.student.bean.Student"> <resultMap id="student_result" type="com.frame.student.bean.Student"> <id property="stuid" column="stuid"/> <result property="stuname" column="stuname"/> <result property="stutime" column="stutime"/> </resultMap> <insert id="insertStudent" parameterType="alias_student"> <![CDATA[ insert into STUDENT (stuid,stuname,stutime) values (#{stuid},#{stuname},#{stutime}) ]]> </insert> <select id="findStudent" resultMap="student_result"> select * from STUDENT where stuid = #{stuid} </select> <update id="updateStudent" parameterType="alias_student" > update STUDENT set stuname=#{stuname},stutime=#{stutime} where stuid=#{stuid} </update> <delete id="deleteStudent" parameterType="alias_student"> delete from STUDENT where stuid=#{stuid} </delete> <select id="findStudentList" parameterType="alias_student" resultMap="student_result"> select * from STUDENT where 1=1 <if test="stuname != null and stuname !='' "> and stuname=#{stuname} </if> <if test="stuid !=null and stuid !='' "> and stuid=#{stuid} </if> </select> </mapper>//创建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 insertStudent(Student entity);
}
package com.frame.student.service; import org.apache.log4j.Logger; 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; protected final Logger log = Logger.getLogger(this.getClass()); @Override public void insertStudent(Student entity) { log.debug("saving Student instance"); try {//名称要和映射文件的select id一致 int result=dao.insert("insertStudent", entity); if(result==1){ log.debug("save successful"); } } catch (RuntimeException re) { re.printStackTrace(); } } }//创建studnetaction
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 insertStudent() {
Student entity = new Student();
entity.setStuid("id_" + System.currentTimeMillis());
entity.setStuname(request.getParameter("stuname"));
entity.setStutime(request.getParameter("stutime"));
try {
service.insertStudent(entity);
writeToPage("插入成功!!!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
//配置mybatis-config配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="alias_student" type="com.frame.student.bean.Student"/>
</typeAliases>
<mappers>
<mapper resource="com/frame/student/bean/Student.xml" />
</mappers>
</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="insertStudent" class="com.frame.student.action.StudentAction" method="insertStudent"></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>SpringStrutsMybatis</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/insertStudent' method="post" >
姓名:<input name="stuname" type="text" />
<br/>
生日:<input name="stutime" type="text" />
<br/>
<input name="insertBtn" type="submit" value="新增">
</form>
</body>
</html>
//发布项目启动运行
//新增查看数据库
//ok项目源码:
http://pan.baidu.com/s/1c2D3nUC
相关文章推荐
- 框架整合____Spring整合Struts2(主流整合方式,最精简整合方式)
- 框架整合____Spring整合Mybatis(主流,最精简整合方式,)
- SSM框架整合方式(二):XML配置文件代码
- JavaEE_Mybatis_SpringMVC_框架整合_lesson1_简单的SSM框架搭建_使用原始Dao开发方式进行测试
- SSM框架整合方式(一):XML配置文件代码
- 框架整合____SSH框架整合(主流整合方式,最易懂整合方式)
- SSM框架整合的其它方式
- SSM框架整合的其它方式
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- SSM框架整合
- SSM框架整合,包括工作流Activiti
- ssm框架整合
- SSM框架下利用mybatis-spring.jar整合包,以及利用注解@transactional实现事务管理
- SSM框架,基于JavaConfig配置方式,不用xml配置文件
- 文章标题 SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- 关于SSM框架整合Spring与Mybaitis面向接口编程时,多个不同包别名设置,多个dao层包通配符表达处理问题
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- ssm框架下slf4j+log4j整合输出日志
- JavaWeb学习笔记之SSM三大框架整合详细教程
- 整合maven+spring+springmvc+mybatis实现ssm通用增删查改基础开发框架