分布式Day02

1. SpringBoot 高级用法
1.1 关于POM.xml文件说明
1.1.1 关于POM文件说明
<build>
        <!--
            插件的作用
            maven插件: 当程序在打包编译等maven操作时有效.
        -->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 


1.2 关于SpringBoot主启动项说明


是否自动配置相关信息


2 SpringBoot高级用法
2.1 关于配置文件复习
2.1.1 properties文件


2.1.2 YML配置文件说明


2.2 为属性赋值
2.2.1 YML为属性赋值
1).编辑YML配置文件

2).实现数据自动注入


2.2.2 利用properties文件为属性赋值
说明:由于YML一般都是系统级别的属性配置文件,一般将业务配置放到指定的properties文件中.

1).编辑pro配置文件

#定义测试redis节点信息
redis.host2=redis的IP地址
redis.port2=6379
1
2
3
2).编辑RedisController2

package com.jt.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
//当程序启动时加载指定的资源
@PropertySource(value = "classpath:/properties/redis.properties",encoding = "utf-8")
public class RedisController2 {
    
    @Value("${redis.host2}")
    private String host;
    @Value("${redis.port2}")
    private int port;

    @RequestMapping("/getNode2")
    public String getNode(){

        return host + "|" +port;
    }
}


2.2 热部署
2.2.1 添加jar包
        <!--支持热部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
1
2
3
4
5
2.2.2 配置热部署
组合键: ctrl + alt + shift + /


2.3 切换环境配置
2.3.1 业务需求
由于开发中可能会遇到不同的环境问题.可能随时修改配置信息. 需要准备一套灵活的机制进行修改.

2.3.2 编辑YML配置文件
#设定默认的环境
spring:
  profiles:
    active : dev


---
#语法:   1. key:(空格)value
#       2.YML文件有层级关系 注意缩进
#       3.YML文件中默认utf-8格式编码
#标识配置信息   新版本的写法
spring:
  config:
    activate:
      on-profile: dev

server:
  port: 8090
# 利用YML方式赋值
redis:
  host: 127.0.0.1
  port: 6379

--- #实现配置文件拆分   旧版本的写法
spring:
  profiles: prod  #为环境起名

server:
  port: 8080

redis:
  host: 10.0.0.1
  port: 7000


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2.4 SpringBoot整合Mybatis
2.4.1 创建项目


2.4.2 添加jar包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.jt</groupId>
    <artifactId>springboot_demo2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot_demo2</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
2.4.3 数据源相关配置
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/jtdb?    
                serverTimezone=GMT%2B8    配置数据源时区    %2B +
                &useUnicode=true&characterEncoding=utf8        使用utf-8编码格式
                &autoReconnect=true                                             是否重新链接
                &allowMultiQueries=true                                       是否允许批量操作
    username: root
    password: root
1
2
3
4
5
6
7
8
9
2.4.4 编辑User对象
面试题: 如果项目需要发布到Linux系统中,问Linux系统内部是否需要安装LOMBOK插件?
答: 不需要
原因: LOMBOK在编译器有效的. 由xx.java文件 动态生成xxx.class文件时 会动态的添加get/set方法


2.4.5 编辑YML配置
server:
  port: 8090
  servlet:
    context-path: /
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
    username: root
    password: root

