Mybatis入门学习(十二、spring整合)

首先整合spring-Mybatis思路

  • 首先导入相关的jar包
  • 加入Mybatis和spring的配置文件
  • 配置Dao层
  • 测试类

相关的jar包

在这里插入图片描述

配置文件

  • 配置文件分为Mybatis和spring的

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>
		<package name="domain"/>
	</typeAliases>
</configuration>
  • 这里面只需要配置一个别名,别名使用的是扫描包的方法
  • 数据源、映射文件之类的都交给spring管理

Spring的配置文件

  • 需要配置以下信息
  • 数据源,如果使用的配置文件,还需要加载配置文件
  • 配置链接工厂(SqlSessionFactory)
  • 配置Dao层(二种方式,传统的Dao以及动态代理)
数据源:
<!-- 加载配置文件 -->
   <context:property-placeholder location="classpath:jdbc.properties" />

	<!-- 数据库连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.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="maxActive" value="10" />
		<!-- 最大空闲数 -->
		<property name="maxIdle" value="5" />
	</bean>

这里是使用的配置文件的方式加载的

配置文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/\u6570\u636E\u5E93\u540D?characterEncoding=utf-8
jdbc.username=\u7528\u6237\u540D
jdbc.password=\u7528\u6237\u5BC6\u7801

链接工厂:
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- 加载mybatis核心配置文件 -->
		<property name="configLocation" value="classpath:SqlMapConfig.xml" />
		<!-- 别名包扫描 -->
		<property name="typeAliasesPackage" value="domain" />
	</bean>

以下方式任选一种配置即可

传统Dao:
	<bean class="dao.impl.UserDaoImpl">
		<property name="sqlSessionFactory" ref="sqlSessionFactory" />
	</bean>
动态代理方式一:
	<bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true" lazy-init="true">
      <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean> 
 	<!-- 配置一个接口 -->
	 <bean parent="baseMapper">
     	 <property name="mapperInterface" value="mapper.UserMapper" />
    </bean> 
动态代理方式二(扫描包):
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    	<!-- basePackage多个包用","分隔 -->
    	<!-- <property name="basePackage" value="mybatis.mapper" /> -->
    	<property name="basePackage" value="mapper" />
    </bean>

总体的Spring配置文件

<?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:jdbc.properties" />

	<!-- 数据库连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.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="maxActive" value="10" />
		<!-- 最大空闲数 -->
		<property name="maxIdle" value="5" />
	</bean>
	
	<!-- SqlSessionFactory配置 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- 加载mybatis核心配置文件 -->
		<property name="configLocation" value="classpath:SqlMapConfig.xml" />
		<!-- 别名包扫描 -->
		<property name="typeAliasesPackage" value="domain" />
	</bean>
    
    <!-- 动态代理,第二种方式:包扫描(推荐): -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    	<property name="basePackage" value="mapper" />
    </bean>
</beans>

测试文件

@Test
	public void testSelectByPrimaryKey() {
		//加载配置文件
	    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");	
		//获取代理类
		UserMapper userMapper = applicationContext.getBean(UserMapper.class);		
		User user = userMapper.selectByPrimaryKey(30);
		System.out.println(user);
	}

总结

以上就是一个完整的整合方案了,可以尝试的试试,下面给一个日志文件

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

猜你喜欢

转载自blog.csdn.net/qq_37871033/article/details/89036834