Spring Boot入门6

回顾:
    ajax发送请求

    $.get(url,param,function)
    $.post(url,param,function)
    $.ajax({
        url:,
        type:,
        data:,
        success:,
    })
    校验用户名:

    编写xml文件,能够看懂xml
        <书架>
            <书>
                <书名></书名>
                <作者></作者>
                <价格></价格>
            </书>
        </书架>
    DOM解析: 一次性将整个文档加载到内存中,形成一棵dom树,可以做增删改查 , 导致内存溢出 , 
    SAX解析: 逐行读取每一行的内容, 读取速度快

    DOM4J: 解析框架, 集成了SAX解析和DOM优点

    了解什么是约束: 约束xml文档必须按照怎样的规则来编写


    json: 是一种轻量级的数据交换格式
        {书架:
            [
                {书:
                    {书名:"",
                     作者:""
                    }
                        }]}
    xml和json的区别?

    对象如何转成json格式字符串
        javascript: JSON.stringify
        java      : new GSON().toJson(对象)
               springboot : return 对象
    json格式字符串转对象
      js:  JSON.parse
      java: new GSON().fromJson(str,类.class)

    省市联动案例

SQL : Structured Query Language    

一、MySql

1. 数据库概述

数据库( Database )是按照数据结构来组织、存储和管理数据的仓库 。 数据按照特定的格式存储起来,用户可以通过SQL(Structured Query Language)对数据库中的数据进行增删改查, 这比咱们之前使用文件来管理数据要方便得多。

  • 数据库 & 表

数据库管理系统(DataBase Manager System ) , 简称 DBMS , 是指一种操作和管理数据库的软件,这类软件用于创建、使用、维护数据库,对数据库进行统一管理和控制,以保证数据的完整性和安全性。我们可以通过它访问数据库中的表数据

数据库其实是存储数据的仓库,在数据库中有数据表,我们所存的数据其实都是往数据表里面存储,而数据库又管理者很多张表,表示这几张表示一批数据的集合。正如我们第一天使用文件存储学生数据一样,如果使用数据库来存储,我们就会有一个表叫做student表,如果还需要存储教师信息,那么还应该有一个teacher表 ,而这两张表正好位于一个数据库中。

这里写图片描述

  • 常见的数据库 SQL 适合做
  1. mysql 开源免费 , 小型数据库, oracle收购
  2. oracle 收费 ,大型数据库,oracle公司产品
  3. sql server 收费 微软产品
  4. SQLite 嵌入式小数据库 、 移动端常用(Android | ios)
  5. DB2 收费 , IBM 产品
  6. H2 开源 ,嵌入式 | 内存版 数据库,纯java实现

以上数据库都是关系型数据库 —> 里面存储的数据,可以建立关系。

非关系型数据库。 NoSql not only sql 不仅仅是sql 适合做缓存 购物车

mogodb redis … 秒杀 抢购

2. MySql 安装

  • mysql安装

    mysql -uroot -p123456

    -u 表示的是 user

    -p password

  • 可视化工具安装

3. 数据库操作

  • SQL分类:
DDL : 数据定义语言,定义数据库/表结构 : create创建,drop删除,alter修改,truncate截断
DML : 数据操纵语言,操作的是数据     : insert插入,update更新,delete删除
DCL : 数据库控制语言,控制的权限相关  : grant(授权) ,revoke(取消授权)
DQL : 数据查询语言,查询数据         : select查询
增删改查
数据库
    创建数据库,删除数据库,修改数据库,查看数据库
表
    创建表,修改表,删除表
表中数据
    插入数据,修改数据,删除数据,查询数据

先使用可视化工具演练过一次后,再使用命令演示

  • 创建数据库
create database xxx;
  • 删除数据库
drop database xxx;
  • 修改数据库
# 一般很少修改数据库 , 如果修改,只会修改数据库的编码,这一般很少修改。
alter database xxx character set gbk;

alter database xxx character set utf8;
  • 显示数据库
show databases;

# 显示这个数据库创建的语句,以及编码是什么
show create database xxxx; 

4.字段类型介绍

java            mysql

