文章目录
Spring和mybatis的整合
环境配置
1.导入jar包依赖和maven静态资源过滤
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--mybatis和spring的整合jar包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!--导入aop核心包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.3</version>
<scope>compile</scope>
</dependency>
<!--spring连接数据库的包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.4</version>
</dependency>
</dependencies>
<build>
<!--解决maven静态资源过滤的问题-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
2.编写实体类和接口类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String name;
private String password;
}
package com.bao.mapper;
import com.bao.domain.User;
import java.util.List;
public interface UserMapper {
List<User> findAllUser();
}
3.编写mybatis-config.xml和db.properties
<?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>
<properties resource="db.properties"></properties>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/bao/mapper/UserMapper.xml"/>
</mappers>
</configuration>
4.编写UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bao.mapper.UserMapper">
<select id="findAllUser" resultType="com.bao.domain.User">
select * from user1;
</select>
</mapper>
5.编写spring-mybatis.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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
//扫描db.properties 使用el表达式的方式将jdbc配置获取
<context:property-placeholder location="classpath:db.properties"/>
//经过导包druid,所以使用DriverManagerDataSource配置数据库
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
//在xml配置文件中,使用SqlSessionFactoryBean来创建session工厂,
第一行属性表示上文中设置的数据源信息,第二行属性则为configLocation属性,用来指定Mybatis的XML配置文件路径,这里的绑定是为了获取mybatis中定义的setting标签或者typeAliases 这里所绑定的Mybatis配置文件不需要是一个完整的Mybatis配置,SqlSessionFactoryBean或使用其中的值定制自己的Environment
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
//若mapper和mapper.xml处于同一个包下,则会自动扫描mapper.xml,若不在同一个包下,则需要通过mapperLocations配置,其中地址由/分隔
</bean>
//
//SqlSessionTemplate是mybatis提供的持久层访问模块化的工具,线程安全,可通过构造参数或依赖注入SqlSessionFactory实例
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
</bean>
//在使用sqlsession时将其放置在mapper的实现类中,即可以在使用该方法是调用session对象
<bean id="userMapperImpl" class="com.bao.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"></property>
</bean>
</beans>
6.使用MapperScannerConfigurer
使用MapperScannerConfigurer没有必要在Spring的xml文件中注册所有的映射器,他会查找类路径下的映射器,并自动将他们创建为MapperFactoryBeans
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
//通过sqlSessionFactoryBeanName可以先将beanname注入到SQLSessionFactoryBeanName,等到spring初始化完成再通过beanname构建sqlSessionFactory
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
///扫描该路径下的映射器
<property name="basePackage" value="com.bao.mapper"></property>
</bean>
总结
Java类中使用sqlsession
mybatis中读取到的配置文件会被封装到Configuration类中,使用该类对应到实际使用中往往通过SqlSessionFactoryBuilder().build,来创建sqlSessionFactory对象,然后通过getMapper对应响应的映射器,执行响应的SQL语句。
String resource = "spring-mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
而对于数据库连接部分,由于是初步接触,所以只是粗略的看了一下源码,主要是通过SqlSessionFactoryBuilder().build方法,通过和Configuration类中的信息以及mybatis中的environments的信息相匹配,主要是凭借DefaultSqlSessionFactory.openSession();获取到对应的数据库配置。
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
xml中调用sqlSession
数据库配置方法都是一致的:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
映射器部分有不同:
使用SqlSessionTemplate
是sqlsession的一个实现类,本质上就是sqlsession,需要有一个mapper实现类才能将mapper封装起来,即通过代理模式来增强mapper中的方法。可以在类中添加一个实现类,在xml中注入
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
</bean>
<bean id="userMapperImpl" class="com.bao.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"></property>
</bean>
使用MapperScannerConfigurer
使用扫描的方法直接扫描对应包下的映射器,进行sqlsession和映射器的配置
总结
框架与普通代码相比,整合了许多内部逻辑,各个框架都有各个框架的明显之处,但是除了框架的核心逻辑之外,其中封装的方法和普通类中的方法大同小异,其中的逻辑也是相同的。