Mybatis_Spring 整合
MyBatis 和 Spring 两大框架已经成了 Java 互联网技术主流框架组合,Spring 配合 MyBatis 的高灵活、可配置、可优化 SQL 等特性,完全可以构建高性能的大型网站。它们经受住了大数据量和大批量请求的考验,在互联网系统中得到了广泛的应用。
下面来讲述下两者的整合步骤:
整合步骤
- 在pom.xml中添加依赖包
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
-
在xml配置文件中配置数据源
要配置数据源要有数据 ,新建jdbc.properties文件
jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8 jdbc.driver=com.mysql.jdbc.Driver jdbc.user=root jdbc.password=root
在配置文件中引入此文件
<context:property-placeholder location="classpath:jdbc.properties"/>
配置数据源
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
- 配置Spring、Mybatis整个的管理Bean,并指定XML Mapper文件的路径
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 指定XML Mapper文件的路径 -->
<property name="mapperLocations" value="classpath:/mapper/*"/>
</bean>
Mapper 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.lanou3g.mybatis.dao.BookDao">
<resultMap id="book" type="com.lanou3g.mybatis.bean.Book">
<id column="bid" property="id" />
<result column="bname" property="bname" />
<result column="author" property="author" />
<result column="author_gender" property="authorGender" />
<result column="price" property="price" />
<result column="description" property="description" />
<association property="bookType" javaType="com.lanou3g.mybatis.bean.BookType">
<id column="t_id" property="id"/>
<result column="tname" property="tname"/>
</association>
</resultMap>
<select id="queryBooks" resultMap="book">
select b.*,t.*, b.id bid, t.id t_id from book b, booktype t where b.btype = t.id;
</select>
</mapper>
- 通过mybatis scheme来自动扫描所有的Mapper接口
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd">
<!-- 自动扫描所有Mapper接口 -->
<mybatis:scan base-package="com.lanou3g.spring.mapper" />
</beans>
Mapper接口类示例:
package com.training.spring.mapper;
import com.training.spring.bean.Message;
import com.training.spring.bean.MessageExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
@Component
public interface MessageMapper {
int deleteByPrimaryKey(Integer id);
int insert(Message record);
List<Message> selectByExample(MessageExample example);
Message selectByPrimaryKey(Integer id);
int updateByExampleSelective(@Param("record") Message record, @Param("example") MessageExample example);
int updateByExample(@Param("record") Message record, @Param("example") MessageExample example);
}
- 使用测试
MessageServiceImpl实现类调用了MessageMapper接口的方法(注意不是继承)
package com.training.spring.serive;
import com.training.spring.bean.Message;
import com.training.spring.mapper.MessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class MessageServiceImpl {
@Autowired
private MessageMapper messageMapper;
public List<Message> queryAll(){
return messageMapper.selectByExample(null);
}
public int insert(){
Message record=new Message();
record.setSubject("测试一下");
record.setStatus(2);
return messageMapper.insert(record);
}
public int testDeleteByPrimaryKey(Integer id){
return messageMapper.deleteByPrimaryKey(id);
}
}
经过上面的几步配置后, 我们就可以直接从Spring的IOC容器中获取Mapper操作接口了,在Application类中获取使用。
public class Application {
public static void main(String[] args) throws SQLException {
// 1. 初始化IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2. 从容器中获取Service对象(Mapper对象已经自动注入到Service中了)
MessageServiceImpl messageService = ctx.getBean(MessageServiceImpl.class);
// 3. 执行查询所有方法
List<Message> messageList = messageService.queryAll();
log.info("" + messageList);
}
}