Spring和MaBatis整合(xml版与纯注解版)

时间:2021-04-24 01:13:42

Spring和MyBatis整合xml版:

先瞅一眼各种文件路径:
Spring和MaBatis整合(xml版与纯注解版)

  • 将之前mybatis中的测试类中的SqlSessionFactory(通过其openSession()来获得对象SqlSession),和Mybatis配置文件中的数据源(url,username等)揉在一起放入Spring配置文件中,对了,还有指定MyBatis核心配置文件的位置
  • 将Mapper接口放进去,扫描包含接口的包,会自动生成实现类
  • 将之前MyBatis中的创建接口代理的代码放入Spring中
  • 将之前Spring中的properties文件放进去
  • 将所需要的包扫描(讲几个层和Mapper接口放进去)
  • 事务管理
   <?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: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
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    

<!--    扫描组件的包:数据层,业务层等-->
    <context:component-scan base-package="com.itjh"/>
    <context:property-placeholder location="jdbcMm.properties"/>
    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
<!--  创建sqlsession对象  -->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!--  注入数据源 -->
        <property name="dataSource" ref="dataSource"></property>
<!--  指定mybatis核心配置文件     -->
        <property name="configLocation" value="MybatisOnlyconfig.xml"></property>
</bean>


    <!--   Mapper扫描配器,扫描Mapper接口,自动生成实现类   -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.itjh.mapper" ></property>
    </bean>

<!-- 事务管理器   -->
    <bean id="manager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <tx:annotation-driven transaction-manager="manager" />
</beans>

从测试类开始看:Dao,Service类等等都加入了注解,调用业务层的实现类

import com.itjh.pojo.Dao;
import com.itjh.service.ServiceDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;

public class Test01 {
    public static void main(String[] args) {
        ApplicationContext applicationContext =new ClassPathXmlApplicationContext("springConfig.xml");
        ServiceDao serviceDao=(ServiceDao) applicationContext.getBean("servicedaoimple");
        List<Dao> daos = serviceDao.selectall(1);
        System.out.println(daos);
    }
}

看业务层的实现类之前先看看其实现的接口:这个接口定义了调用数据的方法,方便实现类ServiceDaoImple重写了这些方法之后对数据进行调用(Dao类的里面数据)

package com.itjh.service;

import com.itjh.pojo.Dao;
import java.util.List;

public interface ServiceDao {
    int add(Dao dao);
    List<Dao> selectall(int id);

}

再看实现类:通过给方法传递参数来让方法中的调用成功,当然方法中的调用需要到另一个接口Mapper,于是用到 @Autowired来代替之前配置文件中的<property>标签,有了这个接口的注入

package com.itjh.service;

import com.itjh.mapper.mapper;
import com.itjh.pojo.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service("servicedaoimple")
public class ServiceDaoImple implements ServiceDao{
    public ServiceDaoImple(){
        System.out.println("巴黎");
    }
    @Autowired
    public mapper map;

    public int add(Dao dao) {
        return map.insert(dao);
    }


    public List<Dao> selectall(int id) {
        System.out.println("茜茜");
        return map.selectall(id);
    }
}

来到Mapper接口中:在最开始的那个Spring配置文件中扫描接口的标签由于会自动生成接口的实现类,过程中就将位置一样的Mapper映射文件与Mapper接口的实现类挂钩了,于是实现了再业务层中对于Mapper接口的方法成功调用后,由于其实现类和映射文件的关系,就自动的进入映射文件中找到和接口方法相同的id,执行sql语句

package com.itjh.mapper;

import com.itjh.pojo.Dao;

import java.util.List;

