SSI整合小例子-增删改查

一,数据库设计

create database ibatis;         --创建数据库ibatis
use ibatis;


create table sperson
(
 id int primary key auto_increment,
 name varchar(50),
 sex varchar(20),
 age int,
 salary float
);

二,搭建框架,创建web项目

打开MyEclipse8.5新建一个web project如图:

填写完项目名称后选择Java EE 5.0,点finish.

添加Spring支持:

选择spring2.5,这里的包默认就可以,因为jar包冲突所以我将不会使用MyEclipse里自带的包,这里我只要后边这个文件 如图:点next


将applicationContext.xml放在src文件夹下,即Folder的路径是src,点finish。

然后我将把导入的spring包删除

导入自己的包

点next

选择你要导入的spring的jar包,放在test/WebRoot/WEB-INF/lib目录下,finish。

接下来导入struts:

选择struts2.1 和  /*

点finish。然后删除导入的struts包,引入自己的jar包

finish.

引入其他jar包的方法:

选择好jar包后就能引入。本程序所用到的所有jar包为:

三,配置SSI3三部分所需要的配置文件

 (1)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">
 <!-- 配置Spring -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
   WEB-INF/classes/com/us/jack/config/applicationContext.xml
   WEB-INF/classes/com/us/jack/config/applicationContext-services.xml
  </param-value>
 </context-param>
 <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>
 <!-- 配置Struts2 -->
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.FilterDispatcher
  </filter-class>
 </filter>
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>
(2)  Struts2部分

struts.xml部分:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 <include file="com/us/jack/action/sbookAction.xml"></include>
</struts>

spersonAction.xml部分:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 
 <constant name="struts.i18n.encoding" value="GBK"/> 
 <package name="sperson" extends="struts-default" namespace="/user">
    
  <!-- 添加员工信息 记住:class="spersonAction"中的 spersonAction是在applicationContext-services.xml文件被Spring管理-->
  <action name="addSPerson" class="spersonAction" method="addSPerson">
   <result name="success">/addPerson.jsp</result>
   <result name="error">/addPerson.jsp</result>
  </action>
  
  <!-- 你也可以下面这种写:这种就没有给Spring控制了
  
  <action name="addSPerson" class="com.bjsxt.ssi2.action.SPersonAction" method="addSPerson">
   <result name="success">/addPerson.jsp</result>
   <result name="error">/addPerson.jsp</result>
  </action>
   -->
  
  <!-- 查看全部员工信息 -->
  <action name="viewSPerson" class="spersonAction" method="viewSPerson">
   <result name="success">/viewPerson.jsp</result>
   <result name="error">/viewPerson.jsp</result>
  </action>
  
  <!-- 通过ID查找员工 -->
  <action name="modifySPerson" class="spersonAction" method="modifySPerson">
   <result name="success">/personMsg.jsp</result>
   <result name="error">/personMsg.jsp</result>
  </action>
  
  <!-- 更新员工 -->
  <action name="updateSPerson" class="spersonAction" method="updateSPerson">
   <result name="success" type="redirectAction">viewSPerson</result>      <!-- action重定向 -->
   <result name="error">/personMsg.jsp</result>
  </action>
  
  <!-- 删除员工 -->
  <action name="removeSPerson" class="spersonAction" method="removeSPerson">
   <result name="success" type="redirectAction">viewSPerson</result>
   <result name="error" type="redirectAction">viewSPerson</result>
  </action>


   
 </package>
</struts>

(3) 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"
 xsi:schemaLocation="
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
   
   <!--配置一个数据源,根据上面propertyConfig指定的location去找数据库连接的配置信息-->  
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- 
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@134.224.15.38:1521:test38"/>
-->
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url" value="jdbc:mysql://localhost:3306/ibatis" />
  <property name="username" value="root" />
  <property name="password" value="root" />
  <property name="maxActive" value="100"/>
        <property name="maxIdle" value="30"/>
        <property name="maxWait" value="1000"/>
        <property name="logAbandoned" value="true"/>
        <property name="defaultAutoCommit" value="true"/>
        <property name="removeAbandoned" value="true"/>
        <property name="removeAbandonedTimeout" value="60"/>
  </bean>


 <!-- SqlMap setup for iBATIS Database Layer -->
  <bean id="sqlMapClient"
  class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <property name="configLocation">
<value>classpath:sqlMapConfig.xml</value>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
 
   </bean>
 
<!--根据sqlMapClien创建一个SqlMapClient模版类-->     
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">  


  
  <property name="sqlMapClient">   
  <ref bean="sqlMapClient" />   
  </property>   
</bean>   
 


<!--将上面的模版类织入到我们的DAO对象中(注入PersonDAO层)-->   
  <bean id="spersonDAO" class="com.bjsxt.ssi3.dao.impl.SPersonDAO">
  <property name="sqlMapClientTemplate">   
    <ref bean="sqlMapClientTemplate" />   
  </property>   
    </bean>
 
</beans>

applicationContext-services.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"
 xsi:schemaLocation="
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 
 <!-- 将业务逻辑Service类交给DAO去处理 -->
 <bean id="spersonServices" class="com.bjsxt.ssi3.services.impl.SPersonServices">
  <property name="spersonDAO" ref="spersonDAO"/>
 </bean>
 
 <!-- 将PeractionAction交给Spring控制 -->
 <bean id="spersonAction" class="com.bjsxt.ssi3.action.SPersonAction">
  <property name="spersonServices" ref="spersonServices"/>
 </bean>
 
 <!-- Transaction manager for a single JDBC DataSource -->
 <bean id="transactionManager"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
 </bean>


 <aop:config>
  <!-- 管理事务操作 -->
  <aop:pointcut id="servicesPointcut"
   expression="execution(* com.bjsxt.ssi3.services.*.*(..))" />
  <aop:advisor advice-ref="txAdvice"
   pointcut-ref="servicesPointcut" />
 </aop:config>
 
 <!-- 事务控制 -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
   <tx:method name="save*" propagation="REQUIRED" />
   <tx:method name="update*" propagation="REQUIRED" />
   <tx:method name="remove*" propagation="REQUIRED" />
   <tx:method name="get*" read-only="true" />
  </tx:attributes>
 </tx:advice>
</beans>

四, IBatis部分

sqlMapConfig.xml部分:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
    "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
 <sqlMap resource="com/bjsxt/ssi3/dao/impl/SPerson.xml"/>
</sqlMapConfig>

SPerson.xml部分:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap>


 <typeAlias alias="sperson" type="com.bjsxt.ssi3.vo.SPerson" />
 
 <!-- 添加一新员工-->
 <insert id="savePerson" parameterClass="sperson">
  <selectKey keyProperty="id" resultClass="int">
   <![CDATA[
    SELECT LAST_INSERT_ID() AS VALUE
   ]]>
  </selectKey>
  <![CDATA[
   INSERT INTO sperson(name,sex,age,salary)
   VALUES(#name#,#sex#,#age#,#salary#)
  ]]>
 </insert>
 
 <!-- 删除员工 -->
 <delete id="deletePerson" parameterClass="int">
  <![CDATA[
   DELETE FROM SPERSON WHERE ID=#id#
  ]]>
 </delete>
 
 <!-- 查找所有的员工 -->
 <select id="findAllPerson" resultClass="sperson">
  <![CDATA[
   SELECT * FROM sperson 
  ]]>
 </select>
 <!-- 更新员工信息 -->
 <update id="updatePerson" parameterClass="sperson">
  <![CDATA[
   UPDATE SPERSON SET 
   name=#name#,sex=#sex#,salary=#salary#
   WHERE id=#id#
  ]]>
 </update>
 <!-- 查找特定员工 -->
 <select id="findPersonById" parameterClass="int" resultClass="sperson">
  <![CDATA[
   SELECT * FROM sperson WHERE ID=#id#
  ]]>
 </select>
</sqlMap>

五,后台代码部分

1、vo实体类部分:SPerson.java

package com.bjsxt.ssi3.vo;
/**
 * 员工类
 * @author xia
 * 
 * 其实就是JavaBen,对应的名字与表名一致
 *
 */
