spring与mybatis

参考:http://www.mamicode.com/info-detail-2861936.html

参考:https://blog.csdn.net/qq_36826506/article/details/81943123

 

mybatis中dao开发的二种方式:

一、Mapper动态代理

Mapper接口开发需要遵循以下四个规范(建议初学者结合下图理解):
1、Mapper.xml文件中的namespace与mapper接口的类路径相同。
2、Mapper接口方法名和Mapper.xml中定义的每个statement的id相同
3、Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql 的parameterType的类型相同
4、Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同

 

 

 

二、原始Dao的开发

 

 

三、resultType和resultMap

基本映射 :(resultType)使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功。
(数据库,实体,查询字段,这些全部都得一一对应)

高级映射:(resultMap) 如果查询出来的列名和pojo的属性名不一致,通过定义一个resultMap对列名和pojo属性名之间作一个映射关系。但是resultMap要更强大一些 ,可自定义。因为resultMap要配置一下,表和类的一一对应关系,所以说就算你的字段名和你的实体类的属性名不一样也没关系,都会给你映射出来,但是,resultType就比较鸡肋了,必须字段名一样,比如说 cId和c_id 这种的都不能映射 。

<!--column不做限制,可以为任意表的字段,而property须为type 定义的pojo属性-->
<resultMap id="唯一的标识" type="映射的pojo对象">
  <id column="表的主键字段,或者可以为查询语句中的别名字段" jdbcType="字段类型" property="映射pojo对象的主键属性" />
  <result column="表的一个字段(可以为任意表的一个字段)" jdbcType="字段类型" property="映射到pojo对象的一个属性(须为type定义的pojo对象中的一个属性)"/>
  <association property="pojo的一个对象属性" javaType="pojo关联的pojo对象">
    <id column="关联pojo对象对应表的主键字段" jdbcType="字段类型" property="关联pojo对象的主席属性"/>
    <result  column="任意表的字段" jdbcType="字段类型" property="关联pojo对象的属性"/>
  </association>
  <!-- 集合中的property须为oftype定义的pojo对象的属性-->
  <collection property="pojo的集合属性" ofType="集合中的pojo对象">
    <id column="集合中pojo对象对应的表的主键字段" jdbcType="字段类型" property="集合中pojo对象的主键属性" />
    <result column="可以为任意表的字段" jdbcType="字段类型" property="集合中的pojo对象的属性" />  
  </collection>
</resultMap>

 

spring中相关有:

mapperLocations属性指定xml文件的路径

 

<!-- 创建SqlSessionFactory,同时指定数据源-->  

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">     

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

 <!-- 指定sqlMapConfig总配置文件,订制的environment在spring容器中不在生效-->   

 <property  name="configLocation"  value="classpath:sqlMapConfig.xml"/>   

 <!--指定实体类映射文件,可以指定同时指定某一包以及子包下面的所有配置文件,mapperLocations和configLocation有一个即可,当需要为实体类指定别名时,可指定configLocation属性,再在mybatis总配置文件中采用mapper引入实体类映射文件 -->  

  <!- - <property  name="mapperLocations"  value="classpath*:com/xxt/ibatis/dbcp/**/*.xml"/>  -->

 <bean>

 

 

<bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource_data"/>
    <property name="configLocation" value="classpath:sqlmap-config.xml"></property>
    <property name="mapperLocations" value="classpath*:mapper/*Mapper.xml"></property>
</bean>

<bean id="sqlSession1" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory1"/>
</bean>
<bean id="newErpPOHeaderInfoDao" class="cn.dao.impl.NewErpPOHeaderInfoDaoImpl">
    <property name="sqlSessionTemplate" ref="sqlSession1"/>
</bean>

 

通过继承SqlSessionDaoSupport获取 getSqlSession(),
SqlSessionDaoSupport来自SqlSessionTemplate或SqlSessionFactory,
SqlSessionFactory来自spring与mybatis中的SqlSessionFactoryBean。

 

smap下bean:

public class SmapUser implements Serializable {
    private static final long serialVersionUID = 7283333332222702L;

    private String uid;


    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getUid() {
        return uid;
    }
....

}

2 、配置文件:

smapJdbc.properties

jdbc.smap.driverClassName=org.postgresql.Driver
jdbc.smap.url=jdbc:postgresql://127.0.0.1/smap
jdbc.smap.username=postgres
jdbc.smap.password=postgres
jdbc.smap.minIdle=4
jdbc.smap.maxIdle=10
jdbc.smap.maxWait=1000
jdbc.smap.maxActive=70
jdbc.smap.initialSize=4

 SmapDao文件:

public interface SmapDao {

    //根据fullUid
    SmapUser selectPerson(String fullUid);

    //  void queryPerson(@Param("fullUid") String fullUid);

}

 SmapDaoImpl 实现类:

public class SmapDaoImpl extends SqlSessionDaoSupport implements SmapDao {
    private static final String GET_USER ="cn.cmri.pds.neusoft.smap.dao.SmapDao.selectPerson";
    @Override
    public SmapUser selectPerson(String fullUid) {

        return getSqlSession().selectOne(GET_USER,fullUid);
    }
}

xxSmap.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="cn.cmri.pds.neusoft.smap.dao.SmapDao">
    <select id="selectPerson" resultType="cn.smap.bean.SmapUser" parameterType="java.lang.String">
    SELECT  uid from v_smap where uidfull=#{fullUid}
    </select>

</mapper>

