栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring-mybtis整合过程详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring-mybtis整合过程详解

一、创建完整包 下载地址:01MyBatis基础代码.zip-Web开发文档类资源-CSDN下载

二、开始整合

1、方式1

step1:导入相关坐标



            mysql
            mysql-connector-java
            8.0.27
        
        
            org.mybatis
            mybatis
            3.5.7
        
        
            log4j
            log4j
            1.2.17
        
        
        
            org.springframework
            spring-context
            5.3.13
        
        
        
            org.springframework
            spring-jdbc
            5.3.13
        
        
        
            org.mybatis
            mybatis-spring
            2.0.6
        
        
        
            com.alibaba
            druid
            1.2.8
        
    

step2:修改daoImpl中代码

修改serviceImpl中代码

step3:创建bean.xml



    
    
    
    
    
    

    
    
        

    
        
    
    
         
    

 step4:编写测试代码

import com.woniuxy.model.Category;
import com.woniuxy.service.CategoryService;
import com.woniuxy.service.impl.CategoryServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class TestCategoryService {
    private static CategoryService categoryService = new CategoryServiceImpl();

    public static void main(String[] args) {
        testFindAllCategories();
    }

    private static void testFindAllCategories() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:bean.xml");
        CategoryService categoryService = (CategoryServiceImpl)ac.getBean("categoryService");
        List categories = categoryService.findAllCategories();
        for(Category category : categories){
            System.out.println(category.getName());
        }
    }
}

step5:测试结果

 方式2:使用SqlSessionTemplate

基于以上代码

step1:修改daoImpl代码 注意:不需要提交commit()和close()的事务提交,因为spring容器已经帮你做了

 是step2:修改bean.xml

step3:测试代码和上面一致,结果如下

 

 3、方式3 基于以上代码修改

step1:修改daoImpl

 step2:修改bean.xml

 step3:测试代码一样,测试结果

 4、方式4 可利用MapperFactoryBean创建Mapper

基于以上代码

step1:修改daoImpl

step2:修改bean.xml

 step3:测试代码和测试结果同上

4、最终解决方案 基于以上代码

step1:

step2:修改bean.xml 

 step3:删除dao整个包

 step4:测试代码同上和测试结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/667895.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号