public class SPerson {
private int id;
private String name;
private String sex;
private int age;
private float salary;


public int getId() {
 return id;
}

public void setId(int id) {
 this.id = id;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public String getSex() {
return sex;
}


public void setSex(String sex) {
this.sex = sex;
}


public float getSalary() {
return salary;
}


public void setSalary(float salary) {
this.salary = salary;
}


public int getAge() {
return age;
}


public void setAge(int age) {
this.age = age;
}

}

2、DAO层

ISPersonDAO.java文件:

package com.bjsxt.ssi3.dao;


import java.util.List;


import com.bjsxt.ssi3.vo.SPerson;


/****
 * DAO层
 * @author Administrator
 *
 */
public interface ISPersonDAO {
/**
 * 添加一员工至数据库中
 * 
 * @param person
 *            员工对象
 * @throws RuntimeException
 */
public void savePerson(SPerson person) throws RuntimeException;

/**
 * 删除员工信息
 * 
 * @param id
 *            ID编码
 * @throws RuntimeException
 */
public void deletePerson(int id) throws RuntimeException;

/**
 * 更新一员工的信息
 * 
 * @param person
 *           员工对象
 * @throws RuntimeException
 */
/**
 */
public void updatePerson(SPerson person) throws RuntimeException;

/**
 * 查找库中所有的员工
 * 
 * @return 返回员工列表列表
 * @throws RuntimeException
 */
public List findAllPerson() throws RuntimeException;

/**
 * 通过ID查找特定的员工
 * 
 * @param id
 *            员工的ID号
 * @return 返回此ID对应的员工信息
 * @throws RuntimeException
 */
public SPerson findPersonById(int id) throws RuntimeException;
}




SPersonDAO.java文件:
package com.bjsxt.ssi3.dao.impl;


import java.util.List;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.bjsxt.ssi3.vo.SPerson;
import com.bjsxt.ssi3.dao.ISPersonDAO;

public class SPersonDAO implements ISPersonDAO {

private SqlMapClientTemplate sqlMapClientTemplate;         //sqlMapClientTemplate 在applicationContext.xml配置 

public SqlMapClientTemplate getSqlMapClientTemplate() {
return sqlMapClientTemplate;
}

public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
this.sqlMapClientTemplate = sqlMapClientTemplate;
}
 

/***
 * //注意:insert("","")方法,第一个参数与SPerson.xml中的select元素的id属性值对应,第2个参数是传进来的参数    
 */
public void savePerson(SPerson person) throws RuntimeException {
 
sqlMapClientTemplate.insert("savePerson", person);
 
}
 

public void deletePerson(int id) throws RuntimeException {
 
sqlMapClientTemplate.delete("deletePerson", id);
}

public void updatePerson(SPerson person) throws RuntimeException {
 
sqlMapClientTemplate.update("updatePerson", person);
 
}

@SuppressWarnings("unchecked")
public List findAllPerson() throws RuntimeException {
 
List findList = null; 
 
try {
findList = (List)sqlMapClientTemplate.queryForList("findAllPerson");
 
}catch (Exception e) {
e.printStackTrace();
}
 
return findList;
 
}

/****
 * 通过ID查询
 */
public SPerson findPersonById(int id) throws RuntimeException {
 
SPerson sPerson = null;

try {

sPerson = (SPerson)sqlMapClientTemplate.queryForObject( "findPersonById", id);
}catch (Exception e) {
e.printStackTrace();
}

return sPerson;
}
 
}

