mybatis开发Dao的Mapper动态代理方式
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动态代理方式的更多相关文章
- 02.MyBatis在DAO层开发使用的Mapper动态代理方式
在实际开发中,Mybatis作用于DAO层,那么Service层该如何调用Mybatis Mybatis鼓励使用Mapper动态代理的方式 Mapper接口开发方法只需要程序员编写Mapper接口(相 ...
- MyBatis开发Dao层的两种方式(Mapper动态代理方式)
MyBatis开发原始Dao层请阅读我的上一篇博客:MyBatis开发Dao层的两种方式(原始Dao层开发) 接上一篇博客继续介绍MyBatis开发Dao层的第二种方式:Mapper动态代理方式 Ma ...
- Mybatis框架基础入门(三)--Mapper动态代理方式开发
使用MyBatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper动态代理开发方法. 原始Dao开发方法需要程序员编写Dao接口和Dao实现类,此方式开发Dao,存在以下问题: Dao方 ...
- Mybatis入门——基础方式的增删该查、mapper动态代理方式的CRUD、类型转换器
一.基础方式的增删该查: 1.mybatis约定:输入参数parameterType和输出参数resulrType在形式上只能有一个. 2.如果输入/输出参数:是简单类型(8个基本类型加String) ...
- MyBatis开发Dao层的两种方式(原始Dao层开发)
本文将介绍使用框架mybatis开发原始Dao层来对一个对数据库进行增删改查的案例. Mapper动态代理开发Dao层请阅读我的下一篇博客:MyBatis开发Dao层的两种方式(Mapper动态代理方 ...
- Mybatis的dao层实现 接口代理方式实现规范+plugins-PageHelper
Mybatis的dao层实现 接口代理方式实现规范 Mapper接口实现时的相关规范: Mapper接口开发只需要程序员编写Mapper接口而不用具体实现其代码(相当于我们写的Imp实现类) Mapp ...
- Mybatis之旅第二篇-Mapper动态代理方式
一.引言 通过上一篇mybatis的入门学习,我们已经会使用mybatis实现简单的增删改查,但是我们也发现了用原始Dao开发的一些问题: Dao方法体存在重复代码:通过SqlSessionFacto ...
- mybatis由浅入深day01_5.3 Mapper动态代理方法
5.3 Mapper动态代理方法(程序员只需要写mapper接口(相当于dao接口)) 5.3.1 实现原理(mapper代理开发规范) 程序员还需要编写mapper.xml映射文件 程序员编写map ...
- Mybatis Mapper动态代理方式 typeAliases 别名的使用
目录结构及配置文件与原始dao方法相比更简便 只需一个UserMapper的接口,放在一起的配置文件,配置文件中namespace的地址确定jdk动态代理的对象 <?xml version=&q ...
随机推荐
- 20155239 2016-2017-2 《Java程序设计》第8周学习总结
教材学习内容总结 - NIO 在Java1.4之前的I/O系统中,提供的都是面向流的I/O系统,系统一次一个字节地处理数据,一个输入流产生一个字节的数据,一个输出流消费一个字节的数据,面向流的I/O速 ...
- 安装配置adb工具及遇到的问题
一. 下载安装 配置环境 二.遇到的问题 1.Terminal 不是内部或外部命令,也不是可运行程序或批处理文件 https://blog.csdn.net/wuqilianga/article/de ...
- idea 使用spring boot 搭建freemarker模板
一丶新建maven spring boot 项目 新建好了开始使用模板 先看一个目录结构 二丶配置pox.xml <?xml version="1.0" encoding ...
- C语言extern关键字使用
在chinaunix上看见一篇转载的文章,觉得特别好,关于extern使用的解释: 参考链接:http://doc.chinaunix.net/CPP/201206/2248432.shtml 在C语 ...
- win10 下ie11安装flash debuger (install flashplayer debuger on win10 64bit)
1不能安装的现象 由于win10 ie11 内置flash 微软不让用户自己手动更新ie11的flash以及安装flash debugger ,这怕是让还在用 flex 开发的大胸弟们很头疼 ...
- macOS -- Mac系统如何编辑hosts文件
Hosts是一个没有扩展名的系统文件,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联"数据库",当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从Hosts文 ...
- mysql中distinct
1.Distinct 位置 单独的distinct只能放在开头,否则报错,语法错误,与其他函数使用时候,没有位置限制如下 Select player_id,count(distinct(task_id ...
- 开始SDK之旅-入门2-集成流程图、轨迹图到系统
http://bbs.ccflow.org/showtopic-2562.aspx 经测试,基本可用,还需增加 WF/Admin/pub.ascx 首先你得先理解流程图的数据的获取方式,其他的就很容易 ...
- sublime Text 3安装sublimecodeIntel插件
下载:https://github.com/SublimeCodeIntel/SublimeCodeIntel 解压到: data/pacakges目录 安装 Package Control插件 ...
- CentOS 下tomcat安装
1. 下载tomcat, http://apache.fayea.com/tomcat/tomcat-8/v8.5.16/bin/apache-tomcat-8.5.16.tar.gz 我下载的是这个 ...