int             int
float           float
double          double
char(字符)/String(字符串)        char/varchar    固定长度(即便长度不够,会补空格) | 可变长度 (数据多长,长度多长)
Date            date(日期)|time(时间)|datetime (日期和时间)| timestamp(日期时间 ,会自动使用当前时间):

文件类型         BLOB | TEXT : TEXT 文本类型 , BLOB 二进制类型

约束:
    单表约束: 
        主键约束: primary key 
        唯一约束: unique
        非空约束: not null

5. 数据库表操作

先使用可视化工具演练过一次后,再使用命令演示

  • 创建数据表
语法: create table 表名(字段名  类型(长度) 【约束】 , 字段名 类型(长度)[约束]);

CREATE TABLE aa (id INTEGER PRIMARY KEY AUTO_INCREMENT , username VARCHAR(25));
  • 删除数据表
语法: delete from --表名

delete from aa;  -- 只会清空表数据,不释放空间(序号接上以前),表依然存在
truncate table aa ; -- 清空表数据,释放空间(序号从1开始),表依然存在

DROP TABLE aa;   -- 删除表,一切都消失
  • 修改数据表
alter table 表名 add 列名 类型(长度) 约束;        --修改表添加列. 
alter table 表名 modify 列名 类型(长度) 约束; --修改列的类型长度及约束.
alter table 表名 change 旧列名 新列名 类型(长度) 约束;    --修改列名.
alter table 表名 drop 列名;                 --删除列.
rename table 表名 to 新表名;                 --修改表名(比较少用)
  • 显示数据表
show tables ; -- 显示当前数据库的所有表

desc  aa;  --查看具体某张表的结构

show create table aa; --显示建表语句

6. 约束介绍

  • 主键约束:primary key

关键字(primary key) 是表中的一个或多个字段,它的值用于唯一地标识表中的某一条记录 。 比如如何突出一名学生的唯一性,一般我们采用学号来表示,如何表示一个人的唯一,我们可以使用身份证号来表示。但是在数据库范畴里面,表示一条记录的唯一,我们通过会增加id 列来表示主键。

主键必须是唯一、 不能为空 , 可以设置为自增长。

# 主键约束可以在建表的时候就给定
create table stu(id int primary key auto_increment , name varchar(25));

# 也可以后面在修改,增加主键约束
create table stu(id int , name varchar(25));

alter table stu modify id int primary key ;

# 如果还想增加自增长,那么还可以继续修改
alter table stu modify id int auto_increment;
  • 唯一约束:unique 主键的值 也是唯一的。

唯一约束指的是表示某个列的值唯一不允许重复。

# 可以在建表的时候给出
create table stu(id int primary key auto_increment , name varchar(25) unique);

# 或者后面再修改
create table stu(id int primary key auto_increment , name varchar(25));

alter table stu modify name varchar(25) unique;
  • 非空约束:not null

非空约束,指的是具体某一列不允许为空,必须有值。

# 可以在建表的时候给出
create table stu(id int primary key auto_increment , name varchar(25) not null);

# 或者后面再修改
create table stu(id int primary key auto_increment , name varchar(25));

alter table stu modify name varchar(25) not null;

7. 表记录CRUD

该小节注重的是表中数据的操作。

1. 增加

--添加一条记录,对具体某些列赋值。顺序要对应 ,若是字符或者日期需要添加 '' 引号
insert into 表 (列名1,列名2,列名3..) values  (值1,值2,值3..); 


--添加一条记录,表中的所有列都要赋值。    
insert intovalues (值1,值2,值3..); 

2. 删除

-- 删除的命令和上面删除表的命令非常相似。 只是多了where条件而已。
delete from 表名 [where 条件];

3. 修改

-- 修改表,针对具体某一列进行赋值
update 表名 set 字段名=值,字段名=值 [where 条件];

-- 如: 更新张三的年龄 ,后面的where 用于限定找到张三
update student set age = 18 where name = '张三';

--如果后面不带where, 那么将会把整张表的所有学生年龄都改为18
update student set age = 18;

4. 查询

  • 准备表中数据