3、业务逻辑层部分

ISPersonServices.java文件:

ISPersonServices.java文件:
package com.bjsxt.ssi3.services;


import java.util.List;
import com.bjsxt.ssi3.vo.SPerson;

public interface ISPersonServices {
/**
 * 添加一员工至数据库中
 * 
 * @param person
 *            员工对象
 * @throws RuntimeException
 */
public void savePerson(SPerson person) throws RuntimeException;

/**
 * 删除员工信息
 * 
 * @param id
 *            ID编码
 * @throws RuntimeException
 */
public void removePerson(int id) throws RuntimeException;


/**
 * 更新一员工的信息
 * 
 * @param person
 *            员工对象
 * @throws RuntimeException
 */
public void updatePerson(SPerson person) throws RuntimeException;

/**
 * 查找库中所有的员工
 * 
 * @return 返回员工列表
 * @throws RuntimeException
 */
public List getAllPerson() throws RuntimeException;

/**
 * 通过员工ID号得到员工对象
 * 
 * @param id
 *            员工ID号码
 * @return 返回此ID对应的员工信息
 * @throws RuntimeException
 */
public SPerson getPersonById(int id) throws RuntimeException;
}

SPersonServices.java文件:

package com.bjsxt.ssi3.services;


import java.util.List;
import com.bjsxt.ssi3.vo.SPerson;

