mysql

1 每个语句的结束记得加分号;

2where条件里再做if分支
    SELECT *FROM `table` WHERE IF( `parentID` is null, `plan_id` <10, `plan_id` >500 )
  还可以再嵌套使用
  WHERE IF(device_id is null , IF(globle_name IS NULL OR globle_name = '', 1 = 1, (P.FileName like concat('%', globle_name, '%') OR D.Name like concat('%', globle_name, '%') OR E.Name like concat('%', globle_name, '%'))), D.id = device_id and P.IsPublished = 1)

3 is null, is not null 用于判断某个字段或是变量为null或不为null.

4 isnull(expr) 的用法:
  如expr 为null,那么isnull() 的返回值为 1,否则返回值为 0。

5 ifnull(exp1,exp2)如果exp1是null的话,就用exp2的值,否则还是用exp1的值

6 NULLIF(expr1,expr2)    如果expr1 =   expr2     成立,那么返回值为NULL,否则返回值为   expr1

7 out参数,存储过程的参数要指明in,out。在调用存储过程时,用带@开头的变量来接收out的值,比如 call procedure1(1,1,@result);

8 用户自定义变量不用声明,直接在变量前加@就可用

9 select count(distinct id) as rowCount from table1,sql server 同样可行。

10 Mysql数据库里表名大小写敏感,字段名大小写不敏感,所以写表名或表的别名时一定要注意大小写。sql server里大小写不敏感

11 FIND_IN_SET(str,strlist)。 strlist为一个用逗号隔开N个字符组成的字符串,返回值的范围在 0 到 N 之间。匹配到值的话,就返回该值的位置,匹配不到返回0。不走索引。

12 in函数, wher id in (1,2,3);如果查询的字段为主键的话,会走索引查询,效果比find_in_set高很多。

EXPLAIN select * from Device where find_in_set(id, '1,2,3,null');  range为all,
EXPLAIN select * from Device WHERE ID IN(1,2);  range为const ,而且还是走的聚集索引查找

13 用union all代替union, union 会过滤掉重复的数据。如果知道数据不会重复,或不在乎是否重复,请使用UNION ALL

14  一些特殊的表名和字段名,在使用时需要加上反单引号··,比如SELECT * FROM `Procedure`; 这个符号是和波浪线~同一个键盘的

15   select utc_timestamp()取到UTC的时间,select now()取数据库所在服务器的时间

插入数据后返回自增ID的方法:

declare newID int(15); //这种变量声明在存储过程里使用

insert into `Table1` (Field1,Field2) values (1,2) ;

select @@IDENTITY INTO newID;  //还有很多其他的方式取,不一一列举

用逗号分隔的数值做存储过程的参数来实现查询功能

CREATE DEFINER=`admin`@`%` PROCEDURE `SP_CREATE_PERSON_TO_GROUP`( in groupIdsWithComma VARCHAR(1000), in userId INT(15))
BEGIN
DECLARE new_id INT(15);
insert into `person` (UserId) values (userId) ;
-- 取得最新的自增值
SELECT @@IDENTITY INTO new_id ; //将查询的结果直接添加到表中
insert into personGroup(PersonId,GrouopId)
select new_id , GroupID from Group where id in(deviceIdsWithComma); END

调用存储过程:

call SP_CREATE_PERSON_TO_GROUP('1,3',1);

varchar和char的区别

char是固定长度的,用于存储固定长度的数据;

varchar会根据具体的长度来使用存储空间,另外varchar需要用额外的1-2个字节存储字符串长度。
  1). 当字符串长度小于255时,用额外的1个字节来记录长度
  2). 当字符串长度大于255时,用额外的2个字节来记录长度
  比如char(255)和varchar(255),在存储字符串"hello world"时,char会用一块255个字节的空间放那个11个字符;它先计算字符串长度为11,然后再加上一个记录字符串长度的字节,一共用12个字节存储,这样varchar在存储不确定长度的字符串时会大大减少存储空间。

varchar和nvarchar的区别
1. varchar(n):长度为n个字节的可变长度且非Unicode的字符数据。n必须是一个介于1和8,000之间的数值。存储大小为输入数据的字节的实际长度

2. nvarchar(n):包含n个字符的可变长度Unicode字符数据。n的值必须介于1与4,000之间。字节的存储大小是所输入字符个数的两倍。所输入的数据字符长度可以为零

 将查询结果放到变量里

SELECT
c1, c2, c3, ...
INTO
@v1, @v2, @v3,...
FROM
table_name
WHERE
condition;
select @v1;

  这里的变量不用declare来声明,直接拿来用。变量的数量必须和查询的列数一致,而且只能用在查询结果是0行或1行的情况下。

