Spring_mybatis结合之1.1
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代理开发之原始版
- 先创建工程,把所有的包都导入进来。
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的更多相关文章
- spring_mybatis :整合
第一步:导入相关架包(使用maven构建项目) 在pom.xml文件中导入相关依赖 1.Junit测试架包 <dependency> <groupId>junit</gr ...
- SpringMVC与mybatis整合
一.逆向工程生成基础信息 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generator ...
- Maven创建多个子项目
一.下载jdk并安装:下载apache-maven包,解压到指定目录.(例:D:\Java\apache-maven-3.3.9) 二.配置环境. 1.配置jdk环境 系统变量 (1)JAVA_HOM ...
- Spring与Mybatis整合
一 概述 1.整合的目的 将Mapper映射器的创建任务交给Spring容器. 二 具体实现 1.创建sqlSessionFactory: <bean id="sqlSessionFa ...
- 框架整合——Spring与MyBatis框架整合
Spring整合MyBatis 1. 整合 Spring [整合目标:在spring的配置文件中配置SqlSessionFactory以及让mybatis用上spring的声明式事务] 1). 加入 ...
- spring整合mybatis,批量扫描mapper接口出现异常
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component c ...
- Spring+Mybatis整合时 Failed to read candidate component class,Caused by:IllegalArgumentException
Spring+Mybatis整合时Caused by: java.lang.IllegalArgumentException错误 org.springframework.beans.factory.B ...
- Maven项目中Spring整合Mybatis
Maven项目中Spring整合Mybatis 添加jar包依赖 spring需要的jar包依赖 <dependency> <groupId>org.springframewo ...
- SSM项目之电商项目easymall(一)
一 环境准备 软件环境: 1 jdk1.8 JAVA_HOME:是给软件用的,各种启动的软件都会寻找JAVA_HOME的环境变量: Path:给windows用的: ...
随机推荐
- OKHttp 官方文档【一】
最近工作比较忙,文章更新出现了延时.虽说写技术博客最初主要是写给自己,但随着文章越写越多,现在更多的是写给关注我技术文章的小伙伴们.最近一段时间没有更新文章,虽有工作生活孩子占用了大部分时间的原因,但 ...
- bzoj 4448 [Scoi2015]情报传递 主席树
比较套路的题目. 可以发现难点在于某个点的权值动态修改 且我们要维护树上一条路径上的点权>x的个数. 每个点都在动态修改 这意味着我们的只能暴力的去查每个点. 考虑将所有可以动态修改的点变成静态 ...
- x86架构:从实模式进入保护模式
详细的过程说明参考:(1) https://www.cnblogs.com/Philip-Tell-Truth/p/5211248.html (2)x86汇编:从实模式到保护模式 这里简化一下 ...
- Linux的VMWare中Centos7的安装
Windows平台下VMWare 14安装Centos 7 一.虚拟机硬件配置 1.选择创建新的虚拟机: 2.选择自定义(高级)进行自定义配置,单击下一步: 3.选择虚拟机硬件兼容性为默认,单击下一步 ...
- 利用mvc模式,实现用户的注册
实现功能:利用mvc模式,实现用户的登陆注册功能 1.程序的框架结构 2个包,bean,以及servlet 3个jsp页面,注册页面,注册成功页面,注册失败页面 mysql驱动 2.编程思想 通过js ...
- RabbitMQ好文章推荐
文章来源:掘金 作者:申城异乡人 RabbitMQ使用教程(一)RabbitMQ环境安装配置及Hello World示例 https://juejin.im/post/5ce644b9f ...
- 利用描述符自定制property
利用描述符自定制property class Lazyproperty: def __init__(self,func): # print('==========>',func) self.fu ...
- Android html5和Android之间的交互
今天补充了会昨天的问题,然后搞半天又出现莫名其妙的问题. 今天讲的是交互,先说html5在Android的调用. 上面的hello world上面的部分都是安卓里的布局 然后按这些布局自动生成代码. ...
- C语言输出颜色
命令后界面输出颜色 嵌入式终端界面输出日志时,为了区分输出的有用信息.错误信息,可以给不同级别的输出加上不同的颜色,以方便查看. 下面是颜色的定义: //颜色宏定义 #define NONE &quo ...
- 一文搞懂Linux系统开发
先列一下Linux系统开发要掌握的知识,以后有时间再一一介绍. 欢迎关注我的微信公众号:fensnote 文章目录 Linux系统开发会用到什么? C语言基础 shell脚本 学会使用Makefile ...