spring-config.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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="
        http://www.springframework.org/schema/aop   
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/security   
        http://www.springframework.org/schema/security/spring-security-3.2.xsd  
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/data/jpa   
        http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd  
        http://www.springframework.org/schema/tx   
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">


    <!-- Activates annotation-based bean configuration -->
    <context:annotation-config/>

    <!-- Scans for application @Components to deploy -->
    <context:component-scan base-package="cn.*"/>

    <context:property-placeholder location="classpath:/jdbc.properties,classpath:/smapJdbc.properties"/>

    <bean id="dataSource_smap" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="${jdbc.smap.driverClassName}"/>
        <property name="url" value="${jdbc.smap.url}"/>
        <property name="username" value="${jdbc.smap.username}"/>
        <property name="password" value="${jdbc.smap.password}"/>
        <property name="minIdle" value="${jdbc.smap.minIdle}"/>
        <property name="maxIdle" value="${jdbc.smap.maxIdle}"/>
        <property name="maxWait" value="${jdbc.smap.maxWait}"/>
        <property name="maxActive" value="${jdbc.smap.maxActive}"/>
        <property name="initialSize" value="${jdbc.smap.initialSize}"/>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource_smap"></property>
    </bean>

    <!-- enable transaction annotation support -->
    <tx:annotation-driven transaction-manager="dataTransactionManager" proxy-target-class="true"/>

    <!-- transaction support -->


    <bean id="smapTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource_smap"/>
    </bean>


    <bean id="sqlSessionFactorySmap" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource_smap"/>
  <!-- 当mybatis的xml文件和mapper接口不在相同包下时,需要用mapperLocations属性指定xml文件的路径。  
         *是个通配符,代表所有的文件,**代表所有目录下 -->   
        <property name="mapperLocations" value="classpath*:mapper/ErpSmap.xml"></property>
    </bean>
    <bean id="sqlSessonSmap" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactorySmap"/>
    </bean>


    <import resource="classpath*:spring-smap-project.xml"/>
    
</beans>  

spring-smap-project.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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="
        http://www.springframework.org/schema/aop   
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/security   
        http://www.springframework.org/schema/security/spring-security-3.2.xsd  
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/data/jpa   
        http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd  
        http://www.springframework.org/schema/tx   
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <bean id="smapDao" class="cn.com.smap.dao.impl.SmapDaoImpl">
        <property name="sqlSessionTemplate" ref="sqlSessonSmap"/>
    </bean>



    <bean id="projectDao" class="cn.com.dao.project.impl.ProjectDaoImpl">
        <property name="sqlSessionTemplate" ref="sqlSession"/>
    </bean>
    <bean id="pmsProjectDao" class="cn.com.dao.project.impl.PmsProjectDaoImpl"/>

    <bean id="projectTagDao" class="cn.com.dao.project.impl.ProjectTagDaoImpl">
        <property name="sqlSessionTemplate" ref="sqlSession"/>
    </bean>
    <bean id="contractDao" class="cn.com.neusoft.newcontract.dao.impl.ContractDaoImpl">
        <property name="sqlSessionTemplate" ref="sqlSession1"/>
    </bean>
   

    <bean id="newErpPOHeaderInfoDao" class="cn.com.neusoft.newErpPOHeaderInfo.dao.impl.NewErpPOHeaderInfoDaoImpl">
        <property name="sqlSessionTemplate" ref="sqlSession1"/>
    </bean>

    
    <bean id="newInterfaceLogDao" class="cn.com.neusoft.interfacelog.dao.impl.NewInterfaceDaoImpl">
        <property name="sqlSessionTemplate" ref="sqlSession1"/>
    </bean>

    <bean id="projectService" class="cn.com.project.service.impl.ProjectServiceImpl">
        <property name="projectDao" ref="projectDao"></property>
        <property name="projectTagDao" ref="projectTagDao"></property>
    </bean>

    <bean id="projectControllor" class="cn.com.project.controller.ProjectController">
        <property name="projectService" ref="projectService"></property>
    </bean>

    <bean id="projectTagControllor" class="cn.com.project.controller.ProjectTagController">
        <property name="projectTagService" ref="projectTagService"></property>
    </bean>

    <bean id="projectTagService" class="cn.com.project.service.impl.ProjectTagServiceImpl">
        <property name="projectTagDao" ref="projectTagDao"></property>
    </bean>

    <bean id="projectCache" class="cn.com.cache.ProjectCache">
        <property name="projectDao" ref="pmsProjectDao"></property>
        <constructor-arg name="projectDao" ref="pmsProjectDao"></constructor-arg>
    </bean>
    <bean id="catchOperationAspect" class="cn.com.cache.CatchOperationAspect"></bean>
    <aop:config proxy-target-class="true">
        <!-- 删除项目 -->
        <aop:aspect ref="catchOperationAspect">
            <aop:pointcut
                    expression="execution(* cn.com.project.service.impl.ProjectServiceImpl.deleteProject(..))"
                    id="projectCacheAspect"/>
            <aop:after method="deleteFromCache" pointcut-ref="projectCacheAspect"/>
        </aop:aspect>
        <!-- 增加项目(增加项目之后添加的是草稿状态的数据,不能放到缓存中去) -->
        <!-- <aop:aspect ref="catchOperationAspect">
            <aop:pointcut
                expression="execution(* cn.com.dao.project.impl.ProjectDaoImpl.addProject())"
                id="projectCacheAddAspect" />
            <aop:after method="addToCache" pointcut-ref="projectCacheAddAspect" />
        </aop:aspect> -->
        <!-- 更新项目(包括更新项目状态) -->
        <aop:aspect ref="catchOperationAspect">
            <aop:pointcut
                    expression="execution(* cn.com.project.service.impl.ProjectServiceImpl.updateProject(..))
				          ||execution(* cn.com.project.service.impl.ProjectServiceImpl.updateProjectStatus(..))"
                    id="projectCacheUpdateAspect"/>
            <aop:after method="updateCache" pointcut-ref="projectCacheUpdateAspect"/>
        </aop:aspect>
    </aop:config>
</beans>

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值