本篇主要讲怎么使用spring整合mybatis
spring的特点是IOC和AOP,IOC是控制反转,即创建对象。整合spring和mybatis正是使用了spring的这种特点。我们可以使用spring创建如下对象,来整和mybatis。
实现步骤:
创建maven,数据库和实体类、前几篇已经记录过不在进行一一演示。
在maven的基础上导入依赖
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.1.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>5.1.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.1.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.7version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>*.xmlinclude>
<include>**/*.xmlinclude>
includes>
<filtering>truefiltering>
resource>
resources>
build>
编写配置文件applicationContext.xml
<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">
<context:component-scan base-package="com.lzl"/>
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="org.apache.ibatis.datasource.pooled.PooledDataSource">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.lzl.model"/>
bean>
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lzl.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
bean>
beans>
编写mybatis-config.xml配置
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
configuration>
db.properties 数据源配置
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/库名?useSSL=false
jdbc.username=用户名
jdbc.password=密码
log4j.properties日志配置
log4j.rootLogger=error
log4j.logger.com.qf.mapper=debug,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
服务层的实现
public interface UserService {
User findUserById(int id);
}
@Service
public class UserServiceImpl implements UserService {
// 设置Dao属性
@Autowired // 自动注入Mapper
private UserMapper userMapper;
@Override
public User findUserById(int id) {
User user = userMapper.findUserById(id);
return user;
}
}
持久层的实现
public interface UserMapper {
User findUserById(int id);
}
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qf.mapper.UserMapper">
<select id="findUserById" parameterType="int" resultType="User">
select * from user where id = #{id}
select>
mapper>
测试类编写
public class TestSpringMybatis {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service = context.getBean("userServiceImpl", UserService.class);
User user = service.findUserById(51);
System.out.println(user);
}
}
至此已经将spring和mybatis整合完毕
上述整合步骤中,将所有的mybatis配置文件中的内容全部整合到了applicationContext.xml中。我们还可以使用另外一种方式来进行整和
这里仅作一种配置别名使用mybatis-config.xml演示
<configuration>
<typeAliases>
<package name="com.qf.model"/>
typeAliases>
configuration>
在applicationContext.xml中加载该配置
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="dataSource" ref="dataSource"/>
bean>
整合完毕以后,log4j日志不显示。原因在于整合mybatis后,mybatis自己无法确定到底使用哪一个日志框架.所以,需要在mybatis的全局配置文件中,指定日志的实现方案为log4j。
<configuration>
<settings>
<setting name="logImpl" value="LOG4J"/>
settings>
configuration>