public interface ISPersonServices {
/**
 * 添加一员工至数据库中
 * 
 * @param person
 *            员工对象
 * @throws RuntimeException
 */
public void savePerson(SPerson person) throws RuntimeException;

/**
 * 删除员工信息
 * 
 * @param id
 *            ID编码
 * @throws RuntimeException
 */
public void removePerson(int id) throws RuntimeException;


/**
 * 更新一员工的信息
 * 
 * @param person
 *            员工对象
 * @throws RuntimeException
 */
public void updatePerson(SPerson person) throws RuntimeException;

/**
 * 查找库中所有的员工
 * 
 * @return 返回员工列表
 * @throws RuntimeException
 */
public List getAllPerson() throws RuntimeException;

/**
 * 通过员工ID号得到员工对象
 * 
 * @param id
 *            员工ID号码
 * @return 返回此ID对应的员工信息
 * @throws RuntimeException
 */
public SPerson getPersonById(int id) throws RuntimeException;
}

4、控制层

SPersonAction.JAVA文件:

package com.bjsxt.ssi3.action;


import java.util.List;
import com.bjsxt.ssi3.services.ISPersonServices;
import com.bjsxt.ssi3.vo.SPerson;
import com.opensymphony.xwork2.ActionSupport;

public class SPersonAction extends ActionSupport {

private ISPersonServices spersonServices;
private SPerson sperson;
private String tips;
private String personId;
 
private List personList;


/**
 * 添加员工信息
 * 
 * @return 返回添加是否成功
 */
public String addSPerson() {
 
 String result = "error";
 
 try {
  spersonServices.savePerson(sperson);
  this.setTips("添加成功");
  
  result = "success";
  
 } catch (Exception e) {
  e.printStackTrace();
  this.setTips("系统出现问题");
 }
 
 return result;
}


/**
 * 查看所有员工
 * 
 * @return
 */
public String viewSPerson() {
 String result = "error";
 try {
  personList = spersonServices.getAllPerson();
  result = "success";
 } catch (Exception e) {
  e.printStackTrace();
  this.setTips("系统出现问题,请稍后访问");
 }
 return result;
}


/**
 * 修改员工信息
 * 
 * @return
 */
public String modifySPerson() {
 String result = "error";
 try {
  sperson = spersonServices.getPersonById(Integer.parseInt(this.getPersonId()));
  result = "success";
 } catch (Exception e) {
  e.printStackTrace();
  this.setTips("系统出现问题");
 }
 return result;
}


public String updateSPerson(){
 String result = "error";
 try{
  spersonServices.updatePerson(sperson);
  result = "success";
 }catch(Exception e){
  e.printStackTrace();
  this.setTips("更新操作失败");
 }
 return result;
}
 
/**
 * 删除图书
 * @return
 */
public String removeSPerson(){
 String result = "error";
 try{
  spersonServices.removePerson(Integer.parseInt(this.getPersonId()));
  result = "success";
 }catch(Exception e){
  e.printStackTrace();
  this.setTips("删除操作失败");
 }
 return result;
}
 
public SPerson getSperson() {
 return sperson;
}


public void setSperson(SPerson sperson) {
 this.sperson = sperson;
}


public void setSpersonServices(ISPersonServices spersonServices) {
 this.spersonServices = spersonServices;
}




public String getTips() {
 return tips;
}

public void setTips(String tips) {
 this.tips = tips;
}

public String getPersonId() {
return personId;
}

public void setPersonId(String personId) {
this.personId = personId;
}

public List getPersonList() {
return personList;
}

public void setPersonList(List personList) {
this.personList = personList;
}
}

