欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis系列教程(二)——spring整合mybatis

程序员文章站 2022-05-29 17:45:17
...

项目地址

项目地址:https://code.csdn.net/luo4105/study_mybatis/

Spring整合mybatis

实现

接着上节的思路,把数据源和mapper的映射配置在spring的配置文件,把SqlSessionFactory对象、SqlSession对象、UserMapper对象都在Spring中注册,实现spring整合mybatis。

项目结构

mybatis系列教程(二)——spring整合mybatis

mvn依赖

<properties>
	<org.springframework-version>4.3.7.RELEASE</org.springframework-version>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<java-version>1.8</java-version>
</properties>

<dependencies>
	<!-- spring -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-aop</artifactId>
		<version>${org.springframework-version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-beans</artifactId>
		<version>${org.springframework-version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version>${org.springframework-version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-core</artifactId>
		<version>${org.springframework-version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-expression</artifactId>
		<version>${org.springframework-version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-jdbc</artifactId>
		<version>${org.springframework-version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-test</artifactId>
		<version>${org.springframework-version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-tx</artifactId>
		<version>${org.springframework-version}</version>
	</dependency>
	<!-- spring -->
	
	<!-- mybatis -->
	<dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis</artifactId>
		<version>3.4.1</version>
	</dependency>
	<dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis-spring</artifactId>
		<version>1.3.1</version>
	</dependency>
	<!-- mybatis -->
	
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>5.1.35</version>
	</dependency>
	
	<dependency>
		<groupId>commons-logging</groupId>
		<artifactId>commons-logging</artifactId>
		<version>1.2</version>
	</dependency>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.12</version>
	</dependency>
</dependencies>

项目还是之前的项目,只是添加了

spring-config.xml :spring的配置文件

UserMapperTestInSpring:配置测试类

spring-config.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"
    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">
 
    <!-- 配置数据源 -->
    <bean id="dataSource"
       class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="driverClassName" value="com.mysql.jdbc.Driver" />
       <property name="url" value="jdbc:mysql://localhost:3306/study_mybatis"/>
       <property name="username" value="root"/>
       <property name="password" value="123456"/>
    </bean>
 
    <!-- 注册、配置sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <property name="dataSource" ref="dataSource" />
       <property name="mapperLocations" value="com/lc/mapper/*.xml" />
    </bean>
 
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage" value="com.lc.dao" />
       <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
</beans>

UserMapperTestInSpring

import java.util.List;
 
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.lc.model.User;
 
public classUserMapperTestInSpring {
 
    private staticClassPathXmlApplicationContext context = null;
    private static UserMapper userMapper;
 
    static {
       context = newClassPathXmlApplicationContext("com/lc/config/spring-config.xml");
       context.start();
       userMapper = context.getBean(UserMapper.class);
    }
 
    @Test
    public void select() {
       List<User>userlist= userMapper.select();
       for (User user : userlist) {
           System.out.println(user);
       }
    }
 
    @Test
    public void delete() {
       if (userMapper.delete(1) >0) {
           System.out.println("del success");
       }
    }
}

运行

mybatis系列教程(二)——spring整合mybatis

总结

注意配置中SqlSessionFactoryBean,它还有很多属性,如设置别名、设置mybatis配置文件(如果有),详细的可以看看文档:http://javadox.com/org.mybatis/mybatis-spring/1.1.1/org/mybatis/spring/SqlSessionFactoryBean.html