Spring整合Mybatis(基于Dao层接口实现类)

Spring整合Mybatis笔记,鉴于网上的资料鱼龙混杂,查看到的部分资料甚至把我带进沟里了,特此自己动手记录笔记:

首先我们知道常规的Mybatis使用基础需要定义一个Mybatis配置文件,一个Mapper映射文件,然后在需要用到的地方读取配置文件,代码如下:

首先我们的数据库很简单,如下:

表名为account

列名分别为id,name,money

对应的实体类如下:

package com.varlor.pojo;

public class Account {
    private Integer id;
    private String name;
    private Double money;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}

我们定义了一个接口如下:

单独使用Mybatis的配置文件如下(不用这个),因为我们这次需要将Spring和Mybatis进行整合,所以下面关于数据库的配置不需要在这个文件里:

整合后的配置文件(用这个)

只需要在配置文件里写入mappers,告诉mybatis映射文件在哪里

也可以不写mybatis的配置文件,完全集成到spring配置文件内,这样就不需要mybatis配置文件了:

映射文件如下:

至于数据库相关配置就整合到了Spring配置文件中(因为我这里用了C3P0连接池,所以里面有点不一样):

<?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:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
">
    <!--数据库相关参数配置properties文件引入-->
    <context:property-placeholder location="db.properties"/>
    <!--接口实现类-->
    <bean id="accountMapperImpl" class="com.varlor.mapper.AccountMapperImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    <!--数据源-->
    <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" 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="configLocation" value="mybatis-config.xml"/>
        <property name="typeAliases" value="com.varlor.pojo.Account"/>
    </bean>
</beans>

 

这里需要注意一下,我们之前在单独使用Mybatis的时候是如下引入配置文件并获得session然后使用session进行相关操作的:

 //1.读取配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.创建SqlSessionFactory工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(resourceAsStream);
        //3.使用工厂生产SqlSession对象
        SqlSession sqlSession = factory.openSession();

 获取到session后我们可以使用session来进行相关操作:

现在我们需要集成在spring里面,因为我们第一个教程是基于Dao层接口实现类,所以我们这个时候需要AccountMapperImpl实现类,这个类需要集成一个SqlSessionDaoSupport类并且实现AccountMapper接口,SqlSessionDaoSupport这个类下有如下方法:

我们会发现可以获取到SqlSession,所以我们使用this.getSqlSession获取到一个session,并且一样的使用session进行相关操作,代码如下:

public class AccountMapperImpl extends SqlSessionDaoSupport implements AccountMapper{
    public List<Account> findAll() {
        
        return this.getSqlSession().selectList("com.varlor.mapper.AccountMapper.findAll");
    }
}

 selectList()括号里的参数代表的是Mapper映射文件,com.varlor.mapper.AccountMapper代表我的映射文件位置,findAll代表映射文件里的id

测试类:

@Test
    public void testMybatis(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("springconfig.xml");
        AccountMapper accountMapper = (AccountMapper) ac.getBean("accountMapperImpl");
        List<Account> accounts = accountMapper.findAll();
        for (Account account:accounts){
            System.out.println(account);
        }

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值