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

  Mapper接口开发需要遵循以下规范:

  1.映射文件中namespace要等于接口的全路径

  2.通过sql语句实现数据库的操作 

  3.映射文件中sql语句id要等与于接口的方法名称
  4.映射文件中传入参数类型要等于接口方法的传入参数类型
  5.映射文件中返回结果集类型要等于接口方法的返回值类型

2.创建包结构:com.huida.mapper

3. 在com.huida.mapper包下创建UserMapper接口。

/**
* 用户管理mapper
*/
Public interface UserMapper {
//根据用户id查询用户信息
public User findUserById(int id) throws Exception;
//查询用户列表
public List<User> findUserByUsername(String username) throws Exception;
//添加用户信息
public void insertUser(User user)throws Exception;
}

3. 在com.huida.mapper下创建Mapper.xml(映射文件)

  在com.huida.mapper下创建UserMapper.xml,配置文件的名字应该与接口的名字相同。这里的头与User.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接口代理实现编写规则:
1.映射文件中namespace要等于接口的全路径
2.通过sql语句实现数据库的操作
3.映射文件中sql语句id要等与于接口的方法名称
4.映射文件中传入参数类型要等于接口方法的传入参数类型
5.映射文件中返回结果集类型要等于接口方法的返回值类型
-->
<mapper namespace="com.huida.mapper.UserMapper">
<select id="findUserById" parameterType="java.lang.Integer" resultType="com.huida.po.User">
<!-- select语句返回的是user对象,所以resultType中写User类的全路径 -->
select * from user where id=#{id}
</select> </mapper>

  需要注意的地方:

  1.映射文件中namespace要等于接口的全路径

  2.映射文件中sql语句id要等与于接口的方法名称

  3.映射文件中传入参数类型要等于接口方法的传入参数类型

  4.映射文件中返回结果集类型要等于接口方法的返回值类型

4. 加载UserMapper.xml文件

  修改SqlMapConfig.xml文件,在SqlMapConfig.xml中引入我们的UserMapper.xml文件:

<mappers>
<mapper resource="com/huida/mapper/UserMapper.xml"/>
</mappers>

5. 测试

package com.huida.test;

import java.io.IOException;
import java.io.InputStream; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test; import com.huida.mapper.UserMapper;
import com.huida.po.User; public class UserMapperTest { private SqlSessionFactory factory=null;
@Before
public void init() throws Exception{
//通过流将核心配置文件读取进来
InputStream inputStream=Resources.getResourceAsStream("config/SqlMapConfig.xml");
//通过核心配置文件输入流来创建工厂
factory=new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void testfindUserById(){
//创建SqlSession
SqlSession openSession=factory.openSession();
//通过会话的getMapper方法来实例化接口(实现类的对象)
UserMapper userMapper=openSession.getMapper(UserMapper.class);//参数放接口的字节码文件
User user=userMapper.findUserById(1);
System.out.println(user); } }

  这里面应该注意的是:我们通过会话的getMapper方法实例化接口(也就是实现类的对象)。

6.小结

(1)selectOne和selectList

  动态代理对象调用sqlSession.selectOne()和sqlSession.selectList()是根据mapper接口方法的返回值决定,如果返回list则调用selectList方法,如果返回单个对象则调用selectOne方法。

(2)namespace

  mybatis官方推荐使用mapper代理方法开发mapper接口,程序员不用编写mapper接口实现类,使用mapper代理方法时,输入参数可以使用pojo包装对象或map对象,保证dao的通用性。  

<!DOCTYPE mapper

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="cn.huida.mybatis.mapper.UserMapper">

<!-- 根据id获取用户信息 -->

<select id="findUserById" parameterType="int" resultType="cn.huida.mybatis.po.User">

select * from user where id = #{id}

</select>

<!-- 自定义条件查询用户列表 -->

<select id="findUserByUsername" parameterType="java.lang.String"

resultType="cn.huida.mybatis.po.User">

select * from user where username like '%${value}%'

</select>

<!-- 添加用户 -->

<insert id="insertUser" parameterType="cn.huida.mybatis.po.User">

<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">

select LAST_INSERT_ID()

</selectKey>

insert into user(username,birthday,sex,address)

values(#{username},#{birthday},#{sex},#{address})

</insert>

</mapper>

mybatis开发Dao的Mapper动态代理方式的更多相关文章

  1. 02.MyBatis在DAO层开发使用的Mapper动态代理方式

    在实际开发中,Mybatis作用于DAO层,那么Service层该如何调用Mybatis Mybatis鼓励使用Mapper动态代理的方式 Mapper接口开发方法只需要程序员编写Mapper接口(相 ...

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

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

  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层的两种方式(原始Dao层开发)

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

  6. Mybatis的dao层实现 接口代理方式实现规范+plugins-PageHelper

    Mybatis的dao层实现 接口代理方式实现规范 Mapper接口实现时的相关规范: Mapper接口开发只需要程序员编写Mapper接口而不用具体实现其代码(相当于我们写的Imp实现类) Mapp ...

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

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

  8. mybatis由浅入深day01_5.3 Mapper动态代理方法

    5.3 Mapper动态代理方法(程序员只需要写mapper接口(相当于dao接口)) 5.3.1 实现原理(mapper代理开发规范) 程序员还需要编写mapper.xml映射文件 程序员编写map ...

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

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

随机推荐

  1. PyAlgoTrade Hello World 第一个程序(一)

    本教程的目标是快速介绍PyAlgoTrade.PyAlgoTrade的目标是帮助您实现股票交易策略.假设您有一个交易策略的想法,并且您希望使用历史数据进行评估,并查看其行为方式,那么PyAlgoTra ...

  2. jenkins配置maven

    # 去官网下载maven # 安装 cd /opt wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.5.3/binari ...

  3. LG4717 【模板】快速沃尔什变换

    题意 题目描述 给定长度为\(2^n\)两个序列\(A,B\),设\(C_i=\sum_{j\oplus k}A_jB_k\)分别当\(\oplus\)是or,and,xor时求出C 输入输出格式 输 ...

  4. SSH学习之四 OpenSSH安全

             OpenSSH是Linux/Unix下一款加密通讯软件.同一时候也是我们用来远程控制Linux/Unixserver重要的必装软件. 对于各版本号的Linux及Unix发行版而言,O ...

  5. ZedGraph右键菜单怎样禁止它弹出(转)

    private void ZGC_ContextMenuBuilder( ZedGraphControl sender,                     ContextMenuStrip me ...

  6. bzoj2957楼房重建

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2957 线段树.每个点记录斜率,要一个单增的序列长度(从1开始). 线段树每个点记录自己区间的 ...

  7. C BIN加密

    #include <stdio.h> #include <string.h> #include <stdlib.h> #ifndef DWORD #define D ...

  8. emacs之配置symbol浏览界面

    由于ecb的method-buffer不能更新,因此抛弃ecb,speedbar的method也不能更新,换imenu-tree 使用el-get install安装imenu-tree imenu- ...

  9. [java]经验集

    Calendar c = Calendar.getInstance(); c.set(1999,12,21); SimpleDateFormat sdf = new SimpleDateFormat( ...

  10. pythonNetday06

    进程 Process(target,name,args,kwargs) p.pid : 创建的新的进程的PID号 p.is_alive() 判断进程是否处于alive状态 p.daemon = Tru ...