在实际开发中,Mybatis作用于DAO层,那么Service层该如何调用Mybatis

  Mybatis鼓励使用Mapper动态代理的方式

  Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体等于Dao接口实现类方法。

1.编写Mapper.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">

<!-- namespace必须和Mapper接口类路径一致 -->
<mapper namespace="cn.mybatis.mapper.UserMapper">

    <!-- 通过ID查询一个结果 -->
    <select id="findUserById" parameterType="int" resultType="cn.itcast.mybatis.po.User">
        select * from user where id = #{id}
    </select>

    <!-- 通过用户名模糊查询 -->
    <select id="findUserByUsername" parameterType="String" resultType="cn.itcast.mybatis.po.User">
        select * from user where username like "%"#{username}"%"
    </select>

    <!-- 添加用户 -->
    <insert id="insert" parameterType="cn.itcast.mybatis.po.User">
        insert into user(username,sex,birthday,address)
            values(#{username},#{sex},#{birthday},#{address})
    </insert>

</mapper>

2.编写Mapper.java接口文件

public interface UserMapper {

    /**
     * 通过ID查询一个结果
     * @param id
     * @return
     */
    public User findUserById(int id);

    /**
     * 通过用户名模糊查询
     * @param username
     * @return
     */
    public List<User> findUserByUsername(String username);

    /**
     * 添加用户
     * @param user
     */
    public void insert(User user);

}

3.Mapper接口开发必须遵循的规范

  • 1.Mapper.xml文件中的【namespace】必须与Mapper.java接口【类路径】相同
  • 2.Mapper.java接口中的【方法名】必须与Mapper.xml中对应的【id】相同
  • 3.Mapper.java接口中的【入参】必须与Mapper.xml中对应的【parameter】相同
  • 4.Mapper.java接口中的【返回类型】必须与Mapper.xml中对应的【resultType】相同

4.在SqlMapConfig.xml中加载Mapper.xml映射文件

5.测试

public class UserServiceImpl {

    private SqlSessionFactory sqlSessionFactory;

    /**
     * 初始化工厂
     *
     * @throws Exception
     */
    @Before
    public void init() throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        this.sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    /**
     * 通过ID查询一个结果
     */
    @Test
    public void m01() {
        // 获取sqlSession,和Spring整理后由Spring管理
        SqlSession sqlSession = this.sqlSessionFactory.openSession();
        // 从sqlSession中获取Mapper接口的代理对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        // 执行查询
        User user = userMapper.findUserById(10);
        System.out.println(user);

        // 和Spring整理后由Spring管理
        sqlSession.close();
    }

    /**
     * 通过用户名模糊查询
     */
    @Test
    public void m02() {
        // 获取sqlSession,和Spring整理后由Spring管理
        SqlSession sqlSession = this.sqlSessionFactory.openSession();
        // 从sqlSession中获取Mapper接口的代理对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        // 执行查询
        List<User> list = userMapper.findUserByUsername("王五");
        for (User user : list) {
            System.out.println(user);
        }

        // 和Spring整理后由Spring管理
        sqlSession.close();
    }

    /**
     * 添加用户
     */
    @Test
    public void m03() {
        // 获取sqlSession,和Spring整理后由Spring管理
        SqlSession sqlSession = this.sqlSessionFactory.openSession();
        // 从sqlSession中获取Mapper接口的代理对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        // 执行查询
        User user = new User();
        user.setUsername("添加User");
        user.setSex("男");
        user.setBirthday(new Date());
        user.setAddress("未知区域");

        userMapper.insert(user);

        // 和Spring整理后由Spring管理
        sqlSession.commit();
        sqlSession.close();

    }
}

02.MyBatis在DAO层开发使用的Mapper动态代理方式的更多相关文章

  1. MyBatis开发Dao层的两种方式(Mapper动态代理方式)

    MyBatis开发原始Dao层请阅读我的上一篇博客:MyBatis开发Dao层的两种方式(原始Dao层开发) 接上一篇博客继续介绍MyBatis开发Dao层的第二种方式:Mapper动态代理方式 Ma ...

  2. 基于Mybatis的Dao层开发

    转自:https://www.cnblogs.com/rodge-run/p/6528398.html 基于Mybatis的Dao层开发 SqlSessionFactoryBuilder用于创建 Sq ...

  3. Mybatis框架基础入门(三)--Mapper动态代理方式开发

    使用MyBatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper动态代理开发方法. 原始Dao开发方法需要程序员编写Dao接口和Dao实现类,此方式开发Dao,存在以下问题: Dao方 ...

  4. Mybatis入门——基础方式的增删该查、mapper动态代理方式的CRUD、类型转换器

    一.基础方式的增删该查: 1.mybatis约定:输入参数parameterType和输出参数resulrType在形式上只能有一个. 2.如果输入/输出参数:是简单类型(8个基本类型加String) ...

  5. mybatis开发Dao的Mapper动态代理方式

    1. 开发规范Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体跟Dao原始方法中接口实现类的方法相 ...

  6. Mybatis之旅第二篇-Mapper动态代理方式

    一.引言 通过上一篇mybatis的入门学习,我们已经会使用mybatis实现简单的增删改查,但是我们也发现了用原始Dao开发的一些问题: Dao方法体存在重复代码:通过SqlSessionFacto ...

  7. Mybatis Mapper动态代理方式 typeAliases 别名的使用

    目录结构及配置文件与原始dao方法相比更简便 只需一个UserMapper的接口,放在一起的配置文件,配置文件中namespace的地址确定jdk动态代理的对象 <?xml version=&q ...

  8. 基于Mybatis的Dao层的开发

    基于Mybatis的Dao层开发 SqlSessionFactoryBuilder用于创建SqlSessionFacoty,SqlSessionFacoty一旦创建完成就不需要SqlSessionFa ...

  9. MyBatis开发Dao层的两种方式(原始Dao层开发)

    本文将介绍使用框架mybatis开发原始Dao层来对一个对数据库进行增删改查的案例. Mapper动态代理开发Dao层请阅读我的下一篇博客:MyBatis开发Dao层的两种方式(Mapper动态代理方 ...

随机推荐

  1. NX二次开发-UFUN输入特征TAG,获取特征所有表达式TAG和个数UF_MODL_ask_exps_of_feature

    NX9+VS2012 #include <uf.h> #include <uf_modl.h> UF_initialize(); //创建块 UF_FEATURE_SIGN S ...

  2. var 更明确地表示一个变量被设置为零值

    创建一个变量并被初始化其为零值,习惯使用关键字var.这种做法是为了更明确地表示一个变量被设置为零值. 如果变量被初始化为某个非零值,就配合结构字面量和短变量操作符来创建变量. 零值 数值类型:0 字 ...

  3. [01]APUE:sysconf / pathconf

    sysconf / pathconf:用于运行时确定特定系统实际支持的限制值 sysconf 函数的参数格式: “_SC_ + 限制项名称”,如:CHILD_MAX 限制值指每个实际用户 ID 可以启 ...

  4. Springboot-WebSocket获取HttpSession问题

    换了新工作,第一个任务就是和这个有关,以前没接触过,没办法,各种度娘.谷哥,大部分都是只言片语,要么就是特定的配置环境还不贴配置--,踩坑无数, 遂整理成笔记 WebSocket协议 WebSocke ...

  5. 24-Ubuntu-文件和目录命令-查找文件内容-grep

    grep Linux系统中grep命令是一种强大的文本搜索工具. grep允许文本文件进行模式查找,所谓模式查找,又被称为正则表达式. 选项 含义 -n 显示匹配行及行号 -v 显示不包括匹配文本的所 ...

  6. [USACO11OPEN]玉米田迷宫Corn Maze

    题目描述 This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn m ...

  7. Codeforces 479【D】div3

    题目链接:http://codeforces.com/problemset/problem/977/D 题意:给你一个数字序列,定了一个游戏规则.你可以对当前数字进行两个操作 1./ 3  如果这个数 ...

  8. 应用Dubbo框架打造仿猫眼项目 理解微服务核心思想

    1:传统应用带来的问题 单一业务开发的迭代问题              扩容困难              部署回滚困难2:微服务概述 微服务是一种将业务系统进一步拆分的架构风格          ...

  9. javascript面向对象编程笔记(函数)

    第三章 函数 3.1 什么是函数 一般来说,函数声明通常由以下几部分组成: function子句 函数名称 函数所需参数 函数体 return子句.如果某个函数没有显示的返回值,默认它的返回值为und ...

  10. delphi 用户可以点击格式修改进行模板修改

    过程 TlistRepAdd.Btn_GCListRepEditClick窗口 TlistRepAdd 补打流程单 1. 给用户权限 呈现出格式修改按钮 procedure TlistRepAdd.B ...