通向架构师的道路(第二十天)万能框架spring(二)maven结合spring与ibatis

本文主要是介绍通向架构师的道路(第二十天)万能框架spring(二)maven结合spring与ibatis,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、前言

上次讲了Struts结合Spring并使用Spring的JdbcTemplate来搭建工程框架后我们面临着jar库无法管理,工程发布不方便,jar包在工程内太占空间,jar包冲突,管理,甚至漏包都问题。于是我们在讲“万能框架spring(二)”前,传授了一篇番外篇,即讲利用maven来管理我们的jar库。

从今天开始我们将结合“万能框架spring(一)”与番外篇maven来更进一步丰富我们的ssx框架,那么今天讲的是使用iBatis3结合SS来构建我们的ssi框架,我们把这个框架命名为beta吧。

二、SSI框架


建完后照着翻外篇《第十九天》中的“四、如何让Maven构建的工程在eclipse里跑起来”对工程进行设置。




3.2 增加iBatis3的jar相关包

打开pom.xml

第一步

找到“slf4j”,将它在pom中的描述改成如下内容:

<dependency>

                        <groupId>org.slf4j</groupId>

                        <artifactId>slf4j-api</artifactId>

                        <version>1.5.10</version>

</dependency>

第二步

增加两个jar包

<dependency>

                        <groupId>org.slf4j</groupId>

                        <artifactId>slf4j-log4j12</artifactId>

                        <version>1.5.10</version>

</dependency>

<dependency>

                        <groupId>org.apache.ibatis</groupId>

                        <artifactId>ibatis-core</artifactId>

                        <version>3.0</version>

</dependency>

3.3 开始配置ibatis与spring结合

打开/src/main/resources/spring/datasource下的datasource.xml,增加如下几行

<bean id="iBatisSessionFactory" class="org.sky.ssi.ibatis.IBatis3SQLSessionFactoryBean" scope="singleton">

                                                <property name="configLocation" value="sqlmap.xml"></property>

                                                <property name="dataSource" ref="dataSource"></property>

</bean>

<bean id="iBatisDAOSupport" class="org.sky.ssi.ibatis.IBatisDAOSupport">

</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

                        <property name="dataSource" ref="dataSource" />

</bean>

此处,我们需要4个类,它们是:

org.sky.ssi.ibatis.IBatis3SQLSessionFactoryBean类

package org.sky.ssi.ibatis;

 

import java.io.IOException;

import java.io.Reader;

import javax.sql.DataSource;

import org.apache.ibatis.builder.xml.XMLConfigBuilder;

import org.apache.ibatis.io.Resources;

import org.apache.ibatis.mapping.Environment;

import org.apache.ibatis.session.Configuration;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;

import org.springframework.beans.factory.FactoryBean;

import org.springframework.beans.factory.InitializingBean;

import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;

 

/**

 *

 * IBatis3SQLSessionFactoryBean is responsible for integrating iBatis 3 <p>

 * with spring 3. Since all environment configurations have been moved to <p>

 * spring, this class takes the responsibility to get environment information<p>

 *  from spring configuration to generate SqlSessionFactory.

 * @author lifetragedy

 *

 */

public class IBatis3SQLSessionFactoryBean implements FactoryBean<SqlSessionFactory>, InitializingBean{

                        private String configLocation;   

    private DataSource dataSource;   

    private SqlSessionFactory sqlSessionFactory;   

    private boolean useTransactionAwareDataSource = true;   

    private String environmentId = "development";

                        public String getConfigLocation() {

                                                return configLocation;

                        }

                        public void setConfigLocation(String configLocation) {

                                                this.configLocation = configLocation;

                        }

                        public DataSource getDataSource() {

                                                return dataSource;

                        }

                        public void setDataSource(DataSource dataSource) {

                                                this.dataSource = dataSource;

                        }

                        public SqlSessionFactory getSqlSessionFactory() {

                                                return sqlSessionFactory;

                        }

                        public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {

                                                this.sqlSessionFactory = sqlSessionFactory;

                        }

                        public boolean isUseTransactionAwareDataSource() {

                                                return useTransactionAwareDataSource;

                        }

                        public void setUseTransactionAwareDataSource(

                                                                        boolean useTransactionAwareDataSource) {

                                                this.useTransactionAwareDataSource = useTransactionAwareDataSource;

                        }

