Java从入门到入土之Spring-mybtis整合篇

本文详细介绍如何在Spring框架中整合MyBatis,包括所需jar包、配置文件编写、数据库连接配置、Mapper接口及映射文件创建、Service层实现及测试类编写。通过具体实例,展示如何使用Spring管理MyBatis的SqlSessionFactory,以及如何进行数据库操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring-mybatis整合需要jarbao
spring jar包
在这里插入图片描述mybatis jar包,注意mybatis-spring连接包版本有要求
在这里插入图片描述
配置文件

<?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <!--开启spring扫描注解-->
    <context:component-scan base-package="com"/>
    <!--加载外部数据库连接信息-->
    <context:property-placeholder location="dataSource.properties"/>
    <!--配置数据库参数,配置spring自带的数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!--引用外部文件,提高扩展性。也可以写固定值-->
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.name}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    <!--将mybatis的SqlSessionFactory 交给spring管理-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--连接数据库的数据-->
        <property name="dataSource" ref="dataSource"/>
        <!--扫描mybatis映射文件,也可以在下面扫描绑定接口,不扫描映射文件-->
        <property name="mapperLocations" value="classpath:com/mapper/*Mapper.xml"></property>
    </bean>
    <!--扫描-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--指定SqlSessionFactory,如果只定义了一个可以不指定-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--扫描映射文件绑定接口,可省略上面扫描映射文件-->
        <property name="basePackage" value="com.mapper"/>
    </bean>
</beans>

daraSource 数据库信息

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/forum
jdbc.name=root
jdbc.password=123456

Mapper接口

package com.mapper;

import com.pojo.Student;

import java.util.List;

public interface StudentMapper {

    List<Student> selectAll();
}

Mapper映射文件

<?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.mapper.StudentMapper">
    <resultMap type="com.pojo.Student" id="student">
        <id property="sid" column="sid"/>
        <result property="name" column="name"/>
    </resultMap>
    <select id="selectAll" resultMap="student">
        select sid,name from student
    </select>
</mapper>

Service层代码

package com.service;

import com.mapper.StudentMapper;
import com.pojo.Student;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/*指定该类为Service层*/
@Service
public class StudentService {

    @Resource
    private StudentMapper studentMapper;

    public List<Student> getAll(){
        List<Student> students = studentMapper.selectAll();
        return students;
    }
}

测试类

package com.test;


import com.pojo.Student;
import com.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class Test1 {

    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentService studentService = (StudentService) ac.getBean("studentService");
        List<Student> all = studentService.getAll();
        System.out.println(all);
    }
}

运行结果

2019-07-25 16:33:12  [ main:1 ] - [ DEBUG ]  ==>  Preparing: select sid,name from student 
2019-07-25 16:33:12  [ main:42 ] - [ DEBUG ]  ==> Parameters: 
2019-07-25 16:33:12  [ main:64 ] - [ DEBUG ]  <==      Total: 2
[Student [sid=1, name=赵六], Student [sid=2, name=赵武]]

Process finished with exit code 0
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值