--准备一张商品分类表(分类ID,分类名称,分类描述)
create table category(
    cid int primary key auto_increment,
    cname varchar(10),
    cdesc varchar(50)
);

--准备一张商品表(商品编号,商品名称,商品价格,商品描述,商品分类编号)
create table product(
    pid int primary key auto_increment,
    pname varchar(10),
    price double,
    pdesc varchar(50),
    cno   int
);

--数据的准备
insert into category values(null,'手机数码','黑马生产的小手机');
insert into category values(null,'鞋靴箱包','传智生产的包包');
insert into category values(null,'香烟酒水','黄鹤楼,茅台');
insert into category values(null,'馋嘴零食','卫龙辣条,周黑鸭');

insert into product values(null,'锤子',2999,'可以砸榴莲的手机',1);
insert into product values(null,'小米',1599,'为发烧而生',1);
insert into product values(null,'李宁',99,'不走寻常路',2);
insert into product values(null,'耐克',399,'just do it',2);
insert into product values(null,'黄鹤楼',20,'饭后一根烟,胜做活神仙',3);
insert into product values(null,'卫龙辣条',5,'卫龙辣条加料不加价',4);
insert into product values(null,'老干妈辣椒酱',9,'永远不变的味道',4);
insert into product values(null,'老干娘辣椒酱',19,'永远不变的味道',4);
  • 简单查询
--语法如下: 
select [distinct]*[列名,列名] from 表 [where 条件].

--例子: 
* 1.查询所有的商品.    select * from product;
* 2.查询商品名和商品价格. select pname,price from product;
* 3.别名查询.使用的关键字是as.as可以省略的.  
    * 3.1表别名:   select * from product as p;
    * 3.2列别名:select pname as pn from product;   
* 4.去掉重复值.  select distinct price from product;
    图书借阅系统,
        谁来借图书。
            张三 1
            张三 1
            张三 1
  • 条件查询

* 1.查询商品名称为十三香的商品所有信息:
    * select * from product where pname = '十三香';
* 2.查询商品价格>60元的所有的商品信息:
    * select * from product where price > 60;

    -- where后的条件写法:
    * > ,<,=,>=,<=,<>



    * like 使用占位符 _ 和 %  _代表一个字符 %代表任意个字符. 
        * select * from product where pname like '%新%';
    * in 在某个范围中获得值.
        * select * from product where pid in (2,5,8);
    * between 12 and 56
  • 排序查询
* 1.查询所有的商品,按价格进行排序.(asc-升序,desc-降序)
    * select * from product order by price;
* 2.查询名称有新的商品的信息并且按价格降序排序.
    * select * from product where pname like '%新%' order by price desc;

 按价格排序 、 分数排序 、 日期 、 年龄...

    order by 列  asc(升序) | desc(降序)
  • 聚合查询
* sum(),avg(),max(),min(),count();
* 1.获得所有商品的价格的总和.--select sum(price) from product;
* 2.获得所有商品的平均价格.--select avg(price) from product;
* 3.获得所有商品的个数.--select count(*) from product;
  • 分组查询
* 1.根据cno字段分组,分组后统计商品的个数.
    * select cno,count(*) from product group by cno;

* 2.根据cno分组,分组统计每组商品的平均价格,并且平均价格> 60;
    * select cno,avg(price) from product group by cno having avg(price)>60;

    having 关键字可以让我们筛选成组后的各种数据,可以理解为对分组后的数据进行再次过滤
  • 分页查询
-- 第一个?表示跳过前面多少条 , 第二个?表示返回多少条记录。
select * from product limit ? , ? ; 

/*
表的字段类型:
int,double,boolean

char(10) abcdefg    保存字符串 固定长度字符串   
    不满10位,会以空格占满10位
varchar(10) abcdefg 保存字符串 可变长度字符串
        不满10位, 不会以空格占满10位 , 只占7位

text : 大文本数据可以使用

date : 年月日

datetime : 年月日 时分秒

timestamp: 时间戳 , 年月日,时分秒, 2046年    

create table 表名(
列名 列的类型 [列的约束],
列名2 列的类型 [列的约束]
);              

*/

后台基本操作

登陆