(五)前台页面的开发

1、增加页面文件addPerson.jsp

<%@ page language="java"  pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>添加员工</title>
  </head>


  <body>
   <s:property value="tips"/>           <!--s:property 拿到Action类中定义的属性值,tips表示属性  -->
    <s:form action="addSPerson" method="post"  namespace="/user">
     <s:textfield name="sperson.name" label="姓名"/>
     <s:textfield name="sperson.sex" label="性别"/>
     <s:textfield name="sperson.age" label="年龄"/>
     <s:textfield name="sperson.salary" label="薪水"/>
     <s:submit value="添加"/>
    </s:form>
    <a href="<%=request.getContextPath() %>/user/viewSPerson.action">查看现有员工</a>
  </body>
</html>

2、修改页面文件personMsg.jsp

<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <title>修改员工信息</title>
 </head>


 <body>
  <s:form action="updateSPerson" method="post"  namespace="/user">
   <s:hidden name="sperson.id">${id}</s:hidden>
   <s:textfield name="sperson.name" label="姓名" readonly="true">${name}</s:textfield>
   <s:textfield name="sperson.sex" label="性别">${sex}</s:textfield>
   <s:textfield name="sperson.age" label="年龄">${age}</s:textfield>
   <s:textfield name="sperson.salary" label="薪水">${salary}</s:textfield>
   <s:submit/>
  </s:form>
  <s:property value="tips" />
 </body>
</html>

3、登录页面文件index.jsp

<%@ page language="java" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>员工管理页面</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>
    <a href="addPerson.jsp">添加员工</a>
    <a href="user/viewSPerson.action">浏览员工</a>
  </body>
</html>

4、浏览页面文件viewPerson.jsp

<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <title>浏览员工</title>
 </head>


 <body>
  <table align="center" border="1" style="width:80%;">
   <tr>
    <th colspan="7" align="center">员工信息</th>
   </tr>
   <tr>
    <td align="left" colspan="7"><a href="<%=request.getContextPath()%>/addPerson.jsp">添加员工</a></td>
   </tr>
   <tr>
    <td>姓名</td>
    <td>性别</td>
    <td>年龄</td>
    <td>薪水</td>
    <td>操作</td>
   </tr>
   <s:iterator value="personList">        <!-- 表示的是SPersonAction.java类中定义List对象personList -->
    <tr>
     <td>
      <s:property value="name"/>
     </td>
     <td>
      <s:property value="sex"/>
     </td>
     <td>
      <s:property value="age"/>
     </td>
     <td>
      <s:property value="salary"/>
     </td>
     <td>
      <a href="<%=request.getContextPath()%>/user/modifySPerson.action?personId=${id}">修改信息</a>
      <a href="<%=request.getContextPath()%>/user/removeSPerson.action?personId=${id}">删除</a>
     </td>
    </tr>
   </s:iterator>
   <s:property value="tips"/>
  </table>
 </body>
</html>





评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值