码迷,mamicode.com
首页 > 编程语言 > 详细

spring-struts2-ibatis(整合)

时间:2016-01-11 10:16:12      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

项目结构

技术分享

jar包

 技术分享

技术分享

技术分享

1、sqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
    <settings useStatementNamespaces="true" />
    <sqlMap resource="com/huawei/bean/StudentMapper.xml" />
</sqlMapConfig>

2、StudentMapper.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 namespace="com.huawei.bean.Student">
    <typeAlias alias="Student" type="com.huawei.bean.Student"/>
    <insert id="add" parameterClass="student">
        insert into student(id,name,grade_id,classname) values(seq_student_id.nextval,#name#,#grade#,#className#)
    </insert>
    <select id="query" resultClass="Student">
        select id,name,grade_id,classname from Student
    </select>
</sqlMap>

3、spring.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- 引入配置文件 -->
    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:db.properties</value>
            </list>
        </property>
    </bean>

    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driver}"></property>
        <property name="username" value="${username}"></property>
        <property name="password" value="${password}"></property>
        <property name="url" value="${url}"></property>
        <!-- 同一时间可以从池分配的最多连接数量。设置为0时表示无限制。  -->
        <property name="maxActive" value="20" />
        <!-- 超时等待时间以毫秒为单位  -->
        <property name="maxWait" value="500" />
        <!-- 池里不会被释放的最多空闲连接数量。设置为0时表示无限制。  -->
        <property name="maxIdle" value="3" />
        <property name="defaultAutoCommit" value="true" />
        <!-- 设置自动回收超时连接 -->
        <property name="removeAbandoned" value="true" />
        <!-- 自动回收超时时间(以秒数为单位) -->
        <property name="removeAbandonedTimeout" value="60" />
    </bean>
    
    <!-- spring注入 -->
    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation" value="classpath:sqlMapConfig.xml" />
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <bean id="istudentDao" class="com.huawei.dao.impl.StudentDaoImpl">
        <property name="sqlMapClient" ref="sqlMapClient"></property>
    </bean>
    
    <bean id="istudentService" class="com.huawei.service.impl.StudentServiceImpl">
        <property name="istudentDao" ref="istudentDao"></property>
    </bean>
    
    <bean id="studentAction" class="com.huawei.action.StudentAction">
        <property name="istudentService" ref="istudentService"></property>
    </bean>
    
    <!-- 事物处理 -->
    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <tx:advice id="advice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="register" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="login" read-only="true" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="query*" read-only="true" />
            <tx:method name="find*" read-only="true" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>
    
    <aop:config>
        <aop:pointcut expression="execution(* com.huawei.service.impl.*.*(..))"
            id="txPointcut" />
        <aop:advisor advice-ref="advice" pointcut-ref="txPointcut" />
    </aop:config>
    
</beans>

4、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="default" namespace="/" extends="struts-default">
    <action name="getStudents" class="com.huawei.action.StudentAction" method="getStudents">
        <result>/list.jsp</result>
    </action>
    <action name="register" class="com.huawei.action.StudentAction" method="register">
        <result>/msg.jsp</result>
    </action>
</package>
</struts>    

5、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">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring*.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>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

6、action.java

package com.huawei.action;

import java.util.List;

import org.apache.log4j.Logger;

import com.huawei.bean.Student;
import com.huawei.service.IStudentService;

public class StudentAction extends BaseAction {
    private Student bean;
    private List<Student> list;
    private IStudentService istudentService;
    private String msg;
    
    private static Logger logger = Logger.getLogger(StudentAction.class);

    public void setIstudentService(IStudentService istudentService) {
        this.istudentService = istudentService;
    }

    
    public List<Student> getList() {
        return list;
    }
    
    public String register(){
        istudentService.add(bean);
        bean.setGrade(null);
        istudentService.add(bean);
        msg = "注册成功";
        return SUCCESS;
    }

    public String getMsg() {
        return msg;
    }
    
    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setList(List<Student> list) {
        this.list = list;
    }


    public Student getBean() {
        return bean;
    }


    public void setBean(Student bean) {
        this.bean = bean;
    }


    public String getStudents(){
        list =istudentService.getStudents();
        logger.info(list.size()+"@@@@@@@@@@@@");
        return SUCCESS;
    }
}

7、service.java

package com.huawei.service.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.huawei.bean.Student;
import com.huawei.dao.IStudentDao;
import com.huawei.service.IStudentService;

public class StudentServiceImpl implements IStudentService{
    private static final Logger logger = Logger.getLogger(StudentServiceImpl.class);
    private IStudentDao istudentDao;
    public void add(Student bean) {
        logger.info("》》》》进入service层");
        istudentDao.add(bean);
        
    }

    public List<Student> getStudents() {
        logger.info("》》》》进入service层");
        return istudentDao.getStudents();
    }

    public void setIstudentDao(IStudentDao istudentDao) {
        this.istudentDao = istudentDao;
    }

}

8、dao.java

package com.huawei.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.huawei.bean.Student;
import com.huawei.dao.IStudentDao;
import com.huawei.service.impl.StudentServiceImpl;

public class StudentDaoImpl extends SqlMapClientDaoSupport implements IStudentDao {
    private static final Logger logger = Logger.getLogger(StudentServiceImpl.class);
    public void add(Student bean) {
        logger.info("》》》进入dao层");
        getSqlMapClientTemplate().insert(Student.class.getName()+".add",bean);
    }

    public List<Student> getStudents() {
        logger.info("》》》进入dao层");
        return getSqlMapClientTemplate().queryForList(Student.class.getName()+".query");
    }
}


9、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">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  <script type="text/javascript" src="<%=path%>/js/jquery.js"></script>

  <body>
 
    <a href="<%=path%>/getStudents.action" target="_blank">blank获取学生信息</a>
      <a href="<%=path%>/register.jsp">注册</a>
  </body>
</html>

register.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>注册</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">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  <script type="text/javascript" src="<%=path%>/js/jquery.js"></script>
  <script type="text/javascript">
  
  </script>
  <body>
    <form action="register.action" method="post">
        姓名:<input name="bean.name"/><br/>
        班级:<input name="bean.grade"/><br>
        班名:<input name="bean.className"/><br/>
        <input type="submit" value="提交"/>      
    </form>
  </body>
</html>

list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
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>
    <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">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    <table>
        <thead>
            <tr>
                <th>id</th>
                <th>name</th>
                <th>grade</th>
                <th>className</th>
            </tr>
        </thead>
        <tbody>
            <c:forEach var="list" items="${list}">
            <tr>
                <td>${list.id}</td>    
                <td>${list.name}</td>    
                <td>${list.grade}</td>    
                <td>${list.className}</td>    
            </tr>
            </c:forEach>
        </tbody>
    </table>
  </body>
</html>

 

spring-struts2-ibatis(整合)

标签:

原文地址:http://www.cnblogs.com/yxl0853133140/p/5120245.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!