Mysql -u root -p
123456

show databases; 查看数据库

– 创建一张person表: 编号,姓名,年龄,出生日期

 create table person(
     pid int,
     name varchar(30),
     age int,
     birthday timestamp
);

– 删除表

drop table person;

– 修改表
– 添加列名 add
alter table person add sex varchar(2);
– 重定义列 modify
alter table person modify sex int;
– 修改列名 change
alter table person change sex gender int;
– 删除列名 drop
alter table person drop gender;
– 修改表名
rename table person to per;

– 查看表
show tables;
show create table person;
desc person;

/*
表的约束: 约束表中的数据必须符合某种规则
主键约束: 唯一并且不能为空 primary key
唯一约束: 唯一可以为空 unique
非空约束: 不能为空 not null

表的五大约束:
主键约束
唯一约束
非空约束
检查约束 check 性别 男,女,妖

    外键约束 

*/

create table student(
sid int primary key,
name varchar(20) unique,
age int not null
);

/*
数据库/表 : create, drop, alter
表中数据的增删改: insert ,delete ,update
*/
/*
插入数据:
不指定列名,需要所有列都插入值
insert into 表名 values(列值1,列值2);
指定了列名,只需要插入对应列的值
insert into 表名(sid,name) values(值1,值2)
*/
create table student(
sid int primary key,
name varchar(20) unique,
age int not null
);

insert into student values(1,'zs',18);
insert into student(sid,age) values(3,20);

/*
删除数据: delete
delete from 表名 [where 条件(sid=1)]
delete from 表名 逐行删除表中所有数据

请说一下delete 和 truncate 的区别  
   DML          DDL
   逐行删除数据       先删除表,再创建表
            效率高

*/
delete from student where sid=1;

delete from student;

truncate table student;

/*
更新:update
update 表名 set 列名=新的值,列名=新的值 [where 条件] ;
*/
– 更新所有记录
update student set age=25;

– 更新3号记录的名字
update student set name='lisi' where sid=3;

/*
查询的操作:
准备工作,商品表,商品分类表
auto_increment : 表的自增属性, 当cid值插入为null的时候,数据库自动赋值
1.创建商品分类表category
分类的编号,分类名称,分类描述

create table category(
cid int primary key auto_increment,
cname varchar(20) unique,
cdesc varchar(100)
);

insert into category values(null,'手机数码','黑马制造');

2.创建商品表
```
    create table product(
      pid int primary key auto_increment,
      pname varchar(10),
      price float,  
      pdesc varchar(100),
      cno int   
    );
        ```

修改配置文件,支持中文:
 1.打开计算机服务,关闭mysql服务
 2.找到C:\Program Files (x86)\MySQL\MySQL Server 5.5\my.ini
    找到第57行:
        default-character-set=gbk
 3.打开计算机服务,启动mysql服务        

*/
insert into category values(null,’手机数码’,’黑马制造’);
insert into category values(null,’鞋靴箱包’,’传智制造’);
insert into category values(null,’香烟酒水’,’传智制造’);
insert into category values(null,’馋嘴零食’,’传智制造’);

insert into product values(null,’锤子’,2999,’可以砸榴莲的手机’,1);
insert into product values(null,’小米’,1599,’为发烧而生’,1);
insert into product values(null,’李宁’,99,’不走寻常路’,2);
insert into product values(null,’耐克’,399,’just do it’,2);
insert into product values(null,’黄鹤楼’,20,’饭后一根烟,胜做活神仙’,3);
insert into product values(null,’卫龙辣条’,5,’卫龙辣条加料不加价’,4);
insert into product values(null,’老干妈辣椒酱’,9,’永远不变的味道’,4);
insert into product values(null,’老干娘辣椒酱’,19,’永远不变的味道’,4);

/*
查询语句:
查询所有商品
select 要查询的列名 from 表名 [where 条件]
select * from product;
* 号代表查询所有列

where条件的写法:
关系运算符: > >= = < <= != <>
逻辑运算符: and or not
其它运算符:
           is null
           is not nll
           in(范围) 判断是否满足这个范围条件
           between..and.. 是否中区间内
           like
        _
        %       

*/
select pname from product;
– 查询价格大于400的所有商品
select * from product where price > 400;

