mybatis与Spring的整合

mybatis与Spring的整合

1. 创建com.Wang的包在该包下分别创建mapper pojo 的包

框架大致为:
在这里插入图片描述

2. 创建User实体类

package com.Wang.pojo;

import lombok.Data;

@Data
public class User {
    private int id;
    private String name;
    private String pwd;
}

3.配置mybatis.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

<!--    <properties resource="db.properties"/>-->

    <typeAliases>
        <typeAlias type="com.Wang.pojo.User" alias="User"/>
    </typeAliases>


<!--
也交给spring-dao.xml来配置了


一般在mybatis.xml中只会留下,一个别名管理typeAliases 与settings设置

-->
<!--    <mappers>-->
<!--        <mapper resource="com/Wang/mapper/UserMapper.xml"/>-->
<!--    </mappers>-->
</configuration>

4.在mapper下创建UserMapper接口同时配置UserMapper.xml

package com.Wang.mapper;

import com.Wang.pojo.User;

import java.util.List;

public interface UserMapper {
    List<User> selectUser();
}

<?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.Wang.mapper.UserMapper">

    <select id="selectUser" resultType="User">
        select * from mybatis.user
    </select>
</mapper>

5.配置spring的配置文件,将原本由mybatis配置文件的内容,交给spring来代理,spring-dao.xml(可是实现复用)

<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--DataSource:使用Spring的数据源替换Nybatis的配置
    c3p0  dbcp  druid我们这里使用Spring提供的
    JDBC : org.springframework.jdbc.datasource

    这里将原本mybatis所管理的数据源 交由spring来进行管理
    关键词为: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?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>


    <!--   sqlSessionFactory
     在 MyBatis-Spring 中,可使用 SqlSessionFactoryBean
     来创建 SqlSessionFactory。

     要配置这个工厂 bean,
     只需要把下面代码放在 Spring 的 XML 配置文件中:



     个人理解:这里就是将dataSource的数据源,给SqlSessionFactoryBean
     让SqlSessionFactoryBean来创建一个sqlSessionFactory,
     相当于utils类下的mybatisUtils工具类
     -->

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!--   与mybatis的配置文件进行绑定
             注:在这里我们可以配置所有在mybatis.xml中的配置
             甚至可以完全替代
             -->
        <!--        绑定mybatis配置文件的地址-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--        绑定mapper.xml的配置文件地址-->
        <property name="mapperLocations" value="classpath:com/Wang/mapper/*.xml"/>
    </bean>






<!--    ==================================以上为固定格式,可拿来复用=====================================-->

    <!--sqlSessionTemplate:就是我们使用的sqLSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--只能使用构造器注入sqlSessionFactory,因为它没有set方法-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>



</beans>

6.在mapper下创建UserMapperImpl类,用来得到sqLSession

package com.Wang.mapper;

import com.Wang.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

public class UserMapperImpl implements UserMapper{

    //我们的所有操作,都使用sqLSession来执行,在原来,现在都使用SqLSessionTemplate;
    private SqlSessionTemplate sqlSession;

    //在spring中万物皆注入set
    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    @Override
    public List<User> selectUser() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.selectUser();
    }
}

7.创建一个applicationContext.xml来整合beans

<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

<!--    用applicationContext.xml来整合beans,-->
    <import resource="spring-dao.xml"/>


    <bean id="userMapper" class="com.Wang.mapper.UserMapperImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>



</beans>

8.测试类

import com.Wang.mapper.UserMapper;
import com.Wang.pojo.User;
import com.Wang.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class MyTest {
    @Test
    public void test(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

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

        List<User> users = userMapper.selectUser();


        for (User user : users) {
            System.out.println(user);
        }
    }
}

方式2:在mapper下创建UserMapperImpl2类,用来得到sqLSession

package com.Wang.mapper;

import com.Wang.pojo.User;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import java.util.List;

public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{

    @Override
    public List<User> selectUser() {
       return getSqlSession().getMapper(UserMapper.class).selectUser();
    }
}

使用该方法时可以省略,在spring-dao.xml中的

 <!--sqlSessionTemplate:就是我们使用的sqLSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--只能使用构造器注入sqlSessionFactory,因为它没有set方法-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三横同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值