欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring 整合Mybatis dao原始方法

程序员文章站 2022-10-27 20:10:22
先看一下项目图,基本就理解了整合的内容 这次主角不再是Mybats的配置文件SqlMapConfig.xml了,而是Spring的applicationContext.xml applicationContext.xml Spring同样开始整合数据源datasource IoC控制反转生成sqlS ......

先看一下项目图,基本就理解了整合的内容

Spring 整合Mybatis dao原始方法

这次主角不再是Mybats的配置文件SqlMapConfig.xml了,而是Spring的applicationContext.xml

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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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-4.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">

    <context:property-placeholder location="classpath:db.properties" />
    
    <!-- 数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
        <property name="typeAliasesPackage" value="com.itheima.mybatis.pojo"></property>
    </bean>
    
    <bean id="userDao" class="com.itheima.mybatis.dao.UserDaoImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>
    
    <bean id="transactionManager"    
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    
        <property name="dataSource" ref="dataSource" />    
    </bean>  
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

Spring同样开始整合数据源datasource

IoC控制反转生成sqlSessionFactory对象,注意有一个属性configLocation,用来关联Mybatis的映射文件SqlMapConfig.xml,

也是在该对象中对包进行别名操作

IoC控制反转生成要调用的对象userDao,其实现类继承SqlSessionDaoSupport,还要注入属性sqlSessionFactory

Mybatis配置文件SqlMapConfig.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>
    <!-- 加载sql映射文件 -->
    <mappers>
        <mapper resource="sqlmap/UserMapper.xml" />
    </mappers>
</configuration>

映射文件UserMapper.xml

<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 映射,存放sql语句 -->
<!-- 
namespace:分类管理sql语句
唯一确定sql语句:namespace + "." + id
 -->
<mapper namespace="test">
    <!-- 
        查询使用select标签
        id:sql语句标识符,通常也称为statement的id
        parameterType:参数类型(输入映射)
        resultType:结果类型(输出映射),配置为pojo,那么mybatis就把结果集中的每一条记录封装为指定的pojo
     -->
    <select id="queryUserById" parameterType="Integer" resultType="user">
        <!-- 
            #{}固定取参语法
            #{}取参名称:parameterType为基础数据类型的时候,名称任意,建议见名思意
                        传 入pojo的时候,取参名称为属性名
            #{}原理:预编译语句,可以防止sql注入
            #{}对字符串参数会自动添加单引号对
         -->
        select * from user where uid=#{uid}
    </select>
</mapper>

接口

package com.itheima.mybatis.dao;

import com.itheima.mybatis.pojo.User;

public interface UserDao {
    User queryUserById(int id);
}

实体类

package com.itheima.mybatis.dao;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.itheima.mybatis.pojo.User;

public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {

    public User queryUserById(int id) {
        // TODO Auto-generated method stub
        // 不需要关闭session,因为父类当中完成拦截关闭动作
        User user = super.getSqlSession().selectOne("test.queryUserById", id);
        return user;
    }

}

 测试类

package com.itheima.mybatis.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.annotation.Transactional;

import com.itheima.mybatis.dao.UserDao;
import com.itheima.mybatis.pojo.User;

public class TestUserDao {
    private ApplicationContext ctx;
    
    @Test
    @Transactional
    public void testQueryUserById() {
        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDao = ctx.getBean(UserDao.class);
        User user = userDao.queryUserById(3);
        System.out.println(user);
    }
}