Mybatis知识整理(十二)Mybatis完结之与Spring整合

需要spring通过单例方式管理SqlSessionFactory。

spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession。(spring和mybatis整合自动完成) 持久层的mapper都需要由spring进行管理。

(1.jar导入)

(2.在applicationContext.xml配置sqlSessionFactory和数据源)

(3.Spring配置文件applicationContext.xml)

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       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-4.0.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    <!--加载配置文件-->
   <context:property-placeholder location="classpath:database.properties" ignore-unresolvable="true"/>
    <!--数据源,dataSource-->
    <!--使用c3p0连接池注册-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <!-- 基础配置 -->
        <property name="jdbcUrl" value="${database.url}"></property>
        <property name="driverClass" value="${database.driver}"></property>
        <property name="user" value="${database.user}"></property>
        <!--<property name="password" value="${jdbc.password}"></property>-->
        <!-- 关键配置 -->
        <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
        <property name="initialPoolSize" value="3"></property>
        <!--连接池中保留的最小连接数。Default: 2 -->
        <property name="minPoolSize" value="2"></property>
        <!--连接池中保留的最大连接数。Default: 15 -->
        <property name="maxPoolSize" value="15"></property>
        <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
        <property name="acquireIncrement" value="3"></property>
        <!-- 性能配置 -->
        <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
        <property name="maxStatements" value="8"></property>
        <!-- maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
        <property name="maxStatementsPerConnection" value="5"></property>
        <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
        <property name="maxIdleTime" value="1800"></property>
    </bean>
    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--加载mybatis的配置文件-->
        <property name="configLocation" value="mybatis/SqlMapConfig.xml"/>
        <!--dataSource数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--mapper批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并且在Spring容器中注入注册-->
    <!--遵循规范;将mapper.java和mapper.xml映射文件保持一致,且在一个目录中-->
    <!--扫描出来的mapper的bean的id是首字母小写-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--指定扫描的包名-->
        <!--如果扫描多个包,不能使用通配符,要使用半角逗号分隔-->
        <property name="basePackage" value="com.ssm.mapper"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    </beans>

(4.mybatis配置文件)

 

<?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>
        <!--针对单个别名-->
        <!--type:类型的路径-->
        <!--alias:别名-->
        <!--<typeAlias type="com.mybatis.po.User" alias="user"></typeAlias>-->
        <!--批量别名定义-->
        <!--mybatis会自动扫描包中的po类,自动定义别名,别名就是类名(首字母是大写或是小写都可以)-->
        <package name="com.ssm.po"></package>
    </typeAliases>
    <!--加载映射文件-->
    <!--<mappers>-->
        <!--spring整合之后自动扫描-->
        <!--<package name="com.ssm.mapper"/>-->
    <!--</mappers>-->
</configuration>

(5.编写mapper.xml与mapper.java开始写程序)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值