Spring整合mybatis完整項目

之前沒有接觸過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>

到這裏,就完成了整合。






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章