Spring整合Mybatis——蛇形转驼峰设置之舍弃主配置文件方式(不是使用spring boot整合)

Spring整合Mybatis——蛇形转驼峰设置(舍弃mybatis配置文件方式,非spring boot)

方式一:不舍弃mybatis配置文件,在mybatis主配置文件最顶部添加
<!-- 使用蛇形转驼峰式 -->
<settings>
    <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

若使用spring整合,再在spring配置文件中添加

<!-- 让spring管理sqlsessionfactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 加载mybatis的全局配置文件 -->
	<property name="configLocation" value="classpath:sqlMapConfig.xml" />
</bean>
方式二:使用Spring整合并舍弃mybatis主配置文件
  1. 在spring配置文件中管理设置类 org.apache.ibatis.session.Configuration

    <!-- mybatis配置驼峰形式的设置类 -->
    <bean id="settings" class="org.apache.ibatis.session.Configuration">
        <property name="mapUnderscoreToCamelCase" value="true"></property>
    </bean>
    
  2. 再配置sqlsessionFactory

    <!-- 配置SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 别的设置省略 -->
        <!-- 配置蛇形转驼峰 -->
        <property name="configuration" ref="settings"></property>
    </bean>
    
  3. 完整配置代码

    <?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:tx="http://www.springframework.org/schema/tx"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
    
    	<!-- 读取数据库配置文件 -->
    	<context:property-placeholder location="classpath:db.properties"/>
    	<!-- 启用注解扫描 -->
    	<context:annotation-config></context:annotation-config>
    	<!-- 设置注解扫描器 -->
    	<context:component-scan base-package="com.zjweu"></context:component-scan>
    	
    	<!-- 连接数据库,配置数据库连接池 -->
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    		<!-- 驱动名字 -->
    		<property name="driverClassName" value="${jdbc.drivername}"></property>
    		<!-- 数据库地址 -->
    		<property name="url" value="${jdbc.url}"></property>
    		<!-- 用户名 -->
    		<property name="username" value="${jdbc.username}"></property>
    		<!-- 密码 -->
    		<property name="password" value="${jdbc.password}"></property>
    		<!-- 其他配置,比如数据库最大连接数 -->
    		<property name="maxActive" value="100"></property>
    		<!-- 最大空闲连接 -->
    		<property name="maxIdle" value="20"></property>
    	</bean>
    	
    	<!-- mybatis配置驼峰形式的设置类 -->
    	<bean id="settings" class="org.apache.ibatis.session.Configuration">
    		<property name="mapUnderscoreToCamelCase" value="true"></property>
    	</bean>
    	
    	<!-- 配置SqlSessionFactory -->
    	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    		<!-- 注入数据库连接 -->
    		<property name="dataSource" ref="dataSource"></property>
    		<!-- 设置映射文件地址 -->
    		<property name="mapperLocations" value="classpath:com/zjweu/mapper/*.xml"></property>
    		<!-- 设置实体类别名 -->
    		<property name="typeAliasesPackage" value="com.zjweu.entity"></property>
    		<!-- 配置蛇形转驼峰 -->
    		<property name="configuration" ref="settings"></property>
    	</bean>
    	
    	<!-- 配置dao -->
    	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    		<property name="basePackage" value="com.zjweu.mapper"></property>
    	</bean>
    	
    	<!-- 配置数据库事务 -->
    	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<!-- 数据库连接注入 -->
    		<property name="dataSource" ref="dataSource"></property>
    	</bean>
    	
    	<!-- 配置事务 -->
    	<tx:annotation-driven transaction-manager="transactionManager"/>
    
    </beans>
    
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值