MySQL和mybatis查询相关
0.mybatis的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 namespace="me.gacl.dao.UserMapper" > </mapper>
1.mybatis 中 foreach collection的用法
<select id="dynamicForeachTest" parameterType="java.util.List" resultType="Blog">
select * from t_blog where id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
2.mybatis批量插入更新
方法一:
<update id="batchUpdate" parameterType="java.util.List">
<foreach separator=";" index="index" item="item" collection="list" close="" open="">
update sys_group set level = #{item.level,jdbcType=INTEGER}
where group_id = #{item.groupId,jdbcType=INTEGER}
</foreach>
</update>
方法二:
<update id="batchUpdate1" parameterType="java.util.List">
update sys_group set level = null where level in
<foreach separator="," index="index" item="item" collection="list" close=")" open="(">
#{item}
</foreach>
</update>
3.mybatis映射
<resultMap id="BaseResultMap" type="me.gacl.domain.User" >
<id column="user_id" property="userId" jdbcType="CHAR" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="user_birthday" property="userBirthday" jdbcType="DATE" />
<result column="user_salary" property="userSalary" jdbcType="DOUBLE" />
</resultMap>
<sql id="Base_Column_List" >
user_id, user_name, user_birthday, user_salary
</sql>
4.mybatis查询select
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from t_user
where user_id = #{userId,jdbcType=CHAR}
</select>
5.mybatis删除delete
<delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
delete from t_user
where user_id = #{userId,jdbcType=CHAR}
</delete>
6.mybatis插入insert
一:
<insert id="insert" parameterType="me.gacl.domain.User" >
insert into t_user (user_id, user_name, user_birthday,
user_salary)
values (#{userId,jdbcType=CHAR}, #{userName,jdbcType=VARCHAR}, #{userBirthday,jdbcType=DATE},
#{userSalary,jdbcType=DOUBLE})
</insert>
二:
<insert id="insertSelective" parameterType="me.gacl.domain.User" >
insert into t_user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="userId != null" >
user_id,
</if>
<if test="userName != null" >
user_name,
</if>
<if test="userBirthday != null" >
user_birthday,
</if>
<if test="userSalary != null" >
user_salary,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="userId != null" >
#{userId,jdbcType=CHAR},
</if>
<if test="userName != null" >
#{userName,jdbcType=VARCHAR},
</if>
<if test="userBirthday != null" >
#{userBirthday,jdbcType=DATE},
</if>
<if test="userSalary != null" >
#{userSalary,jdbcType=DOUBLE},
</if>
</trim>
</insert>
7.mybatis更新update
一:
<update id="updateByPrimaryKey" parameterType="me.gacl.domain.User" >
update t_user
set user_name = #{userName,jdbcType=VARCHAR},
user_birthday = #{userBirthday,jdbcType=DATE},
user_salary = #{userSalary,jdbcType=DOUBLE}
where user_id = #{userId,jdbcType=CHAR}
</update>
二:
<update id="updateByPrimaryKeySelective" parameterType="me.gacl.domain.User" >
update t_user
<set >
<if test="userName != null" >
user_name = #{userName,jdbcType=VARCHAR},
</if>
<if test="userBirthday != null" >
user_birthday = #{userBirthday,jdbcType=DATE},
</if>
<if test="userSalary != null" >
user_salary = #{userSalary,jdbcType=DOUBLE},
</if>
</set>
where user_id = #{userId,jdbcType=CHAR}
</update>
8.mybatis插入返回主键
<insert id="insert" parameterType="me.gacl.domain.User" >
<selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id">
SELECT LAST_INSERT_ID()
</selectKey>
insert into t_user (user_id, user_name, user_birthday,
user_salary)
values (#{userId,jdbcType=CHAR}, #{userName,jdbcType=VARCHAR}, #{userBirthday,jdbcType=DATE},
#{userSalary,jdbcType=DOUBLE})
</insert>
主键值封装到User对象中了。
int count = user.getId()获取的就是主键值。
9.避免重复插入 insert into select from where not exists
CREATE TABLE `user` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`age` int(10) DEFAULT NULL,
`name` varchar(100) DEFAULT NULL,
`role` varchar(100) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
`phone` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8;
INSERT INTO user (
`age`,
`name`,
`role`,
`email`
) SELECT
22,
'admin',
'admin',
'admin'
FROM
user
WHERE
NOT EXISTS (
SELECT
`name`
FROM
user
WHERE
`name` = 'admin'
) LIMIT 1;
10. 插入或更新 ON DUPLICATE KEY UPDATE
create table daily_hit_counter ( day date not null, slot tinyint unsigned not null, cnt int unsigned not null, primary key(day, slot) ) engine = InnoDB;
insert into daily_hit_counter (day, slot, cnt) values ('2017-11-19', 1, 1) ON DUPLICATE KEY UPDATE cnt = cnt + 1;
insert into daily_hit_counter (day, slot, cnt) values ('2017-11-19', 2, 1) ON DUPLICATE KEY UPDATE cnt = cnt + 1;

插入之前已有数据。
第一条执行更新,第二条执行插入。
11.INSERT INTO SELECT语句与SELECT INTO FROM语句区别
语句形式为:Insert into Table2(field1,field2,…) select value1,value2,… from Table1
或者:Insert into Table2 select * from Table1
注意:
(1)要求目标表Table2必须存在,并且字段field,field2…也必须存在
(2)注意Table2的主键约束,如果Table2有主键而且不为空,则 field1, field2…中必须包括主键
(3)注意语法,不要加values,和插入一条数据的sql混了,不要写成:
Insert into Table2(field1,field2,…) values (select value1,value2,… from Table1)
由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量。
2.SELECT INTO FROM语句
语句形式为:SELECT vale1, value2 into Table2 from Table1
要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中。
注意:如果在sql/plus或者PL/SQL执行这条语句,会报”ORA-00905:缺失关键字”错误,原因是PL/Sql与T-SQL的区别。
T-SQL中该句正常,但PL/SQL中解释是:
select..into is part of PL/SQL language which means you have to use it inside a PL/SQL block. You can not use it in a SQL statement outside of PL/SQL.
即不能单独作为一条sql语句执行,一般在PL/SQL程序块(block)中使用。
如果想在PL/SQL中实现该功能,可使用Create table newTable as select * from …:
如: create table NewTable as select * from ATable;
NewTable 除了没有键,其他的和ATable一样
———SQL SELECT INTO语法介绍
SQL SELECT INTO 语句可用于创建表的备份复件。
SELECT INTO 语句
SELECT INTO 语句从一个表中选取数据,然后把数据插入另一个表中。
SELECT INTO 语句常用于创建表的备份复件或者用于对记录进行存档。
SQL SELECT INTO 语法
您可以把所有的列插入新表:
SELECT * INTO new_table_name [IN externaldatabase] FROM old_tablename
或者只把希望的列插入新表:
SELECT column_name(s) INTO new_table_name [IN externaldatabase] FROM old_tablename
SQL SELECT INTO 实例 - 制作备份复件
下面的例子会制作 “Persons” 表的备份复件:
SELECT * INTO Persons_backup FROM Persons
IN 子句可用于向另一个数据库中拷贝表:
SELECT * INTO Persons IN ‘Backup.mdb’ FROM Persons
如果我们希望拷贝某些域,可以在 SELECT 语句后列出这些域:
SELECT LastName,FirstName
INTO Persons_backup
FROM Persons
SQL SELECT INTO 实例 - 带有 WHERE 子句
我们也可以添加 WHERE 子句。
下面的例子通过从 “Persons” 表中提取居住在 “Beijing” 的人的信息,创建了一个带有两个列的名为 “Persons_backup” 的表:
SELECT LastName,Firstname INTO Persons_backup FROM Persons WHERE City=’Beijing’
SQL SELECT INTO 实例 - 被连接的表
从一个以上的表中选取数据也是可以做到的。
下面的例子会创建一个名为 “Persons_Order_Backup” 的新表,其中包含了从 Persons 和 Orders 两个表中取得的信息:
SELECT Persons.LastName,Orders.OrderNo
INTO Persons_Order_Backup
FROM Persons
INNER JOIN Orders
ON Persons.Id_P=Orders.Id_P
11来源于:这里
MySQL和mybatis查询相关的更多相关文章
- 【mybatis】【mysql】mybatis查询mysql,group by分组查询报错:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column
mybatis查询mysql,group by分组查询报错:Expression #1 of SELECT list is not in GROUP BY clause and contains no ...
- Mysql的子查询相关知识,少但是精
Mysql子查询 概念分析: 根据相关性分: (1)不相关子查询:一条Sql语句中含有多条SELECT语句,先执行子查询,再执行外查询,子查询可对立运行 关键字:(1)先子查询,再外查询 (2)可以对 ...
- Mybatis使用MySQL进行模糊查询时输入中文检索不到结果
Mybatis使用MySQL进行模糊查询时输入中文检索时,需要在jdbcURL后增加参数 ?useUnicode=true&characterEncoding=UTF-8
- myBatis查询报错 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
myBatis查询报错 You have an error in your SQL syntax; check the manual that corresponds to your MySQL se ...
- mybatis查询mysql的datetime类型数据时间差了14小时
场景: 数据库字段: mybatis使用 now() 生成时间. 结果: 使用mybatis查询mysql中的数据时,所有时间都比数据库时间多了14小时,考虑了一下,初步判定是系统时区的问题.因为my ...
- mybatis查询mysql数据库tinyint(1)变为boolean类型
mybatis查询mysql数据库对象转化为Map,tinyint(1)被转化为boolean类型,可以t通过避免使用tinyint(1)来解决.
- 【时区问题】SpringBoot+mybatis查询mysql的datetime类型数据时间差14小时
[时区问题]MyBatis查询MySQL的datetime类型数据时间差14小时 故障解决方式 与数据库连接时,定义时区,避免mybatis框架从mysql获取时区.在连接上加上 serverTime ...
- 关于mysql,需要掌握的基础(一):CRUD、存储引擎、单表查询相关、多表查询join、事务并发、权限管理等等
目录 关于mysql,需要掌握的基础(一): 1.了解数据库sql.数据库系统.数据库管理系统的概念. 2.了解DDL.DML.DQL语句是什么? 3.了解存储引擎.存储引擎[InnoDB 和 MyI ...
- mybatis查询mysql 数据库中 BLOB字段,结果出现乱码
起因 mybatis-plus 通过Mapper 查询数据,映射出来的BLOB字段中的yml数据中文是乱码的 --- DefaultValue: '' Formula: '' HintContent: ...
随机推荐
- 逆向破解之160个CrackMe —— 002-003
CrackMe —— 002 160 CrackMe 是比较适合新手学习逆向破解的CrackMe的一个集合一共160个待逆向破解的程序 CrackMe:它们都是一些公开给别人尝试破解的小程序,制作 c ...
- 8、JAVA中的用户输入(I/0交互过程)
这里在数组的学习中用到了用户输入,也就是交互模式,日常的数据,不可能每一次都是程序员定义好的,终究需要用户与程序之间进行交互,机器等待用户输入,用户通过键盘输入所需数据(数据包括,字符,字串,数值等) ...
- css实现左边高度自适应右边高度
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 给面试官讲明白:一致性Hash的原理和实践
"一致性hash的设计初衷是解决分布式缓存问题,它不仅能起到hash作用,还可以在服务器宕机时,尽量少地迁移数据.因此被广泛用于状态服务的路由功能" 01分布式系统的路由算法 假设 ...
- 算法与数据结构基础 - 二叉树(Binary Tree)
二叉树基础 满足这样性质的树称为二叉树:空树或节点最多有两个子树,称为左子树.右子树, 左右子树节点同样最多有两个子树. 二叉树是递归定义的,因而常用递归/DFS的思想处理二叉树相关问题,例如Leet ...
- linux装OpenOffice后传---中文乱码的解决
上一篇的博客已经详细的介绍了linux系统上如何安装OpenOffice,安装之后使用发现转换的pdf出现中文乱码.后来发现是linux上没有中文对应的那个字体. 字体准备 在windows上的位置 ...
- JavaWeb配置详解(结合框架SpringMVC)
详解 先说一说常识性的东西,我们的JavaWeb程序运行一开始走的是web.xml文件,这是我们的核心文件,可以说没有web.xml文件我们就无法运行项目,这个文件长什么样子,读者自己新建一个web项 ...
- HelloDjango 第 07 篇:创作后台开启,请开始你的表演!
作者:HelloGitHub-追梦人物 文中涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 在此之前我们完成了 django 博客首页视图的编写,我们希望首页展示发布的博客文章列 ...
- Lasso估计学习笔记(二)
先看Lasso估计学习笔记(一),这篇是续的上一篇
- mybatisX插件的使用
MybatisX 插件 快捷 mapper方法生成对应-----> mapper.xml中 :ALT +enter