很多使用中都會用到spring+Struts+mybatis這三大框架整合的項目,下面就是這三大框架的一個小dome。可供參考
*,先看下整體目錄結構及架包
一,首先創建web項目並建立目錄結構,添加Struts,spring框架,並行進配置
<?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>
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配置文件
<?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="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的實現
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
<%@ 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>
以上內容希望對大家有所幫助,有什麼問題歡迎多多交流