The number of variables must be the same as the number of columns or expressions in the select list. In addition, the query must returns zero or one row.

myBatis

动态构建sql语句

if

<if test="state != null">
state = #{state}
</if>

  

choose when otherwise,不想写太多条件语句的时候,可以用这个

<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<choose>
<when test="title != null">
AND title like #{title}
</when>
<when test="author != null and author.name != null">
AND author_name like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>

  

where, 会自动去掉多余的and 或or

<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG
<where>
<if test="state != null">
and state = #{state}
</if>
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</where>
</select>

  

trim ,显示的去掉字符

<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>

set,会自动去掉多余的逗号

<update id="updateAuthorIfNecessary">
update Author
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
<if test="bio != null">bio=#{bio},</if>
</set>
where id=#{id}
</update>

  

foreach, 多用于查询语句, 很好用,collection是传入的集合类型的变量名

<select id="findTest" resultType="com.map.expample.entity.Test">
  select * from Tests
  <where>
    id in
    <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
    </foreach>
  </where>
</select>

bind

<select id="selectBlogsLike" resultType="Blog">
<bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />
SELECT * FROM BLOG
WHERE title LIKE #{pattern}
</select>

  

mybatis里所有的判断都是用test  ="",比如test="username != null"

mybatis调用存储过程

 <!-- 创建多个文档 -->
<select id="createProcedures" parameterType="map" >
<foreach collection="entities" item="entity" index="index" >
call SP_CREATE_PROCEDURE(
#{entity.fileName, mode=IN, jdbcType=VARCHAR},
#{entity.filePath, mode=IN, jdbcType=VARCHAR},
#{entity.deviceIdsWithComma, mode=IN, jdbcType=VARCHAR},
#{entity.createUserId, mode=IN, jdbcType=VARCHAR}
);
</foreach>
</select>

 

mybatis语法,从数据库查出来的字段可以多于或少于resultType指定的类的属性,都会按能对应的上的名字进行映射。

myBatis JDBC type和java数据类型对应表:

resulstMap 的使用

<resultMap id="detailedBlogResultMap" type="Blog">
<constructor>
<idArg column="blog_id" javaType="int"/>
</constructor>
<result property="title" column="blog_title"/>
<association property="author" javaType="Author">
<id property="id" column="author_id"/>
<result property="username" column="author_username"/>
<result property="password" column="author_password"/>
<result property="email" column="author_email"/>
<result property="bio" column="author_bio"/>
<result property="favouriteSection" column="author_favourite_section"/>
</association>
<collection property="posts" ofType="Post">
<id property="id" column="post_id"/>
<result property="subject" column="post_subject"/>
<association property="author" javaType="Author"/>
<collection property="comments" ofType="Comment">
<id property="id" column="comment_id"/>
</collection>
<collection property="tags" ofType="Tag" >
<id property="id" column="tag_id"/>
</collection>
<discriminator javaType="int" column="draft">
<case value="1" resultType="DraftPost"/>
</discriminator>
</collection>
</resultMap>
<select id="selectBlogDetails" parameterType="int" resultMap="detailedBlogResultMap">
select
B.id as blog_id,
B.title as blog_title,
B.author_id as blog_author_id,
A.id as author_id,
A.username as author_username,
A.password as author_password,
A.email as author_email,
A.bio as author_bio,
A.favourite_section as author_favourite_section,
P.id as post_id,
P.blog_id as post_blog_id,
P.author_id as post_author_id,
P.created_on as post_created_on,
P.section as post_section,
P.subject as post_subject,
P.draft as draft,
P.body as post_body,
C.id as comment_id,
C.post_id as comment_post_id,
C.name as comment_name,
C.comment as comment_text,
T.id as tag_id,
T.name as tag_name
from Blog B
left outer join Author A on B.author_id = A.id
left outer join Post P on B.id = P.blog_id
left outer join Comment C on P.id = C.post_id
left outer join Post_Tag PT on PT.post_id = P.id
left outer join Tag T on PT.tag_id = T.id
where B.id = #{id}
</select>
  

refer:https://www.jianshu.com/p/75493a342f63

