[框架整合]spring+springmvc+mybatis(2)

在上一篇中,我们已经把项目搭建起来了。接下来将进行spring+mybatis整合


想要源代码的朋友点击这里下载哦~~


目录结构如下:

这里写图片描述

在resources目录下创建文件 mybatis-config.xml 配置文件
(该配置文件主要是管理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>
    <!-- 配置全局属性 -->
    <settings>
        <!-- 使用jdbc的getGeneratedKeys 获取数据库自增值 -->
        <setting name="useGeneratedKeys" value="true"/>
        <!-- 使用列的别名替换列名,默认为:true -->
        <setting name="useColumnLabel" value="true"/> 
        <!-- 开启驼峰命名转换 ,下划线命名法自动转换成驼峰命名法, 如start_time->startTime -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    <!-- 还有很多设置,具体可以参考mybatis官方文档 -->
    </settings>
</configuration>

接下来该创建spring相关配置文件。
这里我们把配置文件的功能分的很清楚。分别为
spring-dao.xml, spring-service.xml (后面还有spring-web.xml)

为什么要这么做呢
因为博主以前整合spring+springmvc+hibernate时就遇到坑,配置文件里的标签完全不知道什么意思,也不知道干什么的,全都挤在一个application.xml里。
这样做当然不好!
所以!配置文件按mvc来划分!!


接着在resources文件夹中,创建spring文件夹,创建spring-dao.xml, spring-service.xml

1) spring-dao.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 配置整合mybatis,主要mybatis和spring dao相关的配置 -->
    <!-- 1: 配置数据库相关参数 -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="locations">
        <list>
            <value>classpath:jdbc.properties</value>
        </list>
      </property>
    </bean>
    <!-- 2:数据库连接池c3p0 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 配置连接池属性 -->
        <property name="driverClass" value="${driver}"></property>
        <property name="jdbcUrl" value="${url}"></property>
        <property name="user" value="${username}"></property>
        <property name="password" value="${password}"></property>

        <!-- c3p0连接池私有属性 -->
        <property name="maxPoolSize" value="30"></property>
        <property name="minPoolSize" value="10"></property>
        <!-- 关闭连接后不自动commit -->
        <property name="autoCommitOnClose" value="false"></property>
        <property name="checkoutTimeout" value="10000"></property>
        <property name="acquireRetryAttempts" value="2"></property>
    </bean>
    <!-- 3:配置SqlSessionFactory对象 ** Mybatis和spring整合配置重点 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 配置MyBatis全局配置文件 mybatis-config.xml -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!-- 扫描entity包 , 可以省略包名,如org.seckill.entity.Seckill -> Seckill -->
        <property name="typeAliasesPackage" value="org.seckill.entity"></property>
        <!-- 扫描sql配置文件,mapper中的xml文件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    </bean>
    <!-- 4:配置扫描DAO接口包,动态实现Dao接口 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <!-- 给出扫描Dao接口包路径 -->
        <property name="basePackage" value="org.seckill.dao"></property>
    </bean>
</beans>

jdbc.propertis 配置

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/seckill?useUnicode=true&characterEncoding=utf8
username=root
password=root
#当然,这里还有很多很多关于mysql的配置。具体看需求

2) spring-service.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" 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 ">
    <!-- 扫描service包下的所有注解 -->
    <context:component-scan base-package="org.seckill.service"></context:component-scan>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入数据源 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置基于注解的声明式事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

到现在为止,如果项目没出错的话,项目已经整合了spring+mybatis了。
spring配置主要开启了声明式事务,和开启扫描指定包下的注解。

在下一篇中,将整合springmvc~

如有问题,欢迎留言。

转载于:https://my.oschina.net/antgan/blog/697211

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值