mybatis基本查询
<?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="cn.xdf.wlyy.jxdtest.dao.RoleDao">
<select id="findById" parameterType="int"
resultType="cn.xdf.wlyy.jxdtest.po.Role">
SELECT * from role where id=#{value}
</select>
<select id="findByName" parameterType="String"
resultType="cn.xdf.wlyy.jxdtest.po.Role">
<!-- 模糊查询的两种方法 -->
<!-- SELECT * from role where name like '%${value}%'; -->
SELECT * from role where name like concat(concat('%',#{value}),'%');
</select>
<delete id="deleteById" parameterType="int">
DELETE FROM role WHERE
id=#{value}
</delete>
<insert id="insertRole" parameterType="cn.xdf.wlyy.jxdtest.po.Role">
INSERT into
role(name,u_id) VALUES(#{name},#{u_id})
</insert>
<update id="updateRole" parameterType="cn.xdf.wlyy.jxdtest.po.Role">
UPDATE role set
name=#{name},u_id=#{u_id} where id =#{id}
</update>
<!--查询所有 resultMap返回结果 -->
<resultMap type="cn.xdf.wlyy.jxdtest.po.Role" id="listRoleMap">
<id property="id" column="id" />
<id property="name" column="name" />
<id property="u_id" column="u_id" />
</resultMap>
<select id="listRole" resultMap="listRoleMap">
select r.id,r.name,r.u_id from
role as r;
</select>
<!-- 查询所有 resultType返回结果 -->
<select id="listRoletype" resultType="cn.xdf.wlyy.jxdtest.po.Role">
select r.id,r.name,r.u_id
from role as r;
</select>
<!-- 多表联查 -->
<!-- 关联所需要的列 笛卡尔积 -->
<resultMap type="cn.xdf.wlyy.jxdtest.po.User" id="getUserMap">
<id property="id" column="u_id" />
<result property="name" column="u_name" />
</resultMap>
<resultMap type="cn.xdf.wlyy.jxdtest.po.Role" id="userRoleMap">
<id property="id" column="r_id" />
<result property="name" column="r_name" />
<association property="user" javaType="cn.xdf.wlyy.jxdtest.po.User"
resultMap="getUserMap" />
</resultMap>
<select id="getUserRole" resultMap="userRoleMap">
SELECT r.id r_id,u.id u_id,
r.name r_name,u.name u_name FROM role as r,user as u where r.u_id=u.id
</select>
<!-- 根据参数联查 -->
<resultMap type="cn.xdf.wlyy.jxdtest.po.Role" id="userRoleMapById">
<id property="id" column="r_id" />
<result property="name" column="r_name" />
<!-- 一对一内嵌如查询 colum指向要映射的一方 -->
<!-- <association property="user" column="u_id" select="getUser"></association> -->
<!-- 映射实体一一对应 列名需通过别名映射 负责为空 -->
<association property="user" javaType="cn.xdf.wlyy.jxdtest.po.User" >
<id property="id" column="u_id" />
<result property="name" column="u_name" />
</association>
<!-- 一对多实现的两种方式 -->
<!-- ofType指定集合中的对象类型 -->
<!-- 不嵌套映射 -->
<!-- <collection property="users" ofType="cn.xdf.wlyy.jxdtest.po.User">
映射中的别名必须写别名 <id property="id" column="u_id"/> <result property="name" column="u_name"/>
</collection> -->
<!--一对多嵌套方式column指向一的一方(唯一键) -->
<collection property="users" ofType="cn.xdf.wlyy.jxdtest.po.User"
column="r_id" select="getUsers"></collection>
<!--一对多实现的两种方式 -->
</resultMap>
<select id="getUserRoleByUid" parameterType="int" resultMap="userRoleMapById">
<!-- 多对一查询 -->
<!-- select r.id r_id ,r.name r_name ,u.id u_id,u.name u_name from role
as r ,user as u where r.u_id=u.id and r.u_id=#{value} -->
<!-- 一对多查询 -->
select r.id r_id ,r.name r_name ,u.id u_id,u.name u_name from role as
r ,user as u where r.id=u.r_id and r.id=#{value}
</select>
<!-- 在做单表查询时候 字段为数据表中原有字段 -多对一 -->
<select id="getUser" parameterType="int" resultType="cn.xdf.wlyy.jxdtest.po.User">
select id
,name from user where id=#{value}
</select>
<!-- 一对多查询 -->
<select id="getUsers" parameterType="int"
resultType="cn.xdf.wlyy.jxdtest.po.User">
select id ,name from user where r_id=#{value}
</select>
</mapper>
mybatis基本查询的更多相关文章
- Mybatis关联查询和数据库不一致问题分析与解决
Mybatis关联查询和数据库不一致问题分析与解决 本文的前提是,确定sql语句没有问题,确定在数据库中使用sql和项目中结果不一致. 在使用SpringMVC+Mybatis做多表关联时候,发现也不 ...
- myBatis批量查询操作,xml中使用foreach案例
使用场景:有一个订单表,实体类为OrderBase.java,订单有个状态为status值可能为"1,2,3,4,5,6",现在需要查询状态为"2,3,4"的订 ...
- MyBatis关联查询 (association) 时遇到的某些问题/mybatis映射
先说下问题产生的背景: 最近在做一个用到MyBatis的项目,其中有个业务涉及到关联查询,我是将两个查询分开来写的,即嵌套查询,个人感觉这样更方便重用: 关联的查询使用到了动态sql,在执行查询时就出 ...
- MyBatis基础:MyBatis关联查询(4)
1. MyBatis关联查询简介 MyBatis中级联分为3中:association.collection及discriminator. ◊ association:一对一关联 ◊ collecti ...
- MyBatis关联查询,一对多关联查询
实体关系图,一个国家对应多个城市 一对多关联查询可用三种方式实现: 单步查询,利用collection标签为级联属性赋值: 分步查询: 利用association标签进行分步查询: 利用collect ...
- Ibatis/Mybatis模糊查询
Ibatis/Mybatis模糊查询 根据网络内容整理 Ibatis中 使用$代替#.此种方法就是去掉了类型检查,使用字符串连接,不过可能会有sql注入风险. Sql代码 select * from ...
- MyBatis一对一查询
---------------------siwuxie095 MyBatis 一对一查询 以订单和用户为例,即 相对订 ...
- MyBatis高级查询
-------------------------siwuxie095 MyBatis 高级查询 1.MyBatis 作为一个 ORM 框架,也对 SQL 的高级查询做了支持, MyBatis 高级查 ...
- 在MyBatis中查询数据、涉及多参数的数据访问操作、插入数据时获取数据自增长的id、关联表查询操作、动态SQL、关于配置MyBatis映射没有代码提示的解决方案
1. 单元测试 在单元测试中,每个测试方法都需要执行相同的前置代码和后置代码,则可以自定义2个方法,分别在这2个方法中执行前置代码和后置代码,并为这2个方法添加@Before和@After注解,然后, ...
- 在数据库中添加数据以后,使用Mybatis进行查询结果为空
在数据库中添加数据以后,使用Mybatis进行查询结果为空,这是因为数据库中添加数据忘记commit的缘故.
随机推荐
- drf 的分页功能
1 settings中配置 page_size = 20 代表每页20条数据 REST_FRAMEWORK = { 'DEFAULT_PARSER_CLASSES': ( 'rest_framewor ...
- nginx之安装
1.简介 Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.由俄罗斯的程序设计师Igor Sysoev所开发,供俄 ...
- Oulipo (poj3461
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29759 Accepted: 11986 Descript ...
- [CSP-S模拟测试94]题解
A.凉宫春日的忧郁 高精硬上似乎跑不过,其实可以都取个$log$.那么只需要比较$y\times log ^x$和$\sum \limits _{i=1}^y log^i$就好了. #include& ...
- drawRect
1) 画笔设置 Paint.Style.STROKE 中空模式 paint = new Paint(); //新建一个画笔对象 paint.setAntiAlias(true);//抗锯齿功能 pai ...
- PADS 学习资料
PADS软件: PADS9.5_3in1.7z 链接: http://pan.baidu.com/s/1epO4Y 密码: zltl 打不开程序的博友,请看你的电脑有没有安装常用软件运行库 ...
- JS-Number 的精度
JS 使用 IEEE 754 的双精度数表示数字,1 位符号,10 位指数,53 位底数. 所以 JS 数字精度近似为 15.95 位 10 进制(10 ** 15.95). 也就是说整部加小数部分超 ...
- JSONP的产生,和ajax的异同!
先说说JSONP是怎么产生的: 其实网上关于JSONP的讲解有很多,但却千篇一律,而且云里雾里,对于很多刚接触的人来讲理解起来有些困难,着用自己的方式来阐释一下这个问题,看看是否有帮助. 1.一个众所 ...
- nginxUbuntu安装Nginx和正确卸载Nginx Nginx相关 与Nginx报错:nginx: [error] invalid PID number "" in "/run/nginx.pid" 解决方法
https://www.cnblogs.com/zhaoyingjie/p/6840616.html https://blog.csdn.net/adley_app/article/details/7 ...
- 用C#调用C++DLL提示找不到DLL解决方法【转】
用C#调用自己写的C++ DLL(x64),总是提示找不到DLL,调试可以,发布release老是提示找不到DLL(dll文件确定存在) 原因:Visual C++的DLL分发方式没选:调试默认选择: ...