                        public String getEnvironmentId() {

                                                return environmentId;

                        }

                        public void setEnvironmentId(String environmentId) {

                                                this.environmentId = environmentId;

                        }

                       

                        public SqlSessionFactory getObject() throws Exception {   

        return this.sqlSessionFactory;   

    }   

  

    public Class<SqlSessionFactory> getObjectType() {   

        return  SqlSessionFactory.class;   

    }

   

    public boolean isSingleton() {   

        return true;   

    }   

  

    public void afterPropertiesSet() throws Exception {   

        this.sqlSessionFactory = this.buildSqlSessionFactory(configLocation);   

    }

   

    protected SqlSessionFactory buildSqlSessionFactory(String configLocation)   

    throws IOException {   

                        if (configLocation == null) {   

                                                throw new IllegalArgumentException(   

                                                "configLocation entry is required");   

                        }   

                        DataSource dataSourceToUse = this.dataSource;   

                        if (this.useTransactionAwareDataSource   

                                                                        && !(this.dataSource instanceof TransactionAwareDataSourceProxy)) {   

                                                dataSourceToUse = new TransactionAwareDataSourceProxy(   

                                                                                                this.dataSource);   

                        }   

 

                        Environment environment = new Environment(environmentId,   

                                                                        new IBatisTransactionFactory(dataSourceToUse), dataSourceToUse);   

 

                        Reader reader = Resources.getResourceAsReader(configLocation);   

                        XMLConfigBuilder parser = new XMLConfigBuilder(reader, null, null);   

                        Configuration config = parser.parse();   

                        config.setEnvironment(environment);   

 

                        return new DefaultSqlSessionFactory(config);   

    }

 

}

org.sky.ssi.ibatis.IBatisDAOSupport

package org.sky.ssi.ibatis;

 

import javax.annotation.Resource;

 

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.log4j.Logger;

 

 

/**

 * Base class for all DAO class. The subclass extends this class to get

 * <p>

 * DAO implementation proxy.

 *

 * @author lifetragedy

 *

 * @param <T>

 */

public class IBatisDAOSupport<T> {

 

                        protected Logger log = Logger.getLogger(this.getClass());

 

                        @Resource

                        private SqlSessionFactory ibatisSessionFactory;

 

                        private T mapper;

 

                        public SqlSessionFactory getSessionFactory() {

                                                return ibatisSessionFactory;

                        }

 

                        protected SqlSession getSqlSession() {

                                                return ibatisSessionFactory.openSession();

                        }

 

                        public T getMapper(Class<T> clazz) {

                                                mapper = getSqlSession().getMapper(clazz);

                                                return mapper;

                        }

 

                        public T getMapper(Class<T> clazz, SqlSession session) {

                                                mapper = session.getMapper(clazz);

                                                return mapper;

                        }

 

                        /**

                         * close SqlSession

                         */

                        protected void closeSqlSession(SqlSession sqlSession) throws Exception {

                                                try {

                                                                        if (sqlSession != null) {

                                                                                                sqlSession.close();

                                                                                                sqlSession = null;

                                                                        }

                                                } catch (Exception e) {

                                                }

                        }

}

org.sky.ssi.ibatis.IBatisTransaction

package org.sky.ssi.ibatis;

import java.sql.Connection;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.ibatis.transaction.Transaction;

import org.springframework.jdbc.datasource.DataSourceUtils;

public class IBatisTransaction implements Transaction{

                        private DataSource dataSource;

                        private Connection connection;

                        public IBatisTransaction(DataSource dataSource, Connection con, boolean autoCommit){

                                                this.dataSource = dataSource;

                                                this.connection = con;

                        }

 

                        public Connection getConnection(){

                                                return connection;

                        }

 

    public void commit()

        throws SQLException{                        }

 

    public void rollback()

        throws SQLException{                        }

 

    public void close()

        throws SQLException{

                        if(dataSource != null && connection != null){

                                                DataSourceUtils.releaseConnection(connection, dataSource);

                        }

    }

}

org.sky.ssi.ibatis.IBatisTransactionFactory

package org.sky.ssi.ibatis;

 

import java.sql.Connection;

import java.util.Properties;

 

import javax.sql.DataSource;

 

