Spring和mybatis结合,Spring管理容器,连接数据库等,mybatis负责管理sql语句,sql的入参和出参等

三种方法:

  • 1、原始dao开发(不怎么用,好奇的宝宝可以自己搜搜。是dao层继承一个整合包SqlSessionDaoSupport的类)
  • 2、mapper代理形式开发之原始版(有多少个接口,就得写多少个property)用整合包MapperFactoryBean
  • 比如
  • <property name="mapperInterface" value="cn.tsu.xiaofeng.mapperInterface.UserMapper"/>
  • 3、mapper代理形式开发之扫描包(直接写包名就行)用整合包MapperScannerConfigurer

1、原始dao开发略

2、mapper代理开发之原始版

  1. 先创建工程,把所有的包都导入进来。

    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>
<typeAliases>
<package name="cn.tsu.xiaofeng.pojo"/>
</typeAliases>
<mappers>
<package name="cn.tsu.xiaofeng.mapperInterface"/>
</mappers>
</configuration>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd "> <context:property-placeholder location="classpath:db.properties"/>
<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<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 name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean> <!--sqlsession工厂 相当于mybatis的前两句,创建 new sqlsession工厂
InputStream resource = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sqlsessionFactory = new SqlSessionFactoryBuilder().build(resource);
-->
<bean name="SqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
</bean> <!--dao接口开发方法一 -->
<bean name="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"><!--MapperFactoryBean-->
<property name="sqlSessionFactory" ref="SqlSessionFactoryBean"/>
<property name="mapperInterface" value="cn.tsu.xiaofeng.mapperInterface.UserMapper"/>
</bean>

在这里我们以前说过mybatis直接使用时是用

InputStream resource = Resources.getResourceAsStream(“SqlMapConfig.xml”);

SqlSessionFactory sqlsessionFactory = new SqlSessionFactoryBuilder().build(resource);创建sqlsession工厂的,现在通过xml文件在容器中创建sqlsession工厂用的是整合包里的SqlSessionFactoryBean

<bean name="SqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:sqlMapConfig.xml"/> </bean>因缺数据源和配置文件,故注入

测试文件junit

public class JunitInterface {
@Test
public void testMapper() throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper mapper = (UserMapper) ac.getBean(UserMapper.class);<!--在这里直接调用id也可以userMapper-->
User user = mapper.selectUser(1);<!--这里需要遵循四大原则-->
System.out.println(user);
}
}

方法2。直接把上面的bean(dao方法yi)换成下面的bean(dao方法二 扫包),用整合包中的MapperScannerConfigurer,不用SqlSessionFactoryBean

<!--dao接口开发方法二  -->
<bean name="MapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.tsu.xiaofeng.mapperInterface" />
</bean>

一旦用了方法二,再用junit测试的时候就不可以通过id使用了,因为我们没有定义name,

为什么方法二中不需要注入sqlsession工厂呢?

因为方法二会自动扫描容器中是否含有sqlsession,有的话直接用,并且把包名所有的的接口都扫描到并且注入实现类。因此当我们调用时,只需要调用实现类即可。比如

public class JunitInterface {
@Test
public void testMapper() throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper mapper = (UserMapper) ac.getBean(UserMapper.class);<!--在这里不可直接调用id也可以userMapper-->
User user = mapper.selectUser(1);<!--这里需要遵循四大原则-->
System.out.println(user);
}
}

资源测试下载

