spring集成mybatis(idea版)

1、创建maven项目
创建项目很简单这里不再赘述。

2、导入依赖
在pom.xml中导入以下依赖

<!-- Mybatis 依赖 -->
<dependency>
	<groupId>org.mybatis</groupId>
	<artifactId>mybatis</artifactId>
	<version>3.5.5</version>
</dependency>
<!-- Mypatis-Spring 依赖 -->
<dependency>
	<groupId>org.mybatis</groupId>
	<artifactId>mybatis-spring</artifactId>
	<version>2.0.5</version>
</dependency>
<!-- PageHelper 依赖(如果需要) -->
<dependency>
	<groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper</artifactId>
	<version>5.2.0</version>
</dependency>

3、配置applicationContext.xml
在src/main/java/resources下创建applicationContext.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	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/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
		
		<!-- 启用自动扫描 -->
	<context:component-scan	base-package="com.zangerqiang.service.impl" />
	<!-- 引入外部的配置文件 -->
	<context:property-placeholder location="config.properties" />

	<!--HikariCP数据源 -->
	<bean id="dataSourceHikariCP" class="com.zaxxer.hikari.HikariDataSource">
        <property name="driverClassName" value="${driverName}" />
        <property name="jdbcUrl" value="${url}" />
        <property name="username" value="${user}" />
        <property name="password" value="${password}" />
        <!-- 连接只读数据库时配置为true, 保证安全 -->
        <!-- <property name="readOnly" value="false" /> -->
        <!-- 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 缺省:30-->
        <property name="connectionTimeout" value="30000" />
        <!-- 一个连接idle状态的最大时长(毫秒),超时则被释放(retired),缺省:10分钟 -->
        <property name="idleTimeout" value="600000" />
        <!-- 一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQL 
            wait_timeout参数(show variables like '%timeout%';-->
        <property name="maxLifetime" value="1800000" />
        <!-- 连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count) -->
        <property name="maximumPoolSize" value="60" />
        <property name="minimumIdle" value="10" />
    </bean>
	
	<!-- 配置SqlSessionFactory -->
	<bean id="sqlSessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSourceHikariCP" />
		<!-- 其它属性 -->
		<property name="typeAliasesPackage" value="com.zangerqiang.entity" />
		<property name="typeHandlersPackage" value="com.zangerqiang.typehandler"/> 
		<property name="mapperLocations" value="mappers/*.xml"/>
		<property name="plugins">
			<array>
				<bean class="com.github.pagehelper.PageInterceptor">
					<property name="properties">
						<value> 
							helperDialect=mysql
							pageSizeZero=true
							reasonable=true
						</value>
					</property>
				</bean>
			</array>
		</property>
	</bean>
	
	<!-- 配置MapperScannerConfigurer -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property 
		name="basePackage" value="com.zangerqiang.dao" />
	</bean>
	
	<!-- 配置事务管理器 -->
	<bean id="txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSourceHikariCP" />
	</bean>

	<!-- 开启事务注解扫描 -->
	<tx:annotation-driven transaction-manager="txManager" />
	
	<!-- 扫描控制器所在的包 -->
	<context:component-scan
		base-package="com.zpark.controller" />

	<!-- 启用注解配置 -->
	<mvc:annotation-driven />
</beans>		

4、总结
其实很简单,大家可以慢慢琢磨。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值