MYSQL mybatis的更多相关文章

  1. mysql+mybatis递归调用

    递归调用的应用场景常常出现在多级嵌套的情况,比如树形的菜单.下面通过一个简单的例子来实现mysql+mybatis的递归. 数据模型 private Integer categoryId; priva ...

  2. Java Spring+Mysql+Mybatis 实现用户登录注册功能

    前言: 最近在学习Java的编程,前辈让我写一个包含数据库和前端的用户登录功能,通过看博客等我先是写了一个最基础的servlet+jsp,再到后来开始用maven进行编程,最终的完成版是一个 Spri ...

  3. Spring Boot 项目学习 (二) MySql + MyBatis 注解 + 分页控件 配置

    0 引言 本文主要在Spring Boot 基础项目的基础上,添加 Mysql .MyBatis(注解方式)与 分页控件 的配置,用于协助完成数据库操作. 1 创建数据表 这个过程就暂时省略了. 2 ...

  4. springBoot+mysql+mybatis demo [基本配置] [遇到的问题]

    springBoot+mysql+mybatis的基本配置: 多环境 application.properties spring.profiles.active=dev spring.applicat ...

  5. springboot学习笔记:8. springboot+druid+mysql+mybatis+通用mapper+pagehelper+mybatis-generator+freemarker+layui

    前言: 开发环境:IDEA+jdk1.8+windows10 目标:使用springboot整合druid数据源+mysql+mybatis+通用mapper插件+pagehelper插件+mybat ...

  6. springboot学习笔记:11.springboot+shiro+mysql+mybatis(通用mapper)+freemarker+ztree+layui实现通用的java后台管理系统(权限管理+用户管理+菜单管理)

    一.前言 经过前10篇文章,我们已经可以快速搭建一个springboot的web项目: 今天,我们在上一节基础上继续集成shiro框架,实现一个可以通用的后台管理系统:包括用户管理,角色管理,菜单管理 ...

  7. springboot学习笔记:10.springboot+atomikos+mysql+mybatis+druid+分布式事务

    前言 上一篇文章我们整合了springboot+druid+mybatis+mysql+多数据源: 本篇文章大家主要跟随你们涛兄在上一届基础上配置一下多数据源情况下的分布式事务: 首先,到底啥是分布式 ...

  8. mysql+mybatis 插入可递增字段库表操作

    mysql本身类型介绍: BIGINT 8 字节 (-9 233 372 036 854 775 808,9 223 372 036 854 775 807) (0,18 446 744 073 70 ...

  9. mysql,mybatis使用中遇到的类型转化的问题

    产生原因还没有明白,先记录一下. 使用DATEDIFF函数,计算两个日期的时间差.在mybatis中,resultType 是map.在代码中,根据map的key取值的时候. 在mysql 5.5.3 ...

  10. spring boot +mysql + mybatis + druid的整理(一)——单数据源

    一,使用spring boot脚手架搭建spring boot框架生成maven项目 如下图所示: 设置自定义的坐标,即左侧的Group和Artifact,右侧可以搜索添加一些依赖,搜索不到的可以在p ...

随机推荐

  1. 检测U盘插入、拨出状态

    头文件 #include <Dbt.h> 关键代码: LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LP ...

  2. 利用反射和JDBC元数据实现更加通用的查询方法

    package com.at221.jdbc; import java.io.IOException; import java.io.InputStream; import java.sql.*; i ...

  3. 蓝桥杯c/c++省赛真题——日志统计

    标题:日志统计 [问题描述]小明维护着一个程序员论坛.现在他收集了一份"点赞"日志,日志共有N行.其中每一行的格式是:ts id  表示在ts时刻编号id的帖子收到一个" ...

  4. vuejs简单介绍特点

    官网:https://cn.vuejs.org/ vue是一个渐进式的框架, 是一个轻量级的框架, 也不算是一个框架, 他核心只关注图层, 是一个构建数据驱动的web界面,易于上手, 还便于于第三方库 ...

  5. qemu创建vm和vcpu进入kvm的流程

    kvm是一个内核模块,它实现了一个/dev/kvm的字符设备来与用户进行交互,通过调用一系列ioctl函数可以实现qemu和kvm之间的切换. 1.qemu发起KVM_CREATE_VM的ioctl创 ...

  6. PAT (Basic Level) Practice (中文)1006 换个格式输出整数 (15 分)

    题目链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805318855278592 #include <iost ...

  7. JS动态添加行列

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Add-Delete Row.a ...

  8. Java8-Map

    1.Staff实体 public class Staff { private String name; private int age; private String address; public ...

  9. centos6删除mysql安装

    1.查看已经安装了的mysql包: 2.卸载mysql: 3.查看剩下的mysql安装包: 4.逐个删除剩下的mysql安装包: 5.删除完后再次查看,以确保已删除干净: 6.删除自己安装的mysql ...

  10. QGraphicsItem的paint函数的一些相关问题

    在QGraphicsItem中,一个成员函数paint(),其声明如下: void QGraphicsItem::paint ( QPainter * painter, const QStyleOpt ...