Javaweb篇

JavaWeb

SpringBoot

​ IOC(Inversion Of Control),控制反转。

​ DI(Dependecy Injection),依赖注入。@Autowired

​ Bean对象,为容器IOC创建的对象。

​ Bean的声明:@Component,@Controller——Controller层,@Service——services层,@Repository——DAO层

​ //当出现上述注解,会自动创建Bean对象。

依赖注入冲突问题

如果遇到多个Bean容器无法自动注解,使用

1
2
@Autowired  //该注解是由spring提供的,默认用类型注入
@Qualifier("Bean名称")

或使用注解

1
@Resource(name = "Bean名称") //该注解是由JDK提供的,默认用名称注入

使用

1
@Primary //主要

依赖注入常见方法

1
2
3
4
@Autowired
private EmployeeMapper employeeMapper;
//employeeMapper是一个持久层的接口,在employeeMapper的注解@Mapper实现控制反转,通过@Autowired实现依赖注入。
//这里相当于实现创建抽象类的实体类

数据库(Database)

MySQL数据模型

二维表。属于关系型数据库。

连接远程服务器数据库

1
mysql -hIP地址 -P端口 -u用户名 -p密码

SQL分类

DDL(Data Definition Language)数据定义语言,定义数据对象(数据库、表、字段)

DML(Data Manipulation Language) 数据操作语言,如增删改

DQL(Data Query Language)数据查询语言

DCL(Data Control Language)数据控制语言,如增添用户、设置权限

DDL语法

数据库操作

添加数据库
1
create databse if no exists 数据库名;
删除数据库
1
drop database if not exists 数据库名;
查询当前数据库
1
select databse();
使用数据库
1
use 数据库名;

表结构操作

表操作

image-20250506211607727

1
2
3
4
5
6
7
8
//创建表
create table employee(
id int comment 'ID,唯一标识',
username varchar(20) comment '用户名',
password varchar(20) comment '用户名'
) comment '用户表';
//修改表名
rename table tb_emp to emp;
表约束

非空约束 not null

唯一约束 unique

主键约束 primary key

默认约束 default

1
2
3
4
5
create table employee(
id int primary key comment 'ID,唯一标识',
username varchar(20) not null unique comment '用户名',
password varchar(20) not null comment'用户名'
) comment '用户表';
字段操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//添加字段
alter table tb_emp add qq varchar(11) comment 'QQ';
//改变字段类型
alter table tb_emp modify qq varchar(13) comment 'QQ';
//改变字段名称
alter table tb_emp change qq qq_num varchar(13) comment 'QQ';
//删除字段
alter table tb_emp drop column qq_num;

//增加数据
insert into tb_emp (name,female) values ('qq',1);
//更新数据
update tb_emp set name = 'QQ' where id = 1;
//删除数据
delete from tb_emp where name = 'qq';

参数占位符

image-20250511150718383

Mybatis

自动映射

Java命名,遵循小驼峰命名:例如,mySql, myName

数据库SQL命名,遵循下划线命名:例如,my_sql, my_name

使用自动映射

image-20250511153240822

动态SQL

常见标签

where标签
1
2
//<where>
<where> <if test="name != NULL"> and name = #{name} </if></where> //<where>自动识别删除字段前的and,
set标签
1
2
//<set> 
<set><if test="name != NULL"> name = #{name},</if></set> //自动删除识别,
foreach标签
1
2
3
4
5
//<foreach collection="ids" item >
<foreach collection="ids" item="id" separator="," open="(" close=")" >
#{id}
//collection集合名称,item集合遍历出的元素,separator隔离符,open遍历前拼接片段,close遍历后拼接片段
</foreach>
sql、include标签
1
2
3
4
5
6
//<sql> 提高代码复用性
<sql id ="commentSelect">
select id, name, gender from
<sql>
//<include>调用
<include refid="commentSelect"><>

工程学习

pojo

封装实体类的时候,封装数据库int类型时,要用封装类integer

因为数据库表中可能出现NULL值,integer类型能出现NULL值对应

工程注解类

路径注解:@Pathvariable

封装json类:@RequestBody

设置默认值:@RequestParam(defaultValue=”1”)

PageHelper分页查询

1
2
3
4
5
PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());
Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);
long total = page.getTotal();
List<Employee> records = page.getResult();
return new PageResult(total, records);

前端上传

MultipartFile

UUID(通用唯一识别码)

image-20250513215602481

传输大小限制multipart


Javaweb篇
http://example.com/2025/08/04/JavaWeb学习指南 (2)/
作者
Luogic
发布于
2025年8月4日
许可协议