参考文档:http://mybatis.org/spring/zh/index.html
1、引入包

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.9</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.7</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.9</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                    <include>**/*.tld</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                    <include>**/*.tld</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

 实体:src\main\java\com\jay\pojo\sys_user.java
2、mapper
接口:src\main\java\com\jay\mapper\SysUserMapper.java

package com.jay.mapper;
import com.jay.pojo.sys_user;
import java.util.List;
public interface SysUserMapper {
    List<sys_user> getUsers();
}

映射:src\main\java\com\jay\mapper\SysUserMapper.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="com.jay.mapper.SysUserMapper">
    <select id="getUsers" resultType="sys_user">
    select * from sys_user
    </select>
</mapper>

实现:src\main\java\com\jay\mapper\SysUserMapperImpl.java

package com.jay.mapper;
import com.jay.pojo.sys_user;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
//还可以使用 mybatis-plugin 和 通用mapper
public class SysUserMapperImpl extends SqlSessionDaoSupport implements SysUserMapper {
    //手动注入SqlSessionTemplate
//    private SqlSessionTemplate sqlSession;
//
//    public void setSqlSession(SqlSessionTemplate sqlSession) {
//        this.sqlSession = sqlSession;
//    }
    @Override
    public List<sys_user> getUsers() {
        //return sqlSession.getMapper(SysUserMapper.class).getUsers();
        return getSqlSession().getMapper(SysUserMapper.class).getUsers();
    }
}

 3、Spring实体注入src\main\resources\applicationContext.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">
    <import resource="spring-dao.xml"/>
    <bean id="sysUserMapper" class="com.jay.mapper.SysUserMapperImpl">
        <!--<property name="sqlSession" ref="sqlSessionTemplate"/>-->
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
</beans>

4、 数据源和数据连接src\main\resources\spring-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url"
                  value="jdbc:mysql://192.168.2.39:3306/saasDb?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="datasource"/>
        <!--映射mybatis-config.xml文件中的mapper配置-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/jay/mapper/*.xml"/>
    </bean>
<!--    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">-->
<!--        <constructor-arg index="0" ref="sqlSessionFactory"/>-->
<!--    </bean>-->
    <!--配置声明式事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <constructor-arg ref="datasource" />
    </bean>
    <!--结合AOP织入事务-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--给哪些方法开启事务 add/delete/update/query, * 代表所有-->
        <!--配置事务的传播特性 propagation-->
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!--配置事务切入-->
    <aop:config>
        <!--mapper下的所有类、所有方法-->
        <aop:pointcut id="txPointCut" expression="execution(* com.jay.mapper.*.*(..))"/>
        <!--在txPointCut指定的方法执行的时候都切入txAdvice事务-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>
</beans>

 5、src\main\resources\ mybatis-config.xml,mapper配置转移到spring-dao.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>
    <typeAliases>
        <package name="com.jay.pojo"/>
    </typeAliases>
</configuration>

 6、测试

@Test
public void test1() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        SysUserMapper userMapper = context.getBean(SysUserMapper.class);
        List<sys_user> users = userMapper.getUsers();
        for (sys_user u:users)
        {
            System.out.println(u.toString());
        }
}

 声明式事务:http://mybatis.org/spring/zh/transactions.html#configuration
7、spring-dao.xml增加配置自动扫描mapper包,自动注入,可以不用建xxximpl.java实现类
在sqlSessionFactory下

<!--配置mapper接口扫描包,动态的实现了mapper接口可以注入到Spring容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <!--要扫描的mapper包-->
        <property name="basePackage" value="com.jay.mapper"/>
    </bean>

 在Impl文件和applicationContext.xml中的bean就可以移除掉了。

posted on 2021-07-30 15:49  邢帅杰  阅读(66)  评论(0编辑  收藏  举报