– 查询价格在9-99之间的所有商品
select * from product where price>=9 and price <=99;

– 写法
select * from product where price between 9 and 99;

– 查询1,2,3分类对应的商品信息
select * from product where cno=1 or cno=2 or cno=3;

select * from product where cno in(1,2);

– 查询名称为null的记录
insert into product values(null,null,19,'永远不变的味道',4);
– 所有的值和null进行比较,结果都null
select * from product where pname=null;

select * from product where pname is null;

/*
模糊查询: like
_ : 匹配单个字符
% : 匹配任意个数的字符
*/
– 查询名称包含 辣 字所有商品信息
select * from product where pname like '%辣%';
– 查询名称中第三个字是辣的商品信息
select * from product where pname like '__辣%';

/*
聚合统计:
统计所有商品的总价 sum()
商品的总数 count()
商品平均价格 avg()
商品最大价格 max()
商品最小价格 min()
*/
– 商品的总价
select SUM(price) from product;

– 商品的总数
select COUNT(*) from product;

– 商品平均价格
select avg(price) from product;

– 最大价格
select MAX(price) from product;

– 最小价格
select MIN(price) from product;

/*
分组统计:
select 分组的条件,分组的目的 from 表名 group by 分组条件;

 分组的本质:先按照分组的条件进行排序,然后再做聚合操作

 where 和 having

 where 分组之前执行的 , 不能接聚合函数

 having 与Group by 结合在一起使用的,分组之后执行的,可以接聚合函数

*/
– 统计每种分类商品数量
select cno,COUNT(*) from product group by cno;

– 统计每种分类商品数量,平且数量大于1的
select cno,COUNT(*) from product group by cno having COUNT(*)>1;

– 排序 order by 列名 asc|desc
– asc : ascend 升序 (默认)
– desc: descend 降序
– 按照商品价格对商品进行排序
select * from product order by price asc;

select * from product order by price desc;

/*
select 输出的内容 from 从哪张表查询 where 条件过滤 group by 分组的条件 having 分组之后的条件过滤 order by 排序 limit
*/

/*
分页 : limit 起始索引(从0开始),查询几条
*/
– 查询第1-3条记录 第1页数据
select * from product limit 0,3;

– 查询第2页数据
select * from product limit 3,3;

– 查询第3页数据
select * from product limit 6,3;

– 角标的算法 : (页码-1)*每页显示的数量

select * from product;
/*
什么SQL: 结构化的查询语言

SQL分类:
DDL: 数据定义语言,结构: create ,drop ,alter ,truncate
DML: 数据操纵语言,数据: insert ,update,delete
DCL: 数据控制语言,控制的权限
DQL: 数据查询语言: select , where ,group by ,having,order by ,limit

数据库:
增加: create database 数据库名称
删除: drop database 数据库名称
修改: alter database 名称 character set 字符编码
查询:
show databases;
show create database 名称
其它:
use 名称

增加: create table 表名(
列名 列的类型 [列的约束] auto_increment,
列名 列的类型
)
列的类型: int ,double ,float , varchar(20), datetime,timestamp
列的约束:
primary key 主键约束
unique 唯一约束
not null 非空约束
删除: drop table 表名

 修改:
     添加列: alter table 表名 add 列名 列的类型 
     重定义列: alter table 表名 modify 列名 新的类型
     修改列名: alter table 表名 change 旧的列名 新的列名 类型
     删除列名: alter table 表名 drop 列名
     修改表名: rename table 表名 to 新表名

  查询:
       show tables;
       show create table 名称
       desc 名称 : 查询表结构       

表中数据:
增加: insert into 名称 values(值1,值2,值3)
删除: delete from 名称 [where 条件]
修改: update 表名 set 列名=值,列名=值 [where 条件]
查询: select 输出的列名 from 表名 where 条件 group by 分组条件 having 分组之后的条件过滤 order by 排序的列名 asc|desc limit 起始索引,显示几条

*/

猜你喜欢

转载自blog.csdn.net/www294993741/article/details/82559674