Spring整合Mybatis详细步骤

网友投稿 220 2023-01-14

Spring整合Mybatis详细步骤

一、基本介绍

所谓的Spring整合Mybatis其实说白了就是将mybatis的加载过程全权交给Spring托管,不再需要加载配置工具等一些操作,而具体的dao层操作依旧是使用mybatis去操作数据库。

1.1 mybatis使用步骤:

1.首先要写一个mybatis-config.xml核心配置文件,配置基本的环境支持:数据源、驱动、url、username、password…

2.然后编写mybatisUtil工具类,先以IO流的形式加载mybatis-config.xml资源Resource,然后通过SqlSessionFactoryBuilder工厂建造者创建一个SqlSessionFactory工厂,并且将SqlSessionFactory工厂封装为单例工厂;最后对外只抛出一个SqlSession会话获取接口!

3.最后dao层通过SqlSession获取Mapper映射器进行SQL的执行!

1.2 Spring整合mybatis步骤:

首先将mybatis-config.xml环境的配置:数据源、驱动、url、username、password…这些基础配置移交给Spring的核心配置文件application.xml文件中!创建bean对象dataSource顶替Resource。

再通过dataSource对象,创建bean对象SqlSessionFactory,这时候SqlSessionFactory工厂就交给Spring托管了!(建议按照mybatis的习俗配置成单例)

然后将mybatis-config.xml配置文件中的Configration配置以依赖注入的形式注入到SqlSessionFactory,mybatis-config.xml中的所有东西就可以在Spring中生效了。

最后将SqlSession的获取也交给Spring托管,以构造器注入的形式将SqlSessionFactory工厂注入SqlSession的依赖中!

之后所有的dao层操作都通过getBean的形式获取SqlSession,然后在执行SQL;对比上面mybatis使用步骤,整合基本上就是将mybatis的加载全权交由Spring掌管,仅此而已。

二、mybatis核心配置

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

三、Spring核心配置

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

https://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

https://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop

https://springframework.org/schema/aop/spring-aop.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

https://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

https://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop

https://springframework.org/schema/aop/spring-aop.xsd">

四、数据库与对应的POJO实体类

package com.pojo;

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

@AllArgsConstructor

@NoArgsConstructor

@Data

public class User {

private int id;

private String username;

private String password;

}

五、dao层

5.1 UserMapper接口

package com.dao;

import com.pojo.User;

import java.util.List;

public interface UserMapper {

public List getUserList();

}

5.2 UserMapper.xml配置

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select *from user;

5.3 接口实现类(一)

单独使用mybatis时是不需要这个实现类的,将impl实现类做的操作数据库事情移交到service层执行。而现在需要这个实现类,内部聚合SqlSessionTemplate对象(等价于SqlSession),然后在实现方法中通过反射获取mapper,然后执行SQL即可!

import com.pojo.User;

import org.mybatis.spring.SqlSessionTemplate;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("userMapperImpl")

public class UserMapperImpl implements UserMapper{

@Autowired

private SqlSessionTemplate sqlSession;

@Override

public List getUserList() {

UserMapper mapper = sqlSession.getMapper(UserMapper.class);

List list = mapper.getUserList();

return list;

}

}

5.4 接口实现类(二)

在Spring - Mybatis最新的整合中提供了SqlSessionDaoSupport抽象类来取代内部聚合SqlSessionTemplate对象,只需要继承这个类即可。虽然明面上不需要依赖注入,但是父类是需要依赖注入一个SqlSessionFactory工厂的!

import com.pojo.User;

import org.apache.ibatis.session.SqlSession;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import java.util.List;

public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{

@Override

public List getUserList() {

SqlSession sqlSession = getSqlSession();

UserMapper mapper = sqlSession.getMapper(UserMapper.class);

List list = mapper.getUserList();

return list;

}

}

六、测试

@Test

public void test1(){

ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");

UserMapper userMapperImpl = context.getBean("userMapperImpl", UserMapper.class);

List list = userMapperImpl.getUserList();

for (User user : list) {

System.out.println(user.toString());

}

}

到此这篇关于Spring整合Mybatis详细步骤的文章就介绍到这了,更多相关Spring整合Mybatis内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:浅谈Java源码ConcurrentHashMap
下一篇:java排序算法之选择排序详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~