mybatis与spring集成

1、在pom.xml添加相关依赖

注:spring 5.0.1.RELEASE有冲突

   1.1 添加spring相关依赖(5.0.2.RELEASE)
       spring-core
       spring-beans
       spring-context
       spring-orm
       spring-tx
       spring-aspects
       spring-web

   1.2 添加mybatis相关依赖
       mybatis核心:mybatis(3.4.5)
       Mybatis分页:pagehelper(5.1.2)

   1.3 spring整合mybatis(1.3.1)
       mybatis-spring

   1.4 添加dbcp2连接池
       commons-dbcp2(2.1.1)
       commons-pool2(2.4.3)

   1.5 添加日志配置(2.9.1)
       log4j-core
       log4j-api
       log4j-web

   1.6 其他
       junit(4.12)
       javax.servlet-api(4.0.0)
       lombok(1.18.2)


   注:使用mybatis-generator插件,pom文件添加支持  

2.创建spring配置文件applicationContext

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--1. 引入外部多文件方式 -->
    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="ignoreResourceNotFound" value="true" />
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
                <value>classpath:redis.properties</value>
            </list>
        </property>
    </bean>
    <!--引入mybatis的相关配置文件-->
    <import resource="applicationContext-mybatis.xml"></import>
    <!--spring管理echcache对应的配置文件-->
    <import resource="applicationContext-ehcache.xml"></import>
    <!--spring管理redis对应的配置文件-->
    <import resource="applicationContext-redis.xml"></import>

</beans>

 

2.1 注解式开发
   开启注解
   <!--1. 注解式开发 -->
   <!-- 注解驱动 -->
   <context:annotation-config/>
   <!-- 用注解方式注入bean,并指定查找范围:com.zking.ssh2及子子孙孙包-->
   <context:component-scan base-package="com.xxx.oa"/>

   2.2 引入外部jdbc配置文件
   <context:property-placeholder location="classpath:jdbc.properties"/>
 
   2.3 配置dbcp2数据库连接池
   详见“dbcp2.txt”

   2.4 spring和mybatis整合
   详见“spring与mybatis整合.txt”

   2.5 注解式事物配置
    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

   2.6 开启动态代理
   <aop:aspectj-autoproxy/>
applicationContext-mybatis.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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--1. 注解式开发 -->
    <!-- 注解驱动 -->
    <context:annotation-config/>
    <!-- 用注解方式注入bean,并指定查找范围:com.xxx.ssh2及子子孙孙包-->
    <context:component-scan base-package="com.xxx.ssm"/>
    <!--注册数据库信息文件-->
    <!--<context:property-placeholder location="classpath:jdbc.properties"/>-->
    <!--数据库连接池配置-->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <!--初始连接数-->
        <property name="initialSize" value="10"/>
        <!--最大活动连接数-->
        <property name="maxTotal" value="100"/>
        <!--最大空闲连接数-->
        <property name="maxIdle" value="50"/>
        <!--最小空闲连接数-->
        <property name="minIdle" value="10"/>
        <!--设置为-1时,如果没有可用连接,连接池会一直无限期等待,直到获取到连接为止。-->
        <!--如果设置为N(毫秒),则连接池会等待N毫秒,等待不到,则抛出异常-->
        <property name="maxWaitMillis" value="-1"/>
    </bean>
    <!--sqlsessionFactory信息配置-->
    <!--4. spring和MyBatis整合 -->
    <!--1) 创建sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 指定数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 自动扫描XxxMapping.xml文件,**任意路径 -->
        <property name="mapperLocations" value="classpath*:com/xxx/ssm/**/mapper/*.xml"/>
        <!-- 指定别名 -->
        <!--<property name="typeAliasesPackage" value="com/xxx/ssm/**/model"/>-->
        <!--配置pagehelper插件-->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <value>
                            helperDialect=mysql
                        </value>
                    </property>
                </bean>
            </array>
        </property>
        <!--设置mybaits对缓存的支持-->
        <property name="configurationProperties">
            <props>
                <!-- 全局映射器启用缓存 *主要将此属性设置完成即可-->
                <prop key="cacheEnabled">true</prop>
                <!-- 查询时,关闭关联对象即时加载以提高性能 -->
                <prop key="lazyLoadingEnabled">false</prop>
                <!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能 -->
                <prop key="aggressiveLazyLoading">true</prop>
            </props>
        </property>
    </bean>

    <!--2) 自动扫描com/xxx/oa/**/mapper下的所有XxxMapper接口(其实就是DAO接口),并实现这些接口,-->
    <!--   即可直接在程序中使用dao接不用口,再获取sqlsession对象-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--basePackage 属性是映射器接口文件的包路径。-->
        <!--你可以使用分号或逗号 作为分隔符设置多于一个的包路径-->
        <property name="basePackage" value="com/xxx/ssm/**/mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
    <!--注解式事务配置-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />
    <!--开启动态代理-->
    <aop:aspectj-autoproxy/>
</beans>

3.注解式开发

   @Repository:将DAO类声明为Bean
   @Service:通常作用在业务层
   @Constroller:通常作用在控制层,将在Spring MVC中使用
   @Component:是一个泛化的概念,仅仅表示spring中的一个组件(Bean),可以作用在任何层次
   @Scope:模式声明(singleton|prototype)
   @Autowired:将自动在代码上下文与其匹配(默认是类型匹配)的Bean,并自动注入到相应的地方
   @Resource:
   1)@Resource后面没有任何内容,默认通过name属性去匹配bean,找不到再按type去匹配
   2)指定了name或者type则根据指定的类型去匹配bean
   3)指定了name和type则根据指定的name和type去匹配bean,任何一个不匹配都将报错

   问题:@Autowired和@Resource两个注解的区别:
   1)@Autowired默认按照byType方式进行bean匹配,@Resource默认按照byName方式进行bean匹配
   2)@Autowired是Spring的注解,@Resource是J2EE的注解,这个看一下导入注解的时候这两个注解的包名就一清二楚了
   Spring属于第三方的,J2EE是Java自己的东西,因此,建议使用@Resource注解,以减少代码和Spring之间的耦合。

   @Transactional
   注:个人感觉注解式事务比以前的声明式事务更加麻烦,要写的东西更多

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值