您的位置:首页 > 编程语言 > Java开发

spring+struts+mybatis三大框架整合配置

2017-12-04 15:21 561 查看
很多使用中都会用到spring+Struts+mybatis这三大框架整合的项目,下面就是这三大框架的一个小dome。可供参考

*,先看下整体目录结构及架包







一,首先创建web项目并建立目录结构,添加Struts,spring框架,并行进配置

1.1添加Struts,spring框架此处省略详细步骤,百度一下都能得到很多,添加成功后会看到如下图所示有Struts及spring的XML配置文件



建立目录结构时最好就建立好实体类



1.2框架添加进来后就是进行配置了
首先配置web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</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>*.action</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>

1.3,配置好web.xml后添加jdbc外部链接文件,(也可以直接在mybatis的XML文件中直接配置)
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///test?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
jdbc.maxActive =2
jdbc.maxIdle =5
jdbc.minIdle=1
jdbc.initialSize =3
jdbc.maxWait =3000
1.4,配置spring的XML文件,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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-3.1.xsd"> 
<!-- 采用注释的方式配置bean -->
<!-- <context:annotation-config /> -->
<!-- 配置要扫描的包 -->
<context:component-scan base-package="com.ss2m"></context:component-scan>
<!--proxy-target-class="true"强制使用cglib代理  如果为false则spring会自动选择-->
<aop:aspectj-autoproxy proxy-target-class="true"/>

<!-- 数据库配置文件位置 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置dbcp数据源 org.apache.commons.dbcp.BasicDataSource-->
<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}" />
<!-- 队列中的最小等待数 -->
<property name="minIdle" value="${jdbc.minIdle}"></property>
<!-- 队列中的最大等待数 -->
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<!-- 最长等待时间,单位毫秒 -->
<property name="maxWait" value="${jdbc.maxWait}"></property>
<!-- 最大活跃数 -->
<property name="maxActive" value="${jdbc.maxActive}"></property>
<property name="initialSize" value="${jdbc.initialSize}"></property>
</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>
<!-- org.springframework.jdbc.datasource.DataSourceTransactionManager -->
<!-- 事务配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<description>Spring 事务管理</description>
<property name="dataSource" ref="dataSource" />
</bean>

<!-- 使用annotation注解方式配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

<!-- <bean id="empleeyAction" class="com.ss2m.action">
<property name=""></property>
</bean> -->

</beans>

二,添加mybatis架包及所需其他架包,并添加mybatis配置文件

添加mybatis及其他架包这个可以在搭建项目的时候就添加,然后这里添加mybatis的主配置文件
<?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="dept" type="com.ss2m.entity.Dept"/>
<typeAlias alias="empleey" type="com.ss2m.entity.Empleey"/>
</typeAliases>

<!-- <environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///test?characterEncoding=utf-8"/>	jdbc\:mysql\://localhost\:3306/${database}
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>  -->

<mappers>
<mapper resource="com/ss2m/mapper/EmpleeyMapper.xml" />
</mappers>
</configuration>
注:上边注释掉了链接MySQL数据库的配置,因为此项目中使用了外部jdbc.properties配置文件

三,配置EmpleeyMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ss2m.entity.Empleey">

<resultMap type="com.ss2m.entity.Empleey" id="empleeyResult">
<result property="empleeyId" column="empleeyId" jdbcType="INTEGER" javaType="
b12e
java.lang.Integer" />
<result property="empleeyName" column="empleeyName" />
<result property="empleeyPwd" column="empleeyPwd" />
<result property="empleeyAge" column="empleeyAge" />
<result property="empleeySex" column="empleeySex" />
<result property="deptId" column="deptId" />
</resultMap>
<select id="empleeyLogin" parameterType="Empleey" resultMap="empleeyResult">
select * from empleey
where
empleeyName=#{empleeyName} and empleeyPwd=#{empleeyPwd}
</select>

<select id="selectAllEmpleey" resultMap="empleeyResult">
select * from empleey
</select>

