想要了解MyBatis基础的朋友可以通过传送门:

  MyBatis学习(一)---配置文件,Mapper接口和动态SQL  http://www.cnblogs.com/ghq120/p/8322302.html

  MyBatis学习(二)---数据表之间关联  http://www.cnblogs.com/ghq120/p/8323918.html

  之前两篇文章都是单独介绍了MyBatis的用法,并没有和任何框架进行整合。使用MyBatis完成数据库的操作仍有一些模板化的代码,比如关闭SqlSession、提交事务等。

  MyBatis和Spring整合只有dao组件的接口没有实现类,避免了显式调用SqlSession的相关操作数据库的方法,事务的提交和SqlSession的关闭。

  实现MyBatis和Spring整合且只有接口没有实现类的要求是:dao组件接口的主文件名和映射文件的主文件名同名,且在同包下,映射文件的命名空间必须是dao组件接口的全限定名,标志相应的sql语句的id必须是接口的方法名。

MyBatis+Spring

此项目实现的还是用户的增删改查

使用Spring后,就不需要使用工具类来获取SqlSessionFactory和SqlSession的对象。通过在Spring容器中配置bean元素来自动获取。

Spring容器的配置文件如下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.xsd"> <!-- 配置数据源,指出连接数据库需要的驱动、url、用户名和密码以及连接池相关信息 -->
<bean id="dbcpdataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"></property>
<property name="username" value="scott"></property>
<property name="password" value="itcast"></property>
<property name="initialSize" value="20"></property>
<property name="maxActive" value="10"></property>
<property name="maxIdle" value="3"></property>
<property name="minIdle" value="2"></property>
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="mybatis-config.xml"></property>
<property name="dataSource" ref="dbcpdataSource"></property>
</bean> <!-- 配置Dao组件接口的代理类,该bean元素只能配置一个Dao组件的代理类,如果要配置多个,该bean元素要出现多次,根据id来区分 -->
<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.ghq.model.dao.UserDao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>

由于Spring容器中已经和数据库建立连接,则MyBatis配置文件中无需再次建立和数据库的连接,mybatis-config.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.ghq.model.entity"/>
</typeAliases> <!-- 注册映射文件 -->
<mappers>
<package name="com.ghq.model.dao"/>
</mappers>
</configuration>

dao组件中的方法为

public interface UserDao {
//根据模糊姓名和性别查询
List<User> getUserBynameAndGender(Map<String,Object> m);
//批量删除用户
public boolean delBatchUser(UserVo vo);
}

dao组件的配置文件UserDao.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.ghq.model.dao.UserDao">
<select id="getUserBynameAndGender" parameterType="java.util.Map" resultType="user">
select * from user_tab
<!-- where 标签默认会去除第一个and,若输入参数是null,则删除where条件 -->
<where>
<if test="username != null and username != ''">
and username like '%${username}%'
</if>
<if test="gender !=null and gender !=''">
and gender = #{gender}
</if>
</where>
</select> <delete id="delBatchUser" parameterType="UserVo">
delete from user_tab
<where>
<if test="idlist !=null and idlist.size() > 0">
and id in
<foreach collection="idlist" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</if>
</where>
</delete> </mapper>

单元测试

使用Spring容器来创建对象,则需要先加载Spring容器,创建dao组件接口的对象,调用对象中的方法。

public class testmybatis {
//根据条件来获取用户
@Test
public void testgetUserbynameAndGender(){
SqlSession session = MybatisDb.getSession();
UserDao userdao = session.getMapper(UserDao.class);
Map<String,Object> user = new HashMap<String,Object>();
user.put("username", "张");
user.put("gender", "女");
List<User> ulist = userdao.getUserBynameAndGender(user);
if (ulist !=null && ulist.size() > 0) {
for (User uu : ulist) {
System.out.println(uu);
}
} else {
System.out.println("没有符合条件的用户");
}
} //批量删除用户的方法
@Test
public void testdelBatchUser(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao)context.getBean("userDao");
UserVo vo = new UserVo();
List<Integer> idlist = new ArrayList<Integer>();
idlist.add(8);
idlist.add(6);
vo.setIdlist(idlist);
boolean flag = userDao.delBatchUser(vo);
if (flag) {
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
}

MyBatis学习(三)---MyBatis和Spring整合的更多相关文章

  1. Mybatis学习(六)————— Spring整合mybatis

