深夜问答(开发)1—Web后端开发、Java等

项目中使用到的Java

IO流

java、resource最终编译都在class内:通过字节码对象获取加载器

//方法2:由于java和resouerces最终编译后都放在class中,通过字节码对象获取加载器获取
InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");

this.getClass():拿到当前类的字节码对象
.getClassLoader():拿到类加载器,能访问编译后的classes目录
.getResourceAsStream("user.txt"):从 classes目录下读取user.txt文件,返回输入流

List

List转流获取内容再转回

List<User> userList = lines.stream().map(line->{//User是自己创建的类
String[] parts = line.split(",");//逗号切分
String xxx = parts[0];
切分获取
}).toList();//转成List然后被存下来

Web后端开发

开发思想

三层架构:Service写Controller调用的方法,Mapper层写Service调用方法

高内聚低耦合:(类功能尽量单一,提升维护,防错等)

Controller(接收请求+响应数据) + Service(逻辑处理) + Dao(数据访问)

Service需要接口 + 实现类、Mapper + Mybatis框架会自动生成实现

@Override:签名防错(防止重写类的名字写错)

容器解耦(Bean对象):Bean、IOC + DI(从new对象变为Spring管理)、自定义Bean名

注意事项:三层架构里依赖是单向的,即Controller注入Service,Service注入Mapper

Bean对象:在IOC容器中创建、管理的对象

1. IOC(控制反转):把对象的创建权从你自己 new 交给 Spring 容器管理

@Component:代表类交由IOC容器管理创建(基础注解)、非以下三类即基础Bean
衍生:

  • @Controller: 标注在控制层类上
  • @Service: 标注在业务层类上
  • @Repository: 标注在数据访问层上

自定义Bean名:作用与方法
此时Bean的名字即myUserService,不加括号,默认名字是类名首字母小写:userServiceImpl
注入时可以用@Qualifier("myUserService")指定它

@Service("myUserService")
public class UserServiceImpl implements UserService { }

2. DI(依赖注入):@Autowird 自动补全依赖(多个则需要指定)

@Autowired:依赖注入

多个Bean处理方式:

  • @Resource(name = "xxx")
  • @Qualifier("xxx")+ @Autowired

组件扫描:被组件扫描(@ComponentScan)扫描:IOC四大注解才能生效,修改扫描范围

@ComponentScan以包含在启动类@SpringBootApplication中:但默认为启动包及其子包范围有效

修改扫描范围

@SpringBootApplication
@ComponentScan(basePackages = "com.example")

对于测试:同包同名

@Test

线程池、连接池

Mybatis:仅需定义Mapper接口,自动实现、封装、回收

替代传统的JDBC,直接使用即可

依赖 + 基础使用

保持让类中属性名 与 表中字段名保持一致,这样会自动封装

  1. 引入依赖:Mybatis Framework、MySQL Driver依赖
  2. 配置:设置使用的数据库:在resource/application.properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
spring.datasource.username=用户名
spring.datasource.password=密码

SQL补全提示 + 日志 + 连接池遗漏处理

对于SQL补全: 要连通数据库 = 配置语言 + IDE连接数据库
image
image

Mybatis日志输出mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

连接池遗漏处理
引入依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.19</version>
</dependency>

yml:spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

Mybatis增删改查 + 预编译 + 形参起名(多参数时)

预编译:参数值传递 #{xxx}

  • 增:@Insert("")
  • 删:@Delete("")
  • 改:@Update("")
  • 查:@Select("")

使用对应@注解后方下方方法直接传递参数即可

@增删改查("xx  id = #{id}")
xxx (xx id);

XML映射SQL:resource/下

规则:

  1. 映射文件 = Mapper接口名 + 同包同名

  2. 映射文件namespace属性 = Mapper全限定名

  3. 映射文件中SQL语句id = Mapper方法名

e.g.:
image

基础结构:映射文件位置、声明版本等
映射文件位置:mybatis.mapper-locations=classpath:mapper/*.xml

基础结构

此即为基础结构
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xxx.xxx.xxx.xxx(与全限定名保持一致)">
内容
</mapper>

增删改查对应标签

e.g.全结构方式:
<select id = "findAll(方法名)" resultType = "xxx.xxx.User(返回类型的全类名)">
类型:
<update>
<delete>
<insert>

使用MyBatisX

点击小鸟:快速切换定位在接口/配置文件之间

使用yml配置文件:层级配置

通过缩进确定层级(禁止Tab)

image

好用的工具

使用lombok:提供Data、get、set、@Slf4j(日志)

@Data:getter/setter/toString/equals/hashCode
@NoArgsConstructor + @AllArgsConstructor:有参无参要一起使用
@Slf4j:日志对象
@Getter/@Setter:单独生成
@ToString:生成toString

Controller层

url传递

  1. Http接口:HttpSerletRequest + 使用api
    @RequestMapping():接收前端请求根路径

  2. Spring提供的响应对象:ResponseEntity + api
    用这个对象当作类的类型,然后使用api即可:

响应状态码:.status();响应头:.header();响应体:.body()

声明请求类@RestController返回json数据

@RestController = @Controller + @ResponseBody()

@RestController//声明为请求类
类 {
    @RequestMapping()//路径
    方法{
	    return xxx(json格式) 
	}
}

@

起步依赖的设置:去官网查询对应的开发即可

posted @ 2026-09-05 01:06  TBeauty  阅读(4)  评论(0)    收藏  举报