想要了解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. How to write date range query in Nest ElasticSearch client?

    Looking at the source code, there are two overloads of the OnField method. When I use the the that t ...

  2. 解决Python向MySQL数据库插入中文数据时出现乱码

    解决Python向MySQL数据库插入中文数据时出现乱码 先在MySQL命令行中输入如下语句查看结果: 只要character_set_client character_set_database ch ...

  3. P2278 操作系统

    P2278 操作系统 题目描述 写一个程序来模拟操作系统的进程调度.假设该系统只有一个CPU,每一个进程的到达时间,执行时间和运行优先级都是已知的.其中运行优先级用自然数表示,数字越大,则优先级越高. ...

  4. canvas+js绘制折线图

    效果: 源码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  5. [Xamarin.iOS] 如何引用Objective-c寫的Class Library (转帖)

    這個範例是如何在Xamarin.ios中去使用一個我們自行在Xcode中開發的Objective-c Class Library. 主要會執行的步驟如下 1. 在Xcode 裡面去建立一個Class ...

  6. javac文件系统

    1.文件 Java编译器在编译的过程中会涉及到对各种文件的搜索和查找,例如在文件夹下搜索.java源在压缩包*.jar内搜索.class文件,同时也会将编译生成的二进制文件写入文件.Java编译器有自 ...

  7. WPF中C#代码触发鼠标点击事件

    1.如下代码; <Button x:Name="btnTest" Click="btnTest_Click"> <Button.Trigger ...

  8. Gradle中的SourceSet理解

    对于maven项目来说,目录结构是固定的,也就是像这样: src/main/ src/main/java/ src/main/resources/ src/test/ src/test/java/ s ...

  9. weblogic.rjvm.PeerGoneException

    并发weblogic异常,报错如下: weblogic.rjvm.PeerGoneException: ; nested exception is: weblogic.utils.net.Socket ...

  10. Angular2 不明真相第一个Demo例子

    如果不是去年换工作接触到AngularJS,估计是不会花时间去学习这个框架的,毕竟是前端的框架,不是自己熟悉的领域.但是为了混得下去,去年就学习了AngularJS的一些用法,当时还整理了一些积累 & ...