    一.Spring整合mybatis思路 非常简单,这里先回顾一下mybatis最基础的根基, mybatis,有两个配置文件 全局配置文件SqlMapConfig.xml(配置数据源,全局变量,加载映 ...

  2. MyBatis学习(二):与Spring整合(非注解方式配置MyBatis)

    搭建SpringMVC的-->传送门<-- 一.环境搭建: 目录结构: 引用的JAR包: 如果是Maven搭建的话,pom.xml的配置如下: <?xml version=" ...

  3. MyBatis学习总结-MyBatis快速入门的系列教程

    MyBatis学习总结-MyBatis快速入门的系列教程 [MyBatis]MyBatis 使用教程 [MyBatis]MyBatis XML配置 [MyBatis]MyBatis XML映射文件 [ ...

  4. Mybatis学习(7)spring和mybatis整合

    整合思路: 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession.(spr ...

  5. Java开发学习(三十九)----SpringBoot整合mybatis

    一.回顾Spring整合Mybatis Spring 整合 Mybatis 需要定义很多配置类 SpringConfig 配置类 导入 JdbcConfig 配置类 导入 MybatisConfig ...

  6. Mybatis 学习笔记1 不整合Spring的方式使用mybatis

    两种方式都包含了: package com.test.mybatis; import java.util.List; import org.apache.ibatis.io.Resources; im ...

  7. MyBatis学习(三)

    前言 感觉学习进度还是比较慢啊,一整天的学习效率不是很高,一会看电视,一会喝茶,对自己的要求不严格...今天就说说关联表数据的插入以及别名的使用. 正文 1.关联插入 之前,我在数据库中已经创建了一张 ...

  8. Mybatis插件扩展以及与Spring整合原理

    @ 目录 前言 正文 插件扩展 1. Interceptor核心实现原理 2. Mybatis的拦截增强 Mybatis与Spring整合原理 1. SqlSessionFactory的创建 2. 扫 ...

  9. Spring框架学习(4)spring整合hibernate

    内容源自:spring整合hibernate    spring整合注解形式的hibernate 这里和上一部分学习一样用了模板模式, 将hibernate开发流程封装在ORM层提供的模板类Hiber ...

随机推荐

  1. Linux命令行测试网速speedtest.net

    Linux命令行测试网速speedtest.net 当发现上网速度变慢时,人们通常会先首先测试自己的电脑到网络服务提供商(通常被称为"最后一公里")的网络连接速度.在可用于测试宽带 ...

  2. 总结day2 ---- while循环的简单使用, 格式化输出.运算符.以及编码的应用

    内容提要 一 : while 循环 while 的基本语句操作 如何终止循环 二 :格式化输出 三 :运算符号 四 :编码初识别 一 : while 循环 1  >>>>whi ...

  3. leetcode-49-字母异位词分组(神奇的哈希)

    题目描述: 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. 示例: 输入: ["eat", "tea", "t ...

  4. 47.ActiveMQ集群

    (声明:本文非EamonSec原创) 使用ZooKeeper实现的Master-Slave实现方式,是对ActiveMQ进行高可用的一种有效的解决方案,高可用的原理:使用ZooKeeper(集群)注册 ...

  5. 对Routers的理解

    路由Routers 对于视图集ViewSet,我们除了可以自己手动指明请求方式与动作action之间的对应关系外,还可以使用Routers来帮助我们快速实现路由信息. REST framework提供 ...

  6. Numpy:np.vstack()&np.hstack() flat/flatten

    一 .  np.vstack: 按垂直方向(行顺序)堆叠数组构成一个新的数组 In[3]: import numpy as np In[4]: a = np.array([[1,2,3]]) a.sh ...

  7. Mac下配置idea(Mac 10.12)

    idea应该是第二个最好用的开发工具,除了宇宙最强大的VS第一外,过来就是它,其体系中已经发布很多语言的开发工具. idea:http://bbs.feng.com/read-htm-tid-1050 ...

  8. (转)OpenStack之服务端口号

    原文:https://blog.csdn.net/henulwj/article/details/47276391 在部署openstack的过程中,你会遇到配置各种服务的endpoint,opens ...

  9. vSphere虚拟主机安装Centos7系统

    经过上一帖的主机设置,这一步就可以开始安装系统了,本次详细记录各个过程并分析结果. Centos7 1.右键点击列表中的虚拟主机,打开控制台. 点击绿色开机键,开始安装. 这里有一个很关键的点,就是上 ...

  10. Tomcat服务器安装配置

    1.到http://tomcat.apache.org/官网下载Tomcat的zip版本,这样直接解压就行了,不用安装.我下载是Tomcat6.0版本的zip文件,解压在D:\Java\apache- ...