import org.apache.ibatis.transaction.Transaction;

import org.apache.ibatis.transaction.TransactionFactory;

 

public class IBatisTransactionFactory implements TransactionFactory{

                       

                         private DataSource dataSource;

                       

                         public IBatisTransactionFactory(DataSource dataSource){

                                                 this.dataSource = dataSource;

                         }

                         

                         public void setProperties(Properties properties){      }

                         

                         public Transaction newTransaction(Connection connection, boolean flag){

                                                 return new IBatisTransaction(dataSource,connection,flag);

                         }

 

}

此三个类的作用就是在datasource.xml文件中描述的,把spring与datasource.xml中的datasource和transaction连接起来,此处尤其是“IBatis3SQLSessionFactoryBean”的写法,它通过spring中的“注入”特性,把iBatis的配置注入进spring并委托spring的context来管理iBatis(此属网上没有的资料,全部为本人在历年工程中的经验总结,并且已经在至少3个项目中进行了集成使用与相关测试)。

建立iBatis配置文件

我们先在/src/main/resources目录下建立一个叫sqlmap.xml的文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">

<configuration>

                        <mappers>

                                                <mapper resource="ibatis/index.xml" />

                                                <mapper resource="ibatis/login.xml" />

                        </mappers>

</configuration>

然后我们在/src/main/resources 目录下建立index.xml与login.xml这2个xml文件。


看到这儿,有人会问了:为什么不把这两个xml文件也建立在spring目录下?

原因很简单:

在datasource.xml文件内我们已经通过

<bean id="iBatisSessionFactory" class="org.sky.ssi.ibatis.IBatis3SQLSessionFactoryBean" scope="singleton">

                                <property name="configLocation" value="sqlmap.xml"></property>

                                <property name="dataSource" ref="dataSource"></property>

</bean>

这样的方式把iBatis委托给了spring,iBatis的核心就是这个sqlmap.xml文件了,而在这个sqlmap.xml文件已经引用了login.xml与index.xml文件了。

而我们的web.xml文件里有这么一句:

<context-param>

                                <param-name>contextConfigLocation</param-name>

                                <param-value>/WEB-INF/classes/spring/**/*.xml</param-value>

</context-param>

因此如果我们再把ibatis/index.xml与ibatis/login.xml再建立到src/main/resources/spring目录下,spring于是会在容器启动时试图加载这两个xml文件,然后一看这两个xml文件不是什么spring的bean,直接抛错,对吧?

其们等一会再来看login.xml文件与index.xml文件,我们先来搞懂iBatis调用原理.

3.4 iBatis调用原理

1)iBatis就是一个dao层,它又被称为sqlmapping,它的sql是书写在一个.xml文件内的,在该xml文件内会将相关的sql绑定到相关的dao类的方法。

2)在调用结束时我们需要在finally块中关闭相关的sql调用。

我们来看一个例子。

login.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper

PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"

"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">

<mapper namespace="org.sky.ssi.dao.LoginDAO">

                        <select id="validLogin" resultType="int" parameterType="java.util.Map">

                                                <![CDATA[

                                                SELECT count(1) from t_login where login_id= #{loginId} and login_pwd=#{loginPwd}

                                                ]]>

                        </select>

</mapper>

该DAO指向了一个接口org.sky.ssi.dao.LoginDAO,该dao接受一个sql,并且接受一个Map类型的参数。

那么我们来看该DAO

LoginDao.java

package org.sky.ssi.dao;

 

import java.util.Map;

 

public interface LoginDAO {

                public int validLogin(Map<String, Object> paraMap) throws Exception;

}

 

LoginImpl.java

package org.sky.ssi.dao.impl;

 

import java.util.Map;

 

import org.apache.ibatis.session.SqlSession;

import org.sky.ssi.dao.LoginDAO;

import org.sky.ssi.ibatis.IBatisDAOSupport;

import org.springframework.stereotype.Repository;

 

@Repository

public class LoginDAOImpl extends IBatisDAOSupport<LoginDAO> implements LoginDAO {

 

                public int validLogin(Map<String, Object> paraMap) throws Exception {

                                SqlSession session = this.getSqlSession();

                                try {

                                                return this.getMapper(LoginDAO.class, session).validLogin(paraMap);

                                } catch (Exception e) {

                                                log.error(e.getMessage(), e);

                                                throw new Exception(e);

                                } finally {

                                                this.closeSqlSession(session);

                                }

                }

 

}

