Spring整合mybatis完整项目

来源:互联网 发布:北京匡恩网络 待遇 编辑:程序博客网 时间:2024/06/06 14:23
之前没有接触过mybatis,突然有个小项目需要改一下,突击了几天,整理了一下这几天的成果,备忘。
首先是配置文件
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@192.168.7.10:1521:VD"/> 
<property name="username" value="joker"/>
<property name="password" value="joker"/>
</bean>
<!--配置SqlSessionFactoryBean,然后注入到sessionFactory中-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
<property name="dataSource" ref="dataSource"/>  
        <!-- <property name="mapperLocations">  
                  <list>  
表示在com.ezca下entity目录里所有以Mapper.xml结尾所有文件 
<value>classpath:org/ezca/autocount/dao/*Mapper.xml</value>
</list>  
</property>  --> 
<!--这里面有mybatis使用的方言和分页插件-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>  
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">   
<property name="basePackage" value="org.ezca.autocount.dao"/>   
<property name="markerInterface" value="org.ezca.autocount.dao.CertRegionMapper"/>
</bean>
<!--================================事务相关控制=================================================-->  
<context:component-scan base-package="org.ezca"> 
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 由spring管理mybatis的事物 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource"></property>  
    </bean> 
    <!-- 开启事务注解驱动 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    <!-因为某一个操作,不想使用mybatis进行映射,故直接注入jdbcTemplate->
     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>

接下来,目录结构,为了看得仔细,我非常标准的将代码分成了entity,service,dao三层。




接下来,是service对mapper的注入:
package org.ezca.autocount.service;
import javax.annotation.Resource;
import org.ezca.autocount.dao.CertRegionMapper;
import org.ezca.autocount.entiy.CertRegion;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class CertRegionService {
@Resource
private CertRegionMapper certRegionMapper;
@Transactional
public void addCertRegion(CertRegion certRegion){
certRegionMapper.inserData(certRegion);
}
@Transactional
public void updateCertRegion(CertRegion certRegion){
certRegionMapper.updateData(certRegion);
}
}
这样,对于mapper来说,申明式的打开了事务,就可以将CertRegionService 作为一个bean注入到action中使用了。
接下来,重点来了:
certRegionMapper.java和certRegionMapper.xml,将mapper文件和mapper接口类放在同一个包内,就在SqlSessionFactoryBean可以不用配置mapperLocations,否则,还需要单独的配置,appper.xml的位置。


注意:certRegionMapper.java,它就是一个普通的接口,如果它只是在MapperScannerConfigurer的配置的basePackage下的一个接口,那么spring会注册一个certRegionMapper的bean供调用,而如果它被配置到了markerInterface下,那么你只能使用继承了这个接口的接口了。
package org.ezca.autocount.dao;
import org.ezca.autocount.entiy.CertRegion;
public interface CertRegionMapper extends CertRegionMapper1{
//用于新增对象
public void inserData(CertRegion certRegion);
//用于更新对象
public void updateData(CertRegion certRegion);
}
接下来是CertRegionMapper.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="org.ezca.autocount.dao.CertRegionMapper">
<!-- 如入参数为多个字符串,也可以不用写parameterType而直接传入一个hashMap,用 #{KEY}的方式取值,同时,mapperxml不支持<>符号,需要&lt,&gt,代替-->
<select id="inserData" parameterType="org.ezca.autocount.entiy.CertRegion">
insert into auoto_cert_region values (#{id,jdbcType=VARCHAR},#{certSn,jdbcType=VARCHAR},#{cert_region,jdbcType=VARCHAR},#{isuse,jdbcType=VARCHAR},to_date(#{cert_optime,jdbcType=VARCHAR},'YYYY-MM-DD HH24:MI:SS'))
</select>
<!--   这是一个对有时间戳类型和blob类型的类进行操作的配置其中在参数中的类中的属性分别对应为byte[]和Date类型,hasTSPCert为boolean 类型,在类的属性中为boolean 。
<update id="updateSignedData" parameterType="org.ezca.autotsp.entity.SignatureData">
  update vd_signed_data t
    set t.tspSigendData =
    #{tspSigendData,jdbcType=BLOB},
    t.tspSignDate = #{tspSignDate,jdbcType=TIMESTAMP},
t.tspsigned = #{tspSigned},
t.hasTSPCert =#{hasTSPCert}
where t.id = #{id}  
    </update>
    
     -->
</mapper>

到这里,就完成了整合。






0 0