<select id="findEmpleeyById" parameterType="int" resultMap="empleeyResult">
select *
from empleey where id=#{empleeyId}
</select>

<insert id="insertEmpleey" parameterType="string">
<![CDATA[
insert into
empleey(empleeyName,empleeyPwd,empleeyAge,empleeySex,deptId)
values(#{empleeyName},#{empleeyPwd},#{empleeyAge},#{empleeySex},#{deptId})
]]>
</insert>

<update id="updateEmpleey" parameterType="string">
update empleey set
empleeyName=#{empleeyName},empleeyPwd=#{empleeyPwd},#{empleeyAge},#{empleeySex},#{deptId}
where empleeyId=#{empleeyId}
</update>

<delete id="deleteEmpleey" parameterType="int">
delete from empleey where
empleeyId=#{empleeyId}
</delete>

</mapper>

四,编写DAO以及dao的实现

编写dao如图



编写dao的实现,此处我使用了注解
package com.ss2m.daoImpl;

import java.util.List;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ss2m.dao.EmpleeyDao;
import com.ss2m.entity.Empleey;
@Repository
public class EmpleeyDaoImpl implements EmpleeyDao{
private final String INSERT_EMPLEEY = "insertEmpleey";
private final String UPDATE_EMPLEEY = "updateEmpleey";
private final String DELETE_EMPLEEY = "deleteEmpleey";
private final String FIND_EMPLEEY_BYID = "findEmpleeyById";
private final String SELECT_ALL = "selectAll";
private final String EMPLEEY_LOGIN = "empleeyLogin";
@Autowired
private SqlSessionTemplate sqlsessionTemplate;

public SqlSessionTemplate getSqlsessionTemplate() {
return sqlsessionTemplate;
}
public void setSqlsessionTemplate(SqlSessionTemplate sqlsessionTemplate) {
this.sqlsessionTemplate = sqlsessionTemplate;
}

public void insertEmpleey(Empleey empleey){
sqlsessionTemplate.insert(INSERT_EMPLEEY, empleey);
}

public void updateEmpleey(Empleey empleey){
sqlsessionTemplate.update(UPDATE_EMPLEEY, empleey);
}

public void deleteEmpleey(Integer empleey){
sqlsessionTemplate.delete(DELETE_EMPLEEY, empleey);
}

public  Empleey findEmpleeyById(Integer empleeyId){
return sqlsessionTemplate.selectOne(FIND_EMPLEEY_BYID, empleeyId);
}

public  List<Empleey> selectAll(){
return sqlsessionTemplate.selectList(SELECT_ALL);
}

public Empleey empleeyLogin(Empleey empleey){
return sqlsessionTemplate.selectOne(EMPLEEY_LOGIN, empleey);
}

}


五,编写service,及service的实现



package com.ss2m.serviceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ss2m.dao.EmpleeyDao;
import com.ss2m.entity.Empleey;
import com.ss2m.service.EmpleeyService;
@Service
@Transactional
public class EmpleeyServiceImpl implements EmpleeyService{
@Autowired
private EmpleeyDao empleeyDao;

public void insertEmpleey(Empleey empleey){
empleeyDao.insertEmpleey(empleey);
}

public void updateEmpleey(Empleey empleey){
empleeyDao.updateEmpleey(empleey);
}

public void deleteEmpleey(Integer empleeyId){
empleeyDao.deleteEmpleey(empleeyId);
}

public Empleey findEmpleeyById(Integer empleeyId){
return empleeyDao.findEmpleeyById(empleeyId);
}

public List<Empleey> selectAll(){
return empleeyDao.selectAll();
}

public Empleey empleeyLogin(Empleey empleey){
return empleeyDao.empleeyLogin(empleey);
}

}

六,编写action类

package com.ss2m.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ss2m.entity.Empleey;
import com.ss2m.service.EmpleeyService;

@Controller
@Scope("prototype")
public class EmpleeyAction extends ActionSupport{
@Autowired
private EmpleeyService empleeyService;
private Empleey empleey;
private List<Empleey> empleeyList;

public String login(){
if (empleey != null && "".equals("")) {
Empleey empleeyLogin = empleeyService.empleeyLogin(empleey);
if (empleeyLogin != null) {
return SUCCESS;
}
}
this.addFieldError("empleey.name", "用户名或密码错误");
return "login";
}

public String getAllInfo(){
empleeyList = empleeyService.selectAll();
return SUCCESS;
}

public String addUI(){
return "addInfo";
}

public String updateUI(){
empleey = empleeyService.findEmpleeyById(empleey.getEmpleeyId());
return "updateInfo";
}

public String addInfo(){
empleeyService.insertEmpleey(empleey);
return SUCCESS;
}

public String deleteInfo(){
empleeyService.deleteEmpleey(empleey.getEmpleeyId());
return SUCCESS;
}

public String updateInfo(){
empleeyService.updateEmpleey(empleey);
return SUCCESS;
}

//get.set
public EmpleeyService getEmpleeyService() {
return empleeyService;
}

public void setEmpleeyService(EmpleeyService empleeyService) {
this.empleeyService = empleeyService;
}

public Empleey getEmpleey() {
return empleey;
}

public void setEmpleey(Empleey empleey) {
this.empleey = empleey;
}

public List<Empleey> getEmpleeyList() {
return empleeyList;
}

public void setEmpleeyList(List<Empleey> empleeyList) {
this.empleeyList = empleeyList;
}

}

七,编写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>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- 指定默认编码集 ,作用于HttpServletRequest的setCharacterEncoding()和freemarker,vilocity的输出 -->
<!-- <constant name="struts.configuration.xmlreload" value="true"/>  -->
<!-- 当struts配置文件修改时是否自动加载 -->
<!-- <constant name="struts.devMode" value="true"/>  -->
<!-- 开发模式下打印详细的错误信息 -->
<constant name="struts.ui.theme" value="xhtml"/>

<package name="empleey" extends="struts-default" namespace="/">
<action name="info_*" class="empleeyAction" method="{1}">
<result name="success">/index.jsp</result>	<!-- 首页显示 -->
<result name="login">/dologin.jsp</result>	<!-- 登录页 -->
<result name="dologin" type="redirectAction">info_success.action</result>
<result name="addInfo" type="redirectAction">info_success.action</result>
<result name="updateInfo">/updateInfo.jsp</result>
</action>
</package>

</struts>


此处仅供参考,有些内容我没改正

八,编写jsp

编写jsp此处依据自己所需编写,下面只做参考
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录页面</title>
<style>
*{
margin: 0px;
padding: 0px;
}
.login{
margin:auto auto;
width:660px;
height:313px;
background-image: url("images/login.png");
}
.Login div{
margin-top: 90px;
margin-left:205px;
overflow: hidden;
float: left;
}
.ipt{
width:160px;
height:20px;
}
.dl{margin-left:218px;margin-top:10px}
.code{width:60px}
.img{vertical-align: middle;}
.msg{font-size: 16px;font-family:微软雅黑;color:red}
</style>
<script>
//该方法用户更换用户的验证码
function  changeImg(){
var objImg=document.getElementById("pic");
objImg.src="checkCode_getCode.action?ran="+Math.random()
}
</script>
</head>

<body>
<div>
<div  class="login">

<div>
<form  action="info_login.action" method="post">
<p class="msg" style="font-size:14;font-family:微软雅黑;color:red">${msg }</p><br>
<p>用户名:<input type="text" name="name" class="ipt"> <font style="微软雅黑" size='2'></font><br><br>

<p>密   码:<input type="password" name="pwd"  class="ipt"></p><br>

<p>验证码:<input type="text" name="code"  class="code" maxlength="4">
<img src="code.action" id="pic" class="img">
<a href="javascript:changeImg()">看不清楚换一张</a></p><br>

<p class="dl"><input type="image" src="images/dl.jpg"></p>
</form>
</div>
</div>
</div>

</body>
</html>


以上内容希望对大家有所帮助,有什么问题欢迎多多交流
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息