spring-mybatis整合步骤

spring-mybatis整合步骤

为什么要整合

如果没有整合?

在mybatis种,操作数据库需要获取SQLSession对象,而此对象是用过SQLSessionFactoryBuilder读取全局配置文件来实例化一个SQLSessionFactory,通过SQLSessionFactory来获取到SQLSession对象。

而用到整合,可以通过单例的形式来管理SQLSessionFactory对象,mybatis-spring中提供了一个SQLSessionFactoryBean类来实例化SQLSessionFactory,SQLSessionFactory中需要必须的属性是DataSource以及Mapper

整合步骤

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-z535TiXH-1617982615201)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20210409232611822.png)]

引入依赖

需要引入mybatis提供的整合jar包 mybatis-spring

<!--mybatis和spring的整合-->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.3.0</version>
</dependency>

mybatis

mybatis全局配置

mybatis-config.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>
    <!--配置懒加载、二级缓存 Setting配置需要该文件-->
    <!--配置数据源,在Spring整合过程中,数据源交给Spring管理-->
    <!--配置Mapper ,也是交给Spring管理-->
</configuration>
pojo类Student类
public class Student1 {
    private Integer SID;
    private String Sname;
    private Integer Ssex;
    private Integer Sage;
    get/set/tostring
    }
dao层StudentMapper接口
public interface StudentMapper {
    public Student1 selectStudentById(Integer id);
}
StudentMapper.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,命令空间:保证命名空间唯一,一般是对应的mapper.java的包全路径-->
<mapper namespace="com.nsh.dao.StudentMapper">
    <select id="selectStudentById" parameterType="int" resultType="com.nsh.pojo.Student1">
        select * from student where SID = #{id}
    </select>
</mapper>

spring配置

创建spring-config.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!--配置连接池c3p0-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
        <!--配置数据连接的核心配置-->
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/text_db"/>
        <property name="user" value="root"/>
        <property name="password" value="123456"/>
    </bean>
    <!--获取会话工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--加载mybatis的配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--加载mybatis的mapper配置-->
        <property name="mapperLocations" value="mapper/*.xml"/>
    </bean>
    <!--获取StudentMapper的代理对象-->
    <bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!--会话工厂-->
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        <!--指定接口文件位置-->
        <property name="mapperInterface" value="com.nsh.dao.StudentMapper"/>
    </bean>
    
</beans>

使用spring容器管理对象

public class TextDemo {
    public static void main(String[] args) {
        String path = "spring-mybatis.xml";
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(path);
       //从IOC中获取需要的对象实例
        StudentMapper studentMapper = (StudentMapper) applicationContext.getBean("studentMapper");
        Student1 student = studentMapper.selectStudentById(1);
        System.out.println(student);
    }
}

在这里插入图片描述

需要注意你的数据库名和数据库中的表要对应好

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值