#springboot整合mybatis
mybatis:
  #定义别名包
  type-aliases-package: com.jt.pojo
  #加载Mapper映射文件
  mapper-locations: classpath:/mybatis/mappers/*.xml
  #开启驼峰映射
  configuration:
    map-underscore-to-camel-case: true

  
  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2.4.6 编辑文件解析
<?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="com.jt.mapper.UserMapper">
    <!--
        1.通过别名包定义用户的类型,
            原理:当返回值结果resultType封装对象时,
                自动的拼接别名包路径 com.jt.pojo.User

        2.开启驼峰规则的说明
            字段:   user_id,user_name,user_age
            属性:    userId ,userName ,userAge

        设定:开启驼峰映射规则
              user_id 去除多余的_  之后首字母大写  userId

        注意事项: 如果开启了驼峰映射规则,则按照要求实现.
    -->
    <select id="findAll" resultType="User">
        select * from user
    </select>
</mapper>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2.4.7 完成mapper测试


3.MybatisPlus
3.1 ORM思想
对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序设计技术,用于实现面向对象编程语言里不同类型系统的数据之间的转换。从效果上说,它其实是创建了一个可在编程语言里使用的“虚拟对象数据库”。如今已有很多免费和付费的ORM产品,而有些程序员更倾向于创建自己的ORM工具。

实质: 是以对象的方式操作数据库.

3.2 MybatisPlus
3.2.1 MP介绍
MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

3.2.2 特性


3.3 MybatisPlus 入门案例
3.3.1 导入jar包
    <!--
            spring整合mybatisplus
            只保留mybatisplus的包,删除mybatis的包
        -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>
1
2
3
4
5
6
7
8
9
3.3.2 编辑POJO对象


3.3.3 继承公共接口


3.3.4 编辑YML配置文件


3.3.5 测试案例


3.3.6 MP实现的原理
问题: 如何实现以对象的方式操作数据库!!!
步骤1: 确定数据库中对象与关系表的映射关系 名称/属性 能否利用注解的方式实现
步骤2: 以统一的方式定义Mapper接口,方便用户的调用.
步骤3: 对象如何转化为Sql语句的环节.
入库操作: userMapper.insert(user);
Sql语句: insert into 表名(字段1,字段2,字段3…) values(值1,值2,值3…)
最终将sql交给Mybatis执行.


3.3.7 添加日志


3.3.8 mybatisPlus 案例测试
package com.jt;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.management.Query;
import java.util.Arrays;
import java.util.List;

@SpringBootTest
class SpringbootDemo2ApplicationTests {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void test01(){
        List<User> userList = userMapper.findAll();
        System.out.println(userList);
    }


    @Test
    public void testInsert(){
        User user = new User();
        user.setName("鲁班七号").setAge(3).setSex("男");
        userMapper.insert(user);
    }

    /**
     * 测试1:  查询id=5的数据
     * 测试2:  根据name="唐僧"查询数据
     */
    @Test
    public void testSelect(){
        User user = userMapper.selectById(5);
        System.out.println(user);
        //查询所有数据
        List<User> userList = userMapper.selectList(null);
        System.out.println(userList);

        //3.查询name="唐僧"数据 where name ="xxx"
        //常见关系运算符 = eq , > gt ,< lt, >= ge ,<= le
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", "唐僧");
        List<User> userList2 = userMapper.selectList(queryWrapper); //where条件构造器
        System.out.println(userList2);
    }

    /**
     * 查询: id= 1,3,5,6的数据
     * Sql:  select xxxxx where id in (1,3,5,6)
     */
    @Test
    public void testSelect02(){
        Integer[] ids = {1,3,5,6}; //模拟前端传递的数据
        //一般需要将数组.转化为List集合
        List<Integer> idList = Arrays.asList(ids);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", idList);
        System.out.println(userMapper.selectList(queryWrapper));
        //方式2: 直接批量查询
        System.out.println(userMapper.selectBatchIds(idList));
    }

    /**
     * 条件: 查询名字中包含'精'字的数据
     * sql:  select ..... where name like '%精%'
     *                                        '王%'
     */
    @Test
    public void testSelect03(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", "精");
        System.out.println(
                userMapper.selectList(queryWrapper));

        //2.模糊查询2
        QueryWrapper<User> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.likeRight("name", "王");
        System.out.println(userMapper.selectList(queryWrapper2));

    }

    /**
     *
     * 将数据按照age降序排列,如果年龄相同 按照ID降序排列
     */
    @Test
    public void testSelect04(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("age","id");
        System.out.println(userMapper.selectList(queryWrapper));
    }

    /**
     * 查询name中包含"君"字 or sex为女性 ,按照Id降序排列
     * sql: where name like "%君%" and xxxx
     */
    @Test
    public void testSelect05(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", "君")
                    .or()    //默认是and 如果需要调用or
                    .eq("sex", "女")
                    .orderByDesc("id");
        System.out.println(userMapper.selectList(queryWrapper));

    }

    /**
     * 将name="测试案例" 修改为 "测试环境"
     * 参数说明: entity 需要改为的数据对象
     *              updateWrapper 更新条件构造器
     * 特点: 将对象中不为null的属性当做set条件     */
    @Test
    public void testUpdate(){
        User user = new User();
        user.setName("测试环境");
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("name", "测试案例");
        userMapper.update(user,updateWrapper);
    }

}

————————————————
版权声明:本文为CSDN博主「闪耀太阳」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_16804847/article/details/111572249

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值