自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数HarmonyOS鸿蒙开发工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年HarmonyOS鸿蒙开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上HarmonyOS鸿蒙开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新
如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注鸿蒙获取)
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
因此在操作 MyBatis 时需要先创建数据库
复制如下代码至 MySQL 中
后续操作将针对该数据库进行演示
-- 创建数据库
drop database if exists excnblog;
create database excnblog DEFAULT CHARACTER SET utf8mb4;
-- 使用数据数据
use excnblog;
-- 创建表[用户表]
drop table if exists userinfo;
create table userinfo(
id int primary key auto\_increment,
username varchar(100) not null,
password varchar(32) not null,
photo varchar(500) default '',
createtime timestamp default current\_timestamp,
updatetime timestamp default current\_timestamp,
`state` int default 1
) default charset 'utf8mb4';
-- 创建文章表
drop table if exists articleinfo;
create table articleinfo(
id int primary key auto\_increment,
title varchar(100) not null,
content text not null,
createtime timestamp default current\_timestamp,
updatetime timestamp default current\_timestamp,
uid int not null, # 发布人
rcount int not null default 1, # 阅读量
`state` int default 1
)default charset 'utf8mb4';
-- 创建视频表
drop table if exists videoinfo;
create table videoinfo(
vid int primary key,
`title` varchar(250),
`url` varchar(1000),
createtime timestamp default current\_timestamp,
updatetime timestamp default current\_timestamp,
uid int
)default charset 'utf8mb4';
-- 添加用户信息
INSERT INTO `excnblog`.`userinfo` (`id`, `username`, `password`, `photo`, `createtime`, `updatetime`, `state`) VALUES
(1, 'admin', 'admin', '', '2021-12-06 17:10:48', '2021-12-06 17:10:48', 1);
-- 添加文章信息
insert into articleinfo(title,content,uid)
values('Java','Java正文',1);
-- 添加视频信息
insert into videoinfo(vid,title,url,uid) values(1,'java title','http://www.baidu.com',1);
MyBatis 的执行流程
创建对应流程
- 创建 controller 包 → 控制器层
- 创建 service 包 → 服务层
- 创建 mapper 包 → 数据持久层
- 创建 entity 包 → 实体类
🔎MyBatis—查询
查询用户信息
查询用户信息 → 此处使用的数据表为 userinfo(用户) 表
执行流程
创建实体类
实体类中的属性对应数据表的字段
(属性名与字段名可以不一一对应, 但建议保持一致)
创建 Interface 与 xml
- Interface → 定义所需的方法
- xml → 实现定义的方法
创建 Interface🍭
创建 Interface, 需添加@Mapper
注解, 目的是为了随着 Spring 项目的启动加载 UserMapper
Interface 中定义的方法的参数建议添加@Param
注解
创建 xml🍭
xml 的配置模板
namespace=""
, 标识当前的 xml 文件实现的哪个 Interface
<?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="">
</mapper>
注意对应关系🍂
namespace="com.example.demo.mapper.UserMapper"
表示当前的 xml 文件实现的是 com → example → demo → mapper → UserMapper(Interface)
在 xml 中编写 SQL 语句
写法1🍂
select * from userinfo where id = ${id}
写法2🍂
select * from userinfo where id = #{id}
大多数情况下使用 #
后面会介绍 $ 与 # 的区别
注意对应关系🍂
使用<select></select>
标签时通常需搭配 2 个属性
- id, 对应方法名
- resultType, 对应方法的返回值
模拟执行流程
服务层(Sevice) → 数据持久层(Mapper)
控制层(Controller) → 服务层(Service)
设置 URL 时不要大小写组合, 建议全部小写, 利用-
或_
分割
运行查看结果
🔎单元测试
定义
单元测试(Unit Testing), 指对软件中的最⼩可测试单元进⾏检查和验证的过程
在 Java 中, 最小可测试单元通常指方法
优点
- 简单, 直观, 快速的测试某个功能是否正确
- 可在打包时发现错误(因为打包之前所有单元测试必须通过, 否则无法打包成功)
- 使用单元测试测试功能时, 可以不污染连接的数据库(即不对数据库进行修改)
执行单元测试
引入依赖
SpringBoot 默认内置了该依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
生成单元测试
在 UserMapper(Interface) 中右击 → Generate🍂
Generate → Test🍂
点击 Test 弹出如下界面🍂
- Testing library, 单元测试的框架
- Class name, 生成的类名
- Superclass, 测试类继承的父类
- Destination package, 文件生成的目录名
- Generate, 生成选项
- setUp / @Before, 前置选项
- tearDown / @After, 后置选项
- Show inherited methods, 显示内置方法
勾选 getUserById() 方法, 点击 OK
弹出如下界面🍂
编写代码
- 添加注解
@SpringBootTest
- 在对应的单元测试方法中编写代码
Assertions—断言
如果断言失败, 后续代码将不会执行
方法 | 说明 |
---|---|
Assertions.assertEquals() | 判断两个对象或两个原始类型是否相等 |
Assertions.assertNotEquals() | 判断两个对象或两个原始类型是否不等 |
Assertions.assertSame() | 判断两个对象的引用是否指向同一个对象 |
Assertions.assertNotSame() | 判断两个对象的引用是否指向不同的对象 |
Assertions.assertTrue() | 判断给定的布尔值是否为 true |
Assertions.assertFalse() | 判断给定的布尔值是否为 false |
Assertions.assertNull() | 判断给定的对象引用是否为 null |
Assertions.assertNotNull() | 判断给定的对象引用是否不为 null |
🔎MyBatis—新增
使用show create table userinfo
查看哪些字段为非空(NOT NULL)
- username → NOT NULL
- password → NOT NULL
- createtime → NOT NULL(但含有默认值)
- updatetime → NOT NULL(但含有默认值)
新增用户信息(不返回用户 Id)
新增用户信息 → 此处使用的数据表为 userinfo(用户) 表
新增操作默认返回值为 Int 类型
新增操作的标签为<insert></insert>
使用<insert></insert>
标签时需搭配 id 属性
在 Interface 中定义方法
/\*\*
\* 新增用户信息
\* @author bibubibu
\* @date 2023/7/2
\*/
int add(Userinfo userinfo);
在 xml 中实现方法
<insert id="add">
insert into userinfo(username, password) values(#{username}, #{password})
</insert>
注意🍂
对于values(#{username}, #{password})
username 对应的是实体类中的属性(不是字段名)
password 对应的是实体类中的属性(不是字段名)
单元测试验证效果
非第一次添加单元测试时, 可能会出现 Error
点击 OK 即可
测试查看效果
针对当前代码, 无法得到添加对象成功后对象的 id🍂
userinfo 表中的 id 是自增的
对于当前运行结果, 表示已添加成功, 但无法得到添加对象的 id
新增用户信息(返回用户 Id)
返回用户 Id, 需在<insert></insert>
标签添加useGeneratedKeys
与keyProperty
- useGeneratedKeys, 是否使用生成的主键
- keyProperty, 将生成的主键赋值给哪个属性
在 Interface 中定义方法
/\*\*
\* 新增用户信息(返回用户 Id)
\* @author bibubibu
\* @date 2023/7/2
\*/
int addAndRetId(UserInfo userInfo);
在 xml 中实现方法
<insert id="addAndRetId" useGeneratedKeys="true" keyProperty="id">
insert into userinfo(username, password) values(#{username}, #{password})
</insert>
注意🍂
keyProperty="id"
对应的是类中的属性, 而非数据库中的字段
- Property, 属性
- Column, 字段
单元测试验证效果
🔎MyBatis—修改
修改用户信息
修改用户信息 → 此处使用的数据表为 userinfo(用户) 表
修改操作默认返回值为 Int 类型
修改操作的标签为<update></update>
使用<update></update>
标签时需搭配 id 属性
在 Interface 中定义方法
/\*\*
\* 修改用户信息
\* @author bibubibu
\* @date 2023/7/3
\*/
int updateUserInfo(UserInfo userInfo);
在 xml 中实现方法
<update id="updateUserInfo">
update userinfo set username = #{username}, password = #{password} where id = #{id}
</update>
单元测试验证效果
🔎MyBatis—配置打印执行的 SQL
# 设置 Mybatis 的 xml 保存路径
mybatis:
mapper-locations: classpath:mapper/\*Mapper.xml
configuration: # 配置打印 MyBatis 执行的 SQL
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 设置日志级别
logging:
level:
com:
example:
demo: debug
效果测试🍭
🔎MyBatis—删除
删除用户信息
删除用户信息 → 此处使用的数据表为 userinfo(用户) 表
删除操作默认返回值为 Int 类型
删除操作的标签为<delete></delete>
使用<delete></delete>
标签时需搭配 id 属性
在 Interface 中定义方法
/\*\*
\* 删除用户信息
\* @author bibubibu
\* @date 2023/7/3
\*/
int delUserInfo(@Param("id") Integer id);
在 xml 中实现方法
<delete id="delUserInfo">
delete from userinfo where id = #{id};
</delete>
单元测试验证效果
不污染数据库进行单元测试
利用注解@Transactional
注解@Transactional
既可以修饰类, 也可以修饰方法
举个栗子🌰
要求
测试删除功能是否正常 + 数据库中的数据不会被删除
(即不污染数据库进行单元测试)
此时可以利用注解@Transactional
删除功能正常🍂
数据库中的数据不会被删除🍂
🔎对比 # 与 $
根据 id 查询用户信息
/\*\*
\* 根据 Id 查询用户信息
\* @author bibubibu
\* @date 2023/7/2
\*/
UserInfo getUserById(@Param("id") Integer id);
使用 #🍂
<select id="getUserById" resultType="com.example.demo.entity.UserInfo">
select * from userinfo where id = #{id}
</select>
使用 $🍂
<select id="getUserById" resultType="com.example.demo.entity.UserInfo">
select * from userinfo where id = ${id}
</select>
运行结果🍂
均可正常运行
根据 username 查询用户信息
/\*\*
\* 根据 username 查询用户信息
\* @author bibubibu
\* @date 2023/7/3
\*/
List<UserInfo> getUserByName(@Param("username") String username);
使用 #🍂
<select id="getUserByName" resultType="com.example.demo.entity.UserInfo">
select * from userinfo where username = #{username}
</select>
使用 $🍂
<select id="getUserByName" resultType="com.example.demo.entity.UserInfo">
select * from userinfo where username = ${username}
</select>
运行结果🍂
使用 # 正常运行
使用 $ 无法正常运行
根据 id 进行排序
排序有 2 种类型
- 升序 → asc
- 降序 → desc
此处以降序举例
/\*\*
\* 根据 Id 进行排序
\* @author bibubibu
\* @date 2023/7/3
\*/
List<UserInfo> getUserOrderById(@Param("order") String order);
使用 #🍂
<select id="getUserOrderById" resultType="com.example.demo.entity.UserInfo">
select * from userinfo order by id #{order}
</select>
使用 $🍂
<select id="getUserOrderById" resultType="com.example.demo.entity.UserInfo">
select * from userinfo order by id ${order}
</select>
运行结果🍂
使用 # 无法正常运行
使用 $ 正常运行
总结
#{}
→ 编译预处理, 不存在 SQL 注入问题${}
→ 直接替换, 存在 SQL 注入问题
使用 $ 的注意事项🍂
一定是可穷举的值(例如关键字…), 在使用之前要对传递的值进行安全性验证
编译预处理是指 MyBatis 在处理 #{ } 时, 将 SQL 中的 #{ } 替换为 ?
直接替换是指 MyBatis 在处理 #{ } 时, 将 SQL 中的 #{ } 替换为参数的值
- 根据 id 查询用户信息🍂
#{}
→ 将#{id}
替换为?
, 参数为 Integer id, 赋值 ? 为对应类型的值(数值型数据无需''
)${}
→ 将${id}
替换为对应的 Integer id 的值- 即针对数值形数据,
#{}
无需添加''
(也可理解为直接替换的一种方式), 因此利用#
可以运行成功 select * from userinfo where id = 1
(✔)- 即针对数值形数据,
${}
直接替换, 因此利用$
可以运行成功 select * from userinfo where id = 1
(✔)
- 即针对数值形数据,
- 根据 username 查询用户信息🍂
#{}
→ 将#{username}
替换为?
, 参数为 String username, 赋值 ? 为对应类型的值(字符串型数据需''
)${}
→ 将${username}
替换为对应的 String username 的值- 即针对字符串型数据,
#{}
会添加''
, 因此利用#
可以运行成功 select * from username where username = 'Tom'
(✔)- 即针对字符串型数据,
${}
直接替换, 因此利用$
无法运行成功 select * from username where username = Tom
(✘)
- 即针对字符串型数据,
- 根据 id 进行排序🍂
#{}
→ 将#{order}
替换为?
, 参数为 String order, 赋值 ? 为对应类型的值(字符串型数据需''
)${}
→ 将${order}
替换为对应的 String order 的值- 即针对字符串型数据,
#{}
会添加''
, 因此利用#
无法运行成功(关键字无需添加''
) select * from userinfo order by id 'desc'
(✘)- 即针对字符串型数据,
${}
直接替换, 因此利用$
可以运行成功(关键字无需添加''
) select * from userinfo order by id desc
(✔)
- 即针对字符串型数据,
简单来说就是针对不同的内容
#{}
可能会对参数添加''
(字符串型), 也可能是直接替换(数值型)
${}
直接替换为对应参数的内容
SQL 注入
既然 # 对于字符串型数据会添加''
, 那使用 $ 时对于字符串型手动添加''
不可以么
答
使用 $ 是不安全的, 可能会引起 SQL 注入问题
什么是 SQL 注入?
举个栗子🌰
userinfo 表中的内容🍂
定义一个 login 方法🍂
UserInfo login(@Param("username") String username, @Param("password") String password);
实现该方法🍂
(对于字符串型手动添加''
)
<select id="login" resultType="com.example.demo.entity.UserInfo">
select * from userinfo where username = '${username}' and password = '${password}'
</select>
单元测试查看结果🍂
登录成功
修改密码后查看结果🍂
登录失败
利用 SQL 注入登录(使用错误的密码)🍂
登录成功
注意此时的密码
String password = "' or 1 = '1";
最终生成的 SQL 语句
select * from userinfo where username = 'admin' and password = '' or 1 = '1'
划分为 2 部分(优先级 → and
> or
)
username = 'admin' and password = ''
/ 1 = '1'
因此可以成功登录
🌸上述即为 SQL 注入, 因此推荐使用 # 而不是 $🌸
🔎like 查询
like 查询 → 此处使用的数据表为 userinfo(用户) 表
数据表中的数据
根据 username 查询用户信息
在 Interface 中定义方法🍂
/\*\*
\* Like 查询
\* @author bibubibu
\* @date 2023/7/3
\*/
List<UserInfo> getListByName(@Param("username") String username);
在 xml 中实现方法🍂
<select id="getListByName" resultType="com.example.demo.entity.UserInfo">
select * from userinfo where username like '%#{username}%'
</select>
单元测试验证效果🍂
Error → 这是因为使用 # 处理字符串类型时, SQL 语句会添加''
即上述 SQL 语句变为select * from userinfo where username like '%'a'%'
句
select * from userinfo where username = 'admin' and password = '' or 1 = '1'
划分为 2 部分(优先级 → and
> or
)
username = 'admin' and password = ''
/ 1 = '1'
因此可以成功登录
🌸上述即为 SQL 注入, 因此推荐使用 # 而不是 $🌸
🔎like 查询
like 查询 → 此处使用的数据表为 userinfo(用户) 表
数据表中的数据
根据 username 查询用户信息
在 Interface 中定义方法🍂
/\*\*
\* Like 查询
\* @author bibubibu
\* @date 2023/7/3
\*/
List<UserInfo> getListByName(@Param("username") String username);
在 xml 中实现方法🍂
<select id="getListByName" resultType="com.example.demo.entity.UserInfo">
select * from userinfo where username like '%#{username}%'
</select>
单元测试验证效果🍂
Error → 这是因为使用 # 处理字符串类型时, SQL 语句会添加''
即上述 SQL 语句变为select * from userinfo where username like '%'a'%'