然后说一下这个,​spring整合mybatis各个配置文件

1. pom.xml

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.23</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.6</version>
        </dependency>
        <!--spring操作数据库,还需要一个spring-jdbc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.7</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.6</version>
        </dependency>
    </dependencies>

2.spring-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--DataSource:使用Spring的数据源替换Mybatis的配置
        我们这里使用Spring提供的jdbc:org.springframework.jdbc.datasource.DriverManagerDataSource-->
    <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=true&useUnicode=true&characterEncoding=UTF-8"/>
        <property name="username" 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"/>
        <property name="mapperLocations" value="classpath:com/tansty/mapper/*.xml"/>
    </bean>
    <!--SqlSessionTemplate就是我们使用的sqlSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--构造器注入-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
</beans>

3.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核心配置文件-->
<configuration>
    <!--起别名-->
    <typeAliases>
        <!--扫描包,默认别名为类的首字母小写-->
        <package name="com.tansty.pojo" />
    </typeAliases>
</configuration>

4.applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<import resource="spring-dao.xml"/>
	//注入sqlsessiontemplate
    <bean id="UserMapper" class="com.tansty.mapper.UserMapperImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>
	//注入sqlsessionFactory
    <bean id="UserMapper2" class="com.tansty.mapper.UserMapper2Impl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
</beans>

5.UserMapperImpl

package com.tansty.mapper;
import com.tansty.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
/**
 * description:  <br>
 * date: 2021/6/20 13:49 <br>
 * author: ztz <br>
 * version: 1.0 <br>
 */
public class UserMapperImpl implements UserMapper{<!-- -->
    private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {<!-- -->
        this.sqlSession = sqlSession;
    }
public List&lt;User&gt; getUser() {<!-- -->
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.getUser();
}
}

6.UserMapper2Impl

package com.tansty.mapper;
import com.tansty.pojo.User;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
/**
 * description:  &lt;br&gt;
 * date: 2021/6/20 14:23 &lt;br&gt;
 * author: ztz &lt;br&gt;
 * version: 1.0 &lt;br&gt;
 */
public class UserMapper2Impl extends SqlSessionDaoSupport implements UserMapper {<!-- -->
    public List&lt;User&gt; getUser() {<!-- -->
        return getSqlSession().getMapper(UserMapper.class).getUser();
    }
}

6.项目结构

<img src=”https://img-blog.csdnimg.cn/20210620143405131.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3RhbnN0eV96aA==,size_16,color_FFFFFF,t_70″ alt=”在这里插入图片描述”>

正文完