0
点赞
收藏
分享

微信扫一扫

王庆华笔记

君心浅语 2022-03-30 阅读 59
java

除了(略)的内容是需要看一下,其他内容需要记住,记不住也要可以通过文档迅速查询到

IDE

快捷键
ctrl + 点击方法 , 追溯方法上级or进入方法
ctrl + alt + 点击方法 , 查找这个方法的实现

spring-mvc

设置请求路径
@RequestMapping, 可放在类或方法上
@GetMapping
@PostMapping

设置参数
@RequestBody:用来接收前端传递给后端的json字符串中的数据,最多只能有一个。
@RequestParam :从请求路径中获取参数,可以有多个。
其他详见:SpringMVC后台接收参数的几种方式

swagger

<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.7.0</version>
</dependency>
<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.7.0</version>
</dependency>
@Api(tags = "类描述")
@ApiOperation("方法描述")
@ApiModel("实体描述")
@ApiModelProperty("实体字段描述")

mybatis-plus

-----------------------------------------配置

<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.1.1</version>
</dependency>
<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
</dependency>
<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
			<version>2.3.28</version>
			<scope>compile</scope>
</dependency>
<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-generator</artifactId>
			<version>3.4.0</version>
</dependency>
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: 123456
mybatis-plus:
  mapper-locations: classpath*:xml/**/*.xml

在配置类或main方法入口添加@MapperScan注解,指定MyBatis扫描的包路径

自动生成代码工具:mybatis-plus-generator


在这里插入图片描述

使用-----------------------------------------
文档:https://mp.baomidou.com/guide/page.html

实体注解 -------------------------------------------:
加在实体类上面的注解,如下:
@TableName(“表名”) : 驼峰命名方式,该值可无。
加在字段上面的注解,如下:
@TableId :标识主键

@TableField
1 如果开启了驼峰映射,那么就不必在@TableField写value值了。
2 @TableField(exist = false) ,表示该字段在数据库中不存在 ,所以不会插入数据库中,以及使用 transient 、 static 修饰属性也不会插入数据库中

@TableLogic : 标识软删除|逻辑删除字段

查询 -------------------------------------------:
QueryWrapper QueryWrapper = new QueryWrapper<>();
在这里插入图片描述

分页 -------------------------------------------:

     @Bean
     public PaginationInterceptor paginationInterceptor() {
          PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
          return paginationInterceptor;
     }
	@ApiOperation("分页查询")
    @GetMapping("page")
    public R page(@RequestParam long current, @RequestParam long size){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        IPage<Job> ipage= iJobService.page(new Page<>(current , size) ,queryWrapper);
        System.out.println("总页数: "+ipage.getPages());
        System.out.println("总记录数: "+ipage.getTotal());
        return R.ok(ipage);
    }

//  以上是单表的分页查询,如果需要级联N个表的分页查询需要手动写dao
IPage<Entity> selectPage(IPage<Entity> page, @Param(Constants.WRAPPER) Wrapper<Entity> queryWrapper);
<select id="selectPage" resultType="xx.xx.xx.Entity">
		SELECT
			me.*
		FROM
			`tabe_name` AS me
				LEFT JOIN rn_member_dept AS deptr ON me.id = deptr.rn_member_id
		WHERE
			me.is_deleted = 0
		  AND deptr.is_deleted = 0
		  AND ${ew.sqlSegment}
</select>

软删除 | 逻辑删除 -------------------------------------------:
与之对应的是硬删除|物理删除

@TableField(value = "is_deleted")
@TableLogic
private Boolean deleted;

没有@TableLogic注解调用deleteById/removeById,直接删除数据, 加@TableLogic 会执行更新 is_deleted字段的操作。

xml 元素-------------------------------------------:
resultMap:
autoMapping = “true” : 开启自动映射,不需要指定别名

<resultMap id="resultMapId" type="xx.xx.xx.Class" autoMapping="true">
	 <association property="dept" javaType="net.newcapec.cloud.ucenter.entity.CustomerDeptEntity"
                     columnPrefix="dept_" autoMapping="true">
            <id column="id" property="id"></id>
    </association>
</resultMap>

resultType:
需要SQL中查询字段指定别名,别名需与resultType指定的实体中属性名相同

 <foreach collection="@paramListName" item="customItemName" index="index" open="(" close=")" separator=",">
     #{customItemName}
 </foreach>
<if test="params.customerCode != null and params.customerCode != ''">
</if>

mysql

SQL查询练习:https://www.cnblogs.com/zero-vic/p/14252232.html

表-新增:CREATE TABLE table_name (column_name column_type);
表-修改:ALTER TABLE table_name ;
表-删除:DROP TABLE table_name ;
新增-记录:
INSERT INTO table_name ( field1, field2,…fieldN )
VALUES
( value1, value2,…valueN ), ( value1, value2,…valueN )…;
注意:VALUES 是多条 , VALUE 是一条
删除:DELETE FROM table_name [WHERE Clause]
更新:UPDATE table_name SET field1=new-value1, field2=new-value2 [WHERE Clause]

查询:
关键字 : LIKE| UNION | DISTINCT
NULL 值处理:IS NULL | IS NOT NULL

查询序列号:
原理:先设置@i = 0 , 然后在@i ++
select (@i:=@i+1) as i,a.* from table_table a , (select @i:=0) as it;

排序 : ORDER BY
1 设定多个字段来排序
2 ASC(升序,默认) , DESC (降序)
3 mysql在不给定order by条件的时候, 可能会根据查询使用的不同索引,得到不同的数据顺序。

