MyBatis Mapper 文件例子
<?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.lee.UserMapper">
<!--返回单一对象-->
<select id="selectName" resultType="com.lee.User">
<![CDATA[
select user_name userName from user_table where user_id = #{userId}
]]>
</select>
<!--返回结果格式 Map<字段名称,字段值>-->
<select id="selectByName" resultType="hashMap" parameterType="string">
<![CDATA[
SELECT * from user_table where user_name=#{userName}
]]>
</select>
<!--调用存储过程-->
<select id="selectUserFromStoreProcedure" statementType="CALLABLE">
<![CDATA[
{call my_pack.my_proc(
#{userId,mode=IN,jdbcType=VARCHAR,javaType=string},
#{userName,mode=OUT,jdbcType=FLOAT,javaType=string})}
]]>
</select>
<!--返回List<User>-->
<select id="selectUsers" resultType="com.lee.User">
<![CDATA[
select user_id userId, user_name userName from user_table
]]>
</select>
<!--重用sql-->
<sql id="subQuery">
<![CDATA[
WITH MY_SUB_QUERY as (
select 'lee' as user_name, '1' as user_id from dual
union select 'lee1' ,'2' from dual
)
]]>
</sql>
<!--动态sql-->
<sql id="selectOther">
<include refid="subQuery" />
<![CDATA[
SELECT t.other_id otherId, t.other_name otherName, t.other_flag otherFlag FROM OtherTable t
INNER JOIN MY_SUB_QUERY mapper ON mapper.user_id = t.user_id
]]>
<if test="filterFlag==true">
<![CDATA[
and t.other_flag = 'Y'
]]>
</if>
<!--
另一个if段
<if test="flag1==true">
...
</if>
-->
<!--
使用choose语句,flag1是从mapper方法中传递的参数,如 @Param("flag1") String flag1
<choose>
<when test="flag1 == 'Y'">
t1.flag1 AS flag
FROM table1 t1
</when>
<otherwise>
t2.flag2 AS flag
FROM table2 t2
</otherwise>
</choose>
-->
</sql>
<!--返回数字-->
<select id="selectCount" resultType="java.lang.Long">
<![CDATA[
SELECT count(*) FROM user_table
]]>
</select>
<!--Map参数, 格式Map<参数名称,参数值>-->
<select id="selectUser" parameterType="java.util.HashMap" resultType="com.lee.User">
<![CDATA[
SELECT user_id userId, user_name userName from user_table
where user_id=#{userId} and user_name = #{userName}
]]>
</select>
</mapper>
--------------------------------------------------这个分割线的作用是要显示下边的Java对象例子--------------------------------------------------
public class User {
private int userId;
private String userName;
public String getUserId() {return userId}
public void setUserId(int userId) {this.userId=userId}
public String getUserName() {return userName}
public void setUserName(String userName) {this.userName=userName}
}
--------------------------------------------------这个分割线的作用是要显示下边的Mapper对象例子--------------------------------------------------
@Repository
public interface UserMapper {
public User selectName(@Param("userId") String userId);
public List<Map<String,Object>> selectByName(@Param("userName")String userName);
<!--Map<字段名称,字段值>-->
public void selectUserFromStoreProcedure(Map<String,Object> map);
public List<User> selectUsers();
public OtherUser selectOther();
public int selectCount();
public User selectUser(Map<String,Object> map);
}
--------------------------------------------------这个分割线的作用是要显示另一些配置例子--------------------------------------------------
<!--用映射配置查询sql-->
<resultMap id="UserMap" type="com.lee.User">
<result column="user_id" property="userId"/>
<result column="user_name" property="userName"/>
</resultMap>
<select id="selectName" resultMap="UserMap">
<![CDATA[
select user_name from user_table where user_id = #{userId}
]]>
</select>
<!--重用映射配置并连接到其它结果集查询-->
<resultMap id="OtherUserMap" type="com.lee.OtherUser" extends="UserMap">
<!--多个查询条件用逗号隔开,如userId=user_id,userName=user_name-->
<collection property="ownedItems" select="selectItems" column="userId=user_id"/>
</resultMap>
<select id="selectItems" resultType="com.lee.UserItem">
SELECT * FROM user_item_table WHERE user_id = #{userId}
</select>
public class OtherUser extends User {
private List<UserItem> ownedItems;
public List<UserItem> getOwnedItems() {return ownedItems}
public void setOwnedItems(List<UserItem> userId) {this.ownedItems=ownedItems}
}
--------------------------------------------------这个分割线的作用是要显示另一个重用子查询配置例子--------------------------------------------------
<mapper namespace="mapper.namespace">
<sql id="selectTable1">
<![CDATA[
select f1, f2, f3 from table1 where 1=1
]]>
</sql>
<select id="getStandardAgents" resultMap="StandardAgent">
<include refid="mapper.namespace.selectTable1"/>
<![CDATA[
and f1 = 'abc'
]]>
</select>
</mapper>
--------------------------------------------------这个分割线的作用是要显示insert/update/delete配置例子--------------------------------------------------
<!--从Oracle序列中产生user_id, jdbcType=VARCHAR用于插入空值-->
<insert id="insertUser" parameterType="com.lee.User">
<selectKey keyProperty="user_id" resultType="string" order="BEFORE">
select db_seq.nextval as user_id from dual
</selectKey>
INSERT INTO
user_table(
user_id,
user_name,
) VALUES(
#{user_id},
#{user_name,jdbcType=VARCHAR}
)
</insert>
<update id="updateUser" parameterType="com.lee.User">
UPDATE user_table
SET user_name = #{userName,jdbcType=VARCHAR},
WHERE
user_id = #{userId}
</update>
<delete id="deleteUser" parameterType="com.lee.User">
DELETE user_table WHERE user_id = #{userId}
</delete>
MyBatis Mapper 文件例子相关推荐
- MyBatis mapper文件中使用常量
MyBatis mapper文件中使用常量 Java 开发中会经常写一些静态常量和静态方法,但是我们在写sql语句的时候会经常用到判断是否等于 //静态类 public class CommonCod ...
- idea查看项目pid_intellij idea 插件开发--快速定位到mybatis mapper文件中的sql
intellij idea 提供了openApi,通过openApi我们可以自己开发插件,提高工作效率.这边直接贴个链接,可以搭个入门的demo:http://www.jianshu.com/p/24 ...
- mybatis mapper文件找不到_MyBatis 面试题
1. 什么是 Mybatis? MyBatis 是一个支持自定义 SQL.存储过程以及高级映射的持久层框架. MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作. Myba ...
- 【Mybatis】Mapper文件中sql不等于的写法
[Mybatis]Mapper文件中sql不等于的写法 认识误区:在xml文件中,是 不能直接使用 < .>.& 的.因为会被语法检查而报错. 正确使用的两种方法 1. 第一种:转 ...
- mybatis mapper xml文件的导入方式和查询方式
mybatis mapper xml文件的导入方式和查询方式 ssm框架 Mybatis mapper与SQLSession的关系 每个基于MyBatis的应用都是以一个SqlSessionFact ...
- 自动生成Mapper文件(基于Mybatis Maven插件)
自动生成Mybatis的Mapper文件 工作中使用mybatis时我们需要根据数据表字段创建pojo类.mapper文件以及dao类,并且需要配置它们之间的依赖关系,这样的工作很琐碎和重复,myba ...
- 使用maven工程实现Mybatis自动生成Mapper文件
本文档为学习记录,参考博文: https://www.cnblogs.com/handsomeye/p/6268513.html https://www.cnblogs.com/maanshancss ...
- mybatis中getMapper是怎么通过动态代理得到dao接口的实现类并执行mapper文件sql语句的
提示1:本文需在掌握动态代理基础后浏览,如果动态代理需要回顾可以看我的另一篇博客 提示2:本文以我之前写的工程为模板进行讲解,工程结构及代码可以看我的另一篇博客 1. 说在前头 2. sqlSessi ...
- Mybatis工作流程,附带mybatis的mapper文件和config配置文件模板。mapper文件和dao接口的关系——xml中的namespace和sql标签id命名要求。
1. Mybatis工作流程 1.1 使用MySQL创建数据库girls并生成一个表boys,如下图. 1.2 创建该表对应的简单实体类Boys,如下图. 1.3 创建Dao接口以及和接口同名的map ...
最新文章
- JS String类型整理
- Proguard混淆代码(1)
- 当我们在谈论内存时,我们在谈论什么
- SAP Commerce Cloud 的 build 过程
- SVM与感知机的异同点
- python中形参可以使用中文定义嘛_python中函数的参数分类
- windows安装安卓开发环境Eclipse+SDK+ADT
- java核心教程_核心Java教程
- python函数求导_python_exp
- MSP430 F5529 单片机 OLED 音乐播放器 八音盒 蜂鸣器 音乐
- 微信内置浏览器不支持下载文件的解决方案
- 测试电脑硬盘损坏的软件,硬盘检测工具哪个好 如何检测硬盘是否损坏【详细介绍】...
- Python精讲:Python中集合的概念和创建方法详解
- 分类(category)的使用
- 360html文件打不开,为什么360安全卫士打不开
- 10 05 05 繁杂
- Java实现读取csv文件
- 血淋淋的BUG:波音在软件开发上错在哪里?
- Python交流QQ群
- OWC11绘制双轴图表
热门文章
- python操作excel-python操作excel
- python学起来难吗-python学起来难吗
- python循环语句-python中的for循环语句怎么写
- python 加注拼音-Python 获取中文字拼音首个字母的方法
- python是c语言吗-初学者python和c语言先学哪个好呢?
- python类装饰器详解-Python装饰器详解
- python就业前景分析-最新的Python就业前景分析一览表
- python3读取excel数据-python3读取Excel表格数据的代码分享
- Pycharm的项目文件名是红色的原因及解决办法
- LeetCode Convert Sorted List to Binary Search Tree(有序单链表转为平衡二叉树)