Spring和Mybatis的整合

首先先说一下我对Spring与mybatis整合的这一过程的自我理解:
Spring与Mybatis整合无非就是把Mybatis的全部动作的结果(生成一个sqlsession)最终封装为一个对象(实现类对应的)的属性,之后将实现类注入到Spring容器中,进行属性的配置即可。最大的区别就是这个对象可由Spring自动创建,我们客户层只需要从Spring容器中提取对象即可,再执行相应的方法。

接下来将主要的步骤列在下面:
①在Spring容器中编写数据源配置
Spring的配置文件不变,需要注入aop

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

数据源配置:

 <!--DataSource:使用Spring的数据源替换Mybatis的配置。也可以用C3P0,dbcp,druid
    我们这里使用Spring提供的JDBC:org.springframework.jdbc.datasource
    -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone = Asia/Shanghai"/>
        <property name="password" value="1437697157"/>
        <property name="username" value="root"/>
    </bean>

②在Spring中创建sqlSessionFactory

<!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/xkl/Dao1/UserMapper.xml"/>
    </bean>

③在Spring创建sqlSessionTemplate:就是我们使用的sqlSession

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">

        <constructor-arg  index="0" ref="sqlSessionFactory"/>
    </bean>

④创建一个实现类

package com.xkl.Dao1;

import com.xkl.Pojo.User;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

public class UserMapperImpl implements UserMapper{
    //在mybatis里面用的都是sqlsession来实现的,在Spring中就用sqlsessionTemplate来实现。
    private SqlSessionTemplate sqlSession;

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSession){
        this.sqlSession = sqlSession;
    }

    public List<User> getList2() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        return mapper.getList2();
    }
}

⑤将实现类注入到Spring容器中

<bean id="userMapper" class="com.xkl.Dao1.UserMapperImpl">
        <property name="sqlSessionTemplate" ref="sqlSession"/>
    </bean>

在这里需要说明的一点是为了使分工更加明确,我们可以创建两个Spring容器,在这里插入图片描述
一般Mybatis-config.xml文件中就进行别名(typeAliases)和设置(settings),Spring-Mybatis.xml就实现①②③步(和Mybatis中的工具类一样),将其统一导入到applicationContext.xml中,最后用的时候就从applicationContext.xml容器中提取就可以了。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
<import resource="Spring-Mybatis.xml"/>


    <bean id="userMapper" class="com.xkl.Dao1.UserMapperImpl">
        <property name="sqlSessionTemplate" ref="sqlSession"/>
    </bean>


</beans>

最后进行测试文件的编写:

import com.xkl.Dao1.UserMapper;
import com.xkl.Pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest2 {
    @Test
    public void SpringMybatisTest(){
        ApplicationContext Context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper usermapper = Context.getBean("userMapper", UserMapper.class);
        for(User user:usermapper.getList2())
        {
            System.out.println(user);
        }


    }
}

最后总结一下在撸的过程出现的Bug(掉头发的教训):
在这里插入图片描述
这里的value写快了,应该是ref的,
不然就会出现:
Caused by: java.lang.IllegalStateException: Cannot convert value of type ‘java.lang.String’ to required type ‘javax.sql.DataSource’ for property ‘dataSource’: no matching editors or conversion strategy found
的错误。
②原先Mybatis-config里面的:
在这里插入图片描述
mapper应该删除。因为在Spring-Mybatis.xml已经注入了mapperlocation
在这里插入图片描述
不然就会出现:
Caused by: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.xkl.Dao1.UserMapper.getList2. please check com/xkl/Dao1/UserMapper.xml and class path resource [com/xkl/Dao1/UserMapper.xml]。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值