事务
1、用 BEGIN, ROLLBACK, COMMIT来实现
BEGIN 开始一个事务
ROLLBACK 事务回滚
COMMIT 事务确认
2、直接用 SET 来改变 MySQL 的自动提交模式:
SET AUTOCOMMIT=0 禁止自动提交
SET AUTOCOMMIT=1 开启自动提交

锁:
lock in share mode

for update

临时表:
临时表只在当前连接可见,当关闭连接时,Mysql会自动删除表并释放所有空间。
CREATE TEMPORARY TABLE

注解(Annotation)

元注解 :修饰注解的注解,通常用在定义注解的上。
@Target:注解作用的位置
@Retention:注解的生命周期
(略)@Documented:注解是否应当被包含在 JavaDoc (api)文档中
(略)@Inherited:是否允许子类继承该注解
(略)@Target:指定注解的使用范围(指定该注解使用在哪里)
示例:@Target({ElementType.xxx}):xxx 取值如下
CONSTRUCTOR:用于描述构造器
FIELD:用于描述域
LOCAL_VARIABLE:用于描述局部变量
METHOD:用于描述方法
PACKAGE:用于描述包
PARAMETER:用于描述参数
TYPE:用于描述类、接口(包括注解类型) 或enum声明
(略)@Retention 用于指明当前注解的生命周期
示例:@Retention(value = RetentionPolicy.RUNTIME)
RetentionPolicy.SOURCE:当前注解编译期可见,不会写入 class 文件
RetentionPolicy.CLASS:类加载阶段丢弃,会写入 class 文件
RetentionPolicy.RUNTIME:永久保存,可以反射获取

自定义注解
1 关键字:@interface 2 自定义注解需要用到元注解
示例:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLog {
	/**
	 * 调用名称
	 * @return
	 */
	String value();
 
	String desc() default "日志描述";
}

注解使用,拿如上示例展示
@SysLog(属性名=“属性值”)
1 注解用default 给属性一个默认值,那么使用注解时,可以不给属性赋值操作,如上desc,如果不传值,就会读取默认值“日志描述”
2 如果注解定义了属性名为value,使用时可以省去属性名
如:@SysLog(“属性值”) = @SysLog(value = “属性值”)

读取注解
Class.getAnnotations() 获取所有的注解,包括自己声明的以及继承的
Class.getAnnotation(Class< A > annotationClass) 获取指定的注解,该注解可以是自己声明的,也可以是继承的
Class.getDeclaredAnnotations() 获取自己声明的注解

枚举(enum)

示例:

public enum RnMemberRoleEnum {

    student("学生",1),
    teacher("教师",2),
    patriarch("家长",3),
    support("后勤人员",4),
    temp("临时人员",5)
    ;

    private String  name;
    private Integer  value;

    RnMemberRoleEnum(String name, Integer value) {
        this.name = name;
        this.value = value;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }
}

lombok

安装插件在这里插入图片描述

<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
</dependency>

注解:@Data:自动生成get、set以及构造方法,重写equals()和hashCode()

单元测试

@RunWith(SpringRunner.class)
 private RestTemplate restTemplate = new RestTemplate();
 restTemplate.postForObject();

Git

上传本地项目至远程仓库
(1)远程建立仓库
(2)进入本地项目根目录下
git init // 初始化项目为git仓库
git add . // 添加本地所有文件到git仓库
git commit -m “first commit”
git remote add origin https://gitee.com/wang-jing-9264/xxx.git
git push -u origin master
提交代码
git add .
git commit -m “first commit”
git pull origin master
git push origin master

redis

<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

使用:

   @Autowired
    private RedisTemplate<String, String> redisTemplate;
//opsForValue 操作字符串 类似于String
redisTemplate.opsForValue();
//opsForList 操作List 类似于List
redisTemplate.opsForList();
//opsForSet 操作Set 类似于Set
redisTemplate.opsForSet();
//opsForHash 操作hash 类似于hash
redisTemplate.opsForHash();
//opsForHyperLogLog 操作HyperLogLog 类似于HyperLogLog
redisTemplate.opsForHyperLogLog();
//opsForZSet 操作ZSet 类似于ZSet
redisTemplate.opsForZSet();

redis常用操作

spring-cloud-stream

windows10环境下的RabbitMQ安装步骤 : https://www.cnblogs.com/saryli/p/9729591.html

rabbit

<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>

rocketmq

  <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rocketmq</artifactId>
 </dependency>
public interface StreamClient {

    String  mq = "saveOrderInput";

    @Input(mq)
    SubscribableChannel saveOrderInput();

    @Output("saveOrderOut")
    SubscribableChannel saveOrderOut();

}

生产消息

   @Autowired
    private StreamClient streamClient;
 streamClient.saveOrderOut().send(MessageBuilder.withPayload("mq测试消息").build());

消费消息

  @StreamListener(StreamClient.saveOrderInput)
  public void saveOrderInput(Object payload) {
       System.out.println(payload);
   }

XXL-JOB

搭建教程:https://www.cnblogs.com/ysocean/p/10541151.html

java8

项目实战

入口:https://blog.csdn.net/jeason13920089655/article/details/105963527/

演示系统:mall (电商系统)
文档:http://www.macrozheng.com/#/database/mall_pms_01
http://www.macrozheng.com/admin/index.html#/login
admin/macro123

SpEL : 读取yml文件

Spring表达式语言

#{…}和${…}

#{…} 用于执行SpEl表达式,并将内容赋值给属性

${…} 主要用于加载外部属性文件中的值

#{…} 和KaTeX parse error: Expected 'EOF', got '#' at position 16: {…} 可以混合使用,但是必须#̲{}外面,{}在里面

示例:

@Value("#{’${server.name}’.split(’,’)}")

举报

相关推荐

0 条评论