ssi整合

1.    ssi整合导入必须的包:

springcore相关包,springweb相关包是整合spring+springmvc必须导入的包。整合spring+mybatis 需要springAopspringormspringcorespringDAO相关的jar包以及mybatis,数据库驱动包,mybatis-spring整合的插件包。当然还少不了连接池相关的驱动包,如c3p0,以及日志相关的包。

 

2.    整合spring+mybatis

  1. springmybatis整合需要的XML配置文件包括一个类映射文件以及引用其他类映射文件的总配置文件。

  2. 两个文件的格式如下:

<?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">
<!-- namespace :程序可以通过他定位到sql语句 -->
<mapper namespace="DeptMapper">
    <resultMap type="Dept" id="DeptMap">
       <id property="deptId" column="dept_id"/>
       <result property="deptName" column="dept_name"/>
       <result property="deptAddress" column="dept_address"/>
    </resultMap>
 
    <insert id="insertID" parameterType="Dept">
       insert into dept(dept_name,dept_address) values(#{deptName},#{deptAddress})
    </insert>
<!—parameterType :接收的参数类型  resultMap : 返回数据要映射的对象,与resultMap中的id相同 -->
    <select id="selectByID" parameterType="Integer" resultMap="DeptMap">
       select * from dept where dept_id = #{deptId}
    </select>
       
</mapper>

Mybatis-config.xml总配置文件:

<?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 type="com.pac.scm.entity.Dept" alias="Dept"/>
    </typeAliases>
    <!-- 加载类映射文件 -->
    <mappers>
       <mapper resource="com/pac/scm/entity/DeptMapper.xml"/>
    </mappers>
</configuration>
  1. 日志的配置文件

log4j.properties如下:

log4j.rootLogger=debug,stdout,logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
  1. spring的配置文件

  2. 1                  其中包括的数据库连接,数据库连接池,spring事物以及mybatisspring整合的配置相关项

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:context="http://www.springframework.org/schema/context"
    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-3.0.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!-- 导入外部的properties配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
    <!-- 配置c3p0数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
       <property name="jdbcUrl" value="${jdbcUrl}"></property>
       <property name="driverClass" value="${driverClass}"></property>
       <property name="user" value="${user}"></property>
       <property name="password" value="${password}"></property>
       <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
       <property name="initialPoolSize" value="${initialPoolSize}"></property>
       <!--连接池中保留的最小连接数。Default: 3 -->
       <property name="minPoolSize" value="3"></property>
       <!--连接池中保留的最大连接数。Default: 15 -->
       <property name="maxPoolSize" value="${maxPoolSize}"></property>
       <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
       <property name="acquireIncrement" value="3"></property>
       <!--最大空闲时间,1800秒内未使用则连接被丢弃,若为0则永不丢弃。Default: 0 -->
        <property name="maxIdleTime" value="1800"></property>
    </bean>
 
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <property name="dataSource" ref="dataSource"></property>
       <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>
    
    <!-- 事物管理  jdbc事物管理-->
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
           <property name="dataSource" ref="dataSource"></property>
        </bean>
    
    <!-- 事务通知 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
       <tx:attributes>
           <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception" />
           <tx:method name="delete*" propagation="SUPPORTS" rollback-for="Exception" />
           <tx:method name="update*" propagation="SUPPORTS" rollback-for="Exception" />
           <tx:method name="add*" propagation="SUPPORTS" rollback-for="Exception" />
           <tx:method name="*" propagation="SUPPORTS" rollback-for="Throwable"/>
       </tx:attributes>
    </tx:advice>
    
    <!-- 扫描除了action外的包中的类,因为action中的类回执springmvc中扫描,这样就会导致aop的失败 
       同样在springmvc的配置文件中也要只扫描action中的包
    -->
     <context:component-scan base-package="com.pac">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
     </context:component-scan>
     
    <!-- aop配置被事务控制的类 -->
    <aop:config>
        <aop:pointcut id="serviceOperation" expression="execution(* com.pac.scm.service.impl.*.*(..))"/><!-- 扫描以Service结尾的bean -->
    <!--       <aop:pointcut id="serviceOperation" expression="execution(* cn.itcast..service.impl.*.*(..))"/> -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
    </aop:config>
    
    <!-- 注入一个工具类,相当于spring的JdbcTemplate -->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
       <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
    </bean>
    
    <!-- 引入外部sprign配置文件 
    <import resource="classpath:cn/itcast/*/conf/*-spring.xml"/>-->
</beans>

3.整合spring+springmvc

1.  Springmvc的整合配置web.xml文件以及springmvc的配置文件

      Web.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    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_3_0.xsd">
    
    <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
       <!-- 相当于调用setter方法 -->
       <param-name>contextConfigLocation</param-name>
       <!-- classpath指定路径为/src/ -->
       <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    
    <!-- springmvc 核心拦截器 -->
    <servlet>
       <servlet-name>DispatcherServlet</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <init-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:springmvc.xml</param-value>
       </init-param>
    </servlet>
    <servlet-mapping>
       <servlet-name>DispatcherServlet</servlet-name>
       <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    
    <!-- springmvc 编码过滤器 -->
    <filter>
       <filter-name>CharacterEncodingFilter</filter-name>
       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
       <init-param>
           <param-name>encoding</param-name>
           <param-value>UTF-8</param-value>
       </init-param>
    </filter>
    <filter-mapping>
       <filter-name>CharacterEncodingFilter</filter-name>
       <url-pattern>/*</url-pattern>
    </filter-mapping>
  <display-name></display-name> 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
  1. Springmvc.xml的配置文件与spring的相似。如下所示:

<?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:mvc="http://www.springframework.org/schema/mvc"
    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-3.0.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
    >
    <!-- 支持javabean转换为json -->
    <mvc:annotation-driven/>
    <context:component-scan base-package="com.pac">
       <!-- 只扫描action -->
       <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
       <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
       
    </context:component-scan>
 </beans>

1.    ssi整合是出现的问题

  1. 1当有异常时无法回滚:

   1.当为在方法申明中添加rollback-for="Exception" 时只能回滚运行时异常。

  2.当方法中添加了rollback-for="Exception",但是还是无法回滚,并且无其他的错误,那么就是由于spring以及springmvc两者在扫描的时候相互影响,导致出错。因此需要保证spring在扫描的时候扫描除了action外的包,而在springmvc扫描的时候要求只扫描action下的包。在spring中添加如下的配置,上面的配置中已经添加:

<context:component-scan base-package="com.pac">

<context:exclude-filter type="annotation"

expression="org.springframework.stereotype.Controller"/>

</context:component-scan>

在springmvc配置文件中添加只需要扫描action包下的类:

    <context:component-scan base-package="com.pac">

       <!-- 只扫描action -->

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />

    </context:component-scan>

2.    mybatis优化

  1. 1.                提供接口,让mybatis根据接口来提供实现,并将实现转换成springbean注入到IOC容器当中。要想利用此方式必须包括以下步骤:

(1).spring.xml文件中添加配置如下:

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

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

       <property name="basePackage" value="com.pac.scm.dao"/>

    </bean>

解析:MapperScannerConfigurer类会扫描basePackage路径下的接口,同时如果在Mapper.xml(类映射文件)中的命名空间中也用的是此基础包的value+接口名的方式,那么MapperScannerConfigurer就会为接口生成一个实现类,并转换成springBean注入到IOC容器当中。

.(2)修改Mapper.xml中的命名空间为com.pac.scm.dao+接口名。

3)如果在接口中的函数要调用mapper.xml文件中的sql的话,那么函数名必须要与id的值相同。看如下例子:

1>.接口

public interface IDeptDao {

public void insert(Dept dept);

public Dept select(Integer deptId);   

}

2>.实体的映射文件

<?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">

<!-- namespace :程序可以通过他定位到sql语句

指定要让mybatis来实现的接口-->

<mapper namespace="com.pac.scm.IDeptDao ">

<resultMap type="Dept" id="DeptMap">

     <id property="deptId" column="dept_id"/>

     <result property="deptName" column="dept_name"/>

     <result property="deptAddress" column="dept_address"/>

</resultMap>

<insert id="insert" parameterType="Dept">

     insert into dept(dept_name,dept_address) values(#{deptName},#{deptAddress})

</insert>

<!—parameterType :接收的参数类型  resultMap : 返回数据要映射的对象,与resultMap中的id相同 -->

<select id="select" parameterType="Integer" resultMap="DeptMap">

     select * from dept where dept_id = #{deptId}

</select>

</mapper>

(4).如果上述条件都实现了,那么既可以不需要dao的实现。

 

  1. 1.      为指定的包下的实体类添加一个别名:

(1).mybatis.xml文件中的取别名的配置修改为:

<typeAliases>

<!-- <typeAlias type="com.pac.scm.entity.Dept" alias="Dept"/> -->

        <package name="com.pac.scm.entity"/>

      </typeAliases>

如此的话mybatis就会为所有该包下的文件取一个与类名相同的别名

(2).以扫描的方式加载类配置文件

     1>.mybatis.xml文件中的加载类映射文件的部分配置注释;

2>.spring.xml中的配置sqlSessionFactorybean中间加上如下配置即可:

<property name="mapperLocations"

value="classpath:mapper.xml所在路径 /*.xml"></property>


转载于:https://my.oschina.net/meshwon/blog/670326

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值