Spring_mybatis结合之1.1的更多相关文章

  1. spring_mybatis :整合

    第一步:导入相关架包(使用maven构建项目) 在pom.xml文件中导入相关依赖 1.Junit测试架包 <dependency> <groupId>junit</gr ...

  2. SpringMVC与mybatis整合

    一.逆向工程生成基础信息 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generator ...

  3. Maven创建多个子项目

    一.下载jdk并安装:下载apache-maven包,解压到指定目录.(例:D:\Java\apache-maven-3.3.9) 二.配置环境. 1.配置jdk环境 系统变量 (1)JAVA_HOM ...

  4. Spring与Mybatis整合

    一 概述 1.整合的目的 将Mapper映射器的创建任务交给Spring容器. 二 具体实现 1.创建sqlSessionFactory: <bean id="sqlSessionFa ...

  5. 框架整合——Spring与MyBatis框架整合

    Spring整合MyBatis 1. 整合 Spring [整合目标:在spring的配置文件中配置SqlSessionFactory以及让mybatis用上spring的声明式事务] 1). 加入 ...

  6. spring整合mybatis,批量扫描mapper接口出现异常

    org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component c ...

  7. Spring+Mybatis整合时 Failed to read candidate component class,Caused by:IllegalArgumentException

    Spring+Mybatis整合时Caused by: java.lang.IllegalArgumentException错误 org.springframework.beans.factory.B ...

  8. Maven项目中Spring整合Mybatis

    Maven项目中Spring整合Mybatis 添加jar包依赖 spring需要的jar包依赖 <dependency> <groupId>org.springframewo ...

  9. SSM项目之电商项目easymall(一)

    一 环境准备 软件环境:    1 jdk1.8       JAVA_HOME:是给软件用的,各种启动的软件都会寻找JAVA_HOME的环境变量:       Path:给windows用的:   ...

随机推荐

  1. mit-6.828 Lab01:Booting a PC Part2 理论知识

    Part 2 目录 Part 2 学习理论知识 反汇编 扇区 BIOS 启动过程总结 Boot loader启动过程总结 A20 gate 读boot/boot.S 和 boot/boot.c源码 - ...

  2. 【CSP2019】括号树 题解(递推+链表)

    前言:抽时间做了做这道题,把学长送退役的题. ----------------- 题目链接 题目大意:定义$()$是合法括号串.如果$A,B$是合法括号串,那么$(AB),AB$为合法括号串.现给定根 ...

  3. SpringMVC 集成 JWT验证方式

    JWT官网: https://jwt.io/ 这里以java的ssm框架为例,集成jwt. 1.pom.xml 导入jwt的包 <!-- jwt --> <dependency> ...

  4. 常见排序算法原理及JS代码实现

    目录 数组 sort() 方法 冒泡排序 选择排序 插入排序 希尔排序 归并排序 堆排序 快速排序 创建时间:2020-08-07 本文只是将作者学习的过程以及算法理解进行简单的分享,提供多一个角度的 ...

  5. 关于手机数码圈KOL的一两点感想

    复工以来,高峰时段9号线地铁上的人依旧不少,安全距离啥的肯定是不用想了,只是从原来的4G手机换成5G手机以后在某些站能接收到5G信号,我终于能在一些原来根本没信号的站里愉快的刷一刷微博和酷安了. 但是 ...

  6. C#LeetCode刷题-树

    树篇 # 题名 刷题 通过率 难度 94 二叉树的中序遍历   61.6% 中等 95 不同的二叉搜索树 II   43.4% 中等 96 不同的二叉搜索树   51.6% 中等 98 验证二叉搜索树 ...

  7. C#LeetCode刷题之#874-模拟行走机器人​​​​​​​(Walking Robot Simulation)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4038 访问. 机器人在一个无限大小的网格上行走,从点 (0, 0 ...

  8. C#LeetCode刷题之#852-山脉数组的峰顶索引(Peak Index in a Mountain Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4003 访问. 我们把符合下列属性的数组 A 称作山脉: A.le ...

  9. 漏洞重温之XSS(下)

    XSS总结 XSS的可利用方式 1.在登录后才可以访问的页面插入xss代码,诱惑用户访问,便可直接偷取用户cookie,达到窃取用户身份信息的目的. 2.修改昵称,或个人身份信息.如果别的用户在登录状 ...

  10. vue cli 中关于vue.config.js中chainWebpack的配置

    Vue CLI  的官方文档上写:调整webpack配置最简单的方式就是在vue.config.js中的configureWebpack选项提供一个对象. Vue CLI 内部的 webpack 配置 ...