很简单吧,一切逻辑都在xml文件内。

一定记得不要忘了在finally块中关闭相关的sql调用啊,要不然将来工程出了OOM的错误不要怪我啊.

3.5 index模块

Index.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper

PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"

"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">

<mapper namespace="org.sky.ssi.dao.StudentDAO">

                        <select id="getAllStudent" resultType="org.sky.ssi.dbo.StudentDBO">

                                                <![CDATA[

                                                SELECT student_no studentNo, student_name studentName from t_student

                                                ]]>

                        </select>

 

                        <update id="addStudent" parameterType="java.util.Map">

                                                insert into t_student(student_no, student_name)values(seq_student_no.nextval,#{stdName})

                        </update>

 

                        <update id="delStudent" parameterType="java.util.Map">

                                                delete from t_student where student_no=#{stdNo}

                        </update>

</mapper>

它指向了StudentDAO这个接口

StudentDAO.java

package org.sky.ssi.dao;

 

import org.sky.ssi.dbo.StudentDBO;

import org.sky.ssi.student.form.*;

import java.util.*;

 

public interface StudentDAO {

 

                        public List<StudentDBO> getAllStudent() throws Exception;

 

                        public void addStudent(Map<String, Object> paraMap) throws Exception;

 

                        public void delStudent(Map<String, Object> paraMap) throws Exception;

}

StudentDAOImpl.java

package org.sky.ssi.dao.impl;

 

import java.util.List;

import java.util.Map;

 

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.apache.ibatis.session.SqlSession;

import org.sky.ssi.dao.StudentDAO;

import org.sky.ssi.ibatis.IBatisDAOSupport;

import org.sky.ssi.dbo.StudentDBO;

 

import org.springframework.stereotype.Repository;

 

@Repository

public class StudentDAOImpl extends IBatisDAOSupport<StudentDAO> implements StudentDAO {

 

                        @Override

                        public List<StudentDBO> getAllStudent() throws Exception {

                                                SqlSession session = this.getSqlSession();

                                                try {

                                                                        return this.getMapper(StudentDAO.class, session).getAllStudent();

                                                } catch (Exception e) {

                                                                        throw new Exception(e);

                                                } finally {

                                                                        this.closeSqlSession(session);

                                                }

                        }

 

                        public void addStudent(Map<String, Object> paraMap) throws Exception {

                                                SqlSession session = this.getSqlSession();

                                                try {

                                                                        this.getMapper(StudentDAO.class, session).addStudent(paraMap);

                                                } catch (Exception e) {

                                                                        throw new Exception(e);

                                                } finally {

                                                                        this.closeSqlSession(session);

                                                }

                        }

 

                        public void delStudent(Map<String, Object> paraMap) throws Exception {

                                                SqlSession session = this.getSqlSession();

                                                try {

                                                                        this.getMapper(StudentDAO.class, session).delStudent(paraMap);

                                                } catch (Exception e) {

                                                                        throw new Exception(e);

                                                } finally {

                                                                        this.closeSqlSession(session);

                                                }

                        }

}

3.6 Service接口微微有些改变

为了演示给大家看 iBatis接受多个参数的例子因此我们把原来的如:login(String loginId, String loginPwd)这样的方法改成了public int validLogin(Map<String, Object> paraMap) throws Exception;这样的结构,请大家注意。

四、beta工程中的增加功能

4.1 增加了一个filter

在我们的web.xml文件中

<filter>

                        <filter-name>LoginFilter</filter-name>

                        <filter-class>org.sky.ssi.filter.LoginFilter</filter-class>

                        <init-param>

                                                <param-name>exclude</param-name>

                                                <param-value>/jsp/login/login.jsp,

                                                             /login.do

                                                </param-value>

                        </init-param>

</filter>

<filter-mapping>

                        <filter-name>LoginFilter</filter-name>

                        <url-pattern>*.jsp</url-pattern>

</filter-mapping>

<filter-mapping>

                        <filter-name>LoginFilter</filter-name>

                        <url-pattern>/servlet/*</url-pattern>

</filter-mapping>

<filter-mapping>

                        <filter-name>LoginFilter</filter-name>

                        <url-pattern>*.do</url-pattern>

</filter-mapping>

有了这个filter我们就不用在我们的web工程中每一个action、每 个jsp里进行“用户是否登录”的判断了,它会自动根据配置除去“exclude”中的相关web resource,全部走这个“是否登录”的判断。

注意此处这个exclude是笔者自己写的,为什么要exclude?

如果你不exclude,试想一个用户在login.jsp中填入相关的登录信息后点一下login按钮跳转到了login.do,而这两个web resource由于没有被“排除”出“需要登录校验”,因此每次你一调用login.jsp, login.do这个filter就都会强制要求你再跳转到login.jsp,那么我们一个用户从login.jsp登录完后再跳回login.jsp再跳回,再跳回,如此重复,进入死循环。

4.2 增加了一个自动记录异常的日志功能

在我们的applicationContext.xml文件中

    <bean

        id="methodLoggerAdvisor"

        class="org.sky.ssi.util.LoggerAdvice" >

    </bean>

    <aop:config>

        <aop:aspect

            id="originalBeanAspect"

            ref="methodLoggerAdvisor" >

            <aop:pointcut

                id="loggerPointCut"

                expression="execution(* org.sky.ssi.service.impl.*.*(..))" />

            <aop:around

                method="aroundAdvice"

                pointcut-ref="loggerPointCut" />

        </aop:aspect>

    </aop:config>

这样,我们的dao层、service层、有错就只管往外throw,框架一方面在接到相关的exception会进行数据库事务的自动回滚外,还会自动把service层抛出的exception记录在log文件中。

五、测试我们的工程

确认我们的StudentServiceImpl中删除学生的delStudent方法内容如下:

public void delStudent(String[] stdNo) throws Exception {

              for (String s : stdNo) {

                     Map<String, Object> paraMap = new HashMap<String, Object>();

                     paraMap.put("stdNo", s);

                     studentDAO.delStudent(paraMap);

                     throw new Exception("force system to throw a exception");

              }

}

 

我们把beta工程添加入我们在eclipse中配好的j2eeserver中去并启动起来。


在IE中输入:http://localhost:8080/beta/index.do。 系统直接跳到login界面


我们输入相关的用户名写密码。


我们选中“13号学生高乐高”与“9号学生”,点“deletestudent”按钮。



后台抛错了,查看数据库内的数据


数据还在,说明我们的iBatis的事务已经在spring中启作用了.

再次更改StudentServiceImpl.java类中的delStudent方法,把“throw new Exception("force system to throw a exception");”注释掉,再来运行



我们再次选 中9号和13号学生,点deletestudent按钮,删除成功,这个够13的人终于被删了,呵呵。

这篇关于通向架构师的道路(第二十天)万能框架spring(二)maven结合spring与ibatis的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/351107

相关文章

闲置电脑也能活出第二春?鲁大师AiNAS让你动动手指就能轻松部署

对于大多数人而言,在这个“数据爆炸”的时代或多或少都遇到过存储告急的情况,这使得“存储焦虑”不再是个别现象,而将会是随着软件的不断臃肿而越来越普遍的情况。从不少手机厂商都开始将存储上限提升至1TB可以见得,我们似乎正处在互联网信息飞速增长的阶段,对于存储的需求也将会不断扩大。对于苹果用户而言,这一问题愈发严峻,毕竟512GB和1TB版本的iPhone可不是人人都消费得起的,因此成熟的外置存储方案开

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

Java进阶13讲__第12讲_1/2

多线程、线程池 1.  线程概念 1.1  什么是线程 1.2  线程的好处 2.   创建线程的三种方式 注意事项 2.1  继承Thread类 2.1.1 认识  2.1.2  编码实现  package cn.hdc.oop10.Thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory

每天认识几个maven依赖(ActiveMQ+activemq-jaxb+activesoap+activespace+adarwin)

八、ActiveMQ 1、是什么? ActiveMQ 是一个开源的消息中间件(Message Broker),由 Apache 软件基金会开发和维护。它实现了 Java 消息服务(Java Message Service, JMS)规范,并支持多种消息传递协议,包括 AMQP、MQTT 和 OpenWire 等。 2、有什么用? 可靠性:ActiveMQ 提供了消息持久性和事务支持,确保消