public interface mapper {
    int insert(Dao dao);
    List<Dao> selectall(int id);
}

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.itjh.mapper.mapper">
    <insert id="insert">
        insert into bank (name,age) values (#{name},#{age})
    </insert>
    <select id="selectall" resultType="com.itjh.pojo.Dao">
        select * from bank where id = #{id}
    </select>
</mapper>

对了还有MyBatis核心配置文件:不过里面的数据源标签,扫描映射文件标签都没了(映射文件加载变成现在的Spring配置文件中对于包含接口的包的扫描产生实现类,且由于接口与映射文件路径相同,于是加载出了映射文件),就简单的加一个日志:

<?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.itjh.mapper.mapper">
    <insert id="insert">
        insert into bank (name,age) values (#{name},#{age})
    </insert>
    <select id="selectall" resultType="com.itjh.pojo.Dao">
        select * from bank where id = #{id}
    </select>
</mapper>

最后还有properties文件:用于搭配Spring配置文件中的数据源中${}

梳理一遍:

整体流程: 将参数从测试类传进业务层的实现类(方法由业务层的接口来定义,业务层实现类实现),再传进Mapper接口的方法,再因为其实现类和映射文件的关系,直接进入了映射文件进行sql语句的执行

Spring和MyBatis整合纯注解版:

将原本xml中的配置全部抽离出来:

  • 数据源
  • 将数据源放入SqlSessionFactoryBean对象中并且返回
  • MapperScannerConfigurer来加载Mapper接口(接口和Mapper映射文件同路径)
  • xml型的扫描和propertis文件现在放在纯注解的配置类上

数据源(SpringCofig类)与propertis文件:应当引用import javax.sql.DataSource;别和import javax.activation.DataSource;混淆了,二者不同点

package com.springcofig;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;


public class SpringCofig {
    @Value("${jdbc.Driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Bean
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        dataSource.setUrl("jdbc:mysql://localhost:3306/parent?useSSL=false");
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        return dataSource;
    }
}

propertis文件

jdbc.username=root
jdbc.Driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql:///parent
jdbc.password=123456

MyBatis核心文件的注解型:看上面第一段话的二三行

  • public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource)中的DataSource属于引用类型,会自动的在容器中寻找DataSource的bean,而SpringCofig类已经用@Bean注册了DataSource,所以这里就可以直接注入了,代替了配置文件中的<property>标签
  • MapperScannerConfigurer扫描的是包含接口的包,会自动生成实现类
package com.springcofig;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;

public class MybatisConfig {
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer=new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.mapp");
        return mapperScannerConfigurer;
    }
}

Mapper接口:使用注解的方式代替了映射文件繁琐的sql语句,当然只有语句简单的情况下才会用注解的方式而不用映射文件的方式

package com.mapp;

import com.pojo.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;

public interface Mapperinter {
    @Select("select * from bank")
    List<User> selectall();
}

然后就业务层的接口和实现类:

package com.service;

import com.pojo.User;

import java.util.List;

public interface ServiceIm {
    public List<User> selectall();
}
package com.service;

import com.mapp.Mapperinter;
import com.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class ServiceImplement implements ServiceIm {

    @Autowired
    private Mapperinter mapperinter;
    public List<User> selectall() {
        return mapperinter.selectall();
    }
}

上面实现类所调用的Mapper接口:返回值为集合

package com.mapp;

import com.pojo.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;

public interface Mapperinter {
    @Select("select * from bank")
    List<User> selectall();
}

User数据层:

package com.pojo;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

@Component
public class User {
    private int age;
    private String name;
    private String gander;
    private int id;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public String getGander() {
        return gander;
    }

    public void setGander(String gander) {
        this.gander = gander;
    }

    public int getId() {
        return id;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", gander='" + gander + '\'' +
                ", id=" + id +
                '}';
    }
}

测试类:

import com.config;
import com.pojo.User;
import com.service.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.List;

public class TestDay01 {
    public static void main(String[] args) {
        ApplicationContext applicationContext =new AnnotationConfigApplicationContext(config.class);
        ServiceIm serviceIm =(ServiceIm) applicationContext.getBean(ServiceImplement.class);
        List<User> list=serviceIm.selectall();
        System.out.println(list);
    }
}

总结:主线任务是调用bean类型为ServiceImplement.class的方法,然后调用其中的方法,然后会进入到Mapper接口,得到需要的东西
支线任务是那些Spring配置类MyBatis配置类等等都是给上面提供各种条件