. SpringBoot usage
Environment switching
Business requirements
Software generally runs in many different environments The development phase has a dev environment Testing will be carried out after development There will be a test environment The final project is deployed to the user's service production environment
Problem: if you need to manually modify the configuration file every time you switch the environment, it will cause a lot of inconvenience
Requirements: if the impact of environment switching can be simplified
Multi environment editing
Requirement: if multi environment test is adopted, the data items (configuration attributes) in each environment shall be consistent Otherwise, missing may lead to abnormal project startup
Multi environment configuration: key syntax - "environment segmentation"
Define environment name:
spring: config: activate: on-profile: prod #The name of the environment is written here
Default environment name:
#Default environment options spring: profiles: #Default environment configuration name active: test #Choose which environment to use
All configurations:
#Default environment options spring: profiles: #Default environment configuration name active: test #Environment segmentation --- #YML file syntax ## 1.YML data structure k-v structure ## 2.k and v need to be connected by "space" ## 3. The YML configuration file has a parent-child relationship, so pay attention to the position of indented items spring: config: activate: on-profile: prod server: port: 80 #configure port servlet: #web project publishing path context-path: / #/Represents the root directory #The YML file that defines the dept attribute value supports UTF-8 by default dept: id: 100 name: Finance Department #Environment split line --- # Each environment should have its own name spring: config: activate: on-profile: test server: port: 8080 #configure port servlet: #web project publishing path context-path: / #/Represents the root directory #The YML file that defines the dept attribute value supports UTF-8 by default dept: id: 100 name: Group headquarters
Hot deployment
Requirement description
In the development phase, every time you modify the source code, you have to restart the server before the program can take effect Can the program automatically complete the monitoring and restart the server
Import jar package
<!--Disadvantages of supporting hot deployment:IDEA After hot deployment is configured with special memory, the memory overhead will increase --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency>
Configuring the IDEA environment
Key combination: ctrl + shift + alt + / or ctrl + alt + a
Check auto compile
Start IDEA auto compilation
Configuration of hot deployment environment of mac version of idea
1) : IntelliJ IDEA – Preferences... Enter the Preferences configuration page, or use the shortcut key command +,
2) : press the shift key twice in succession, open the selection box, enter reg, and select "Registry..."
3) : check compiler automake. allow. wher. app. Running, click Close to Close.
4) : select "Edit Configurations..." in the project
Spring boot integrates Mybatis
Import database
Installing SqlYog
1). Unzip the installation package
Run sql file
Complete the cracking according to the cracking code in the data, enter the user name and password, and click the link
Correct presentation
Import database
1). New database
Right click in the blank space to create the database
Right click in the blank space (pour into folder to create table)
Import data
Create a SpringBoot project
Create project
Check the dependent jar package
pom.xml file
be careful ⚠️:
When using this jar package, you must connect to the database, otherwise an error will be reported
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
<?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> <groupId>com.jt</groupId> <artifactId>springboot_demo_2</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot_demo_2</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- spring-boot-starter Startup item When the program resolves to the jar Package time,Will follow jar Configuration instantiation object in package Including configuration of data sources. Load data source......When using this jar The package must be connected to the database, otherwise an error will be reported --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--Introducing database driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <!--Valid only for current project--> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <!--true Should jar Package files are not transferred between parent and child projects.--> <!--<optional>true</optional>--> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--spring integration mybatis-plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!--When a project is packaged lombok Except package.--> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
Configure files to connect to the database
server: port: 8090 #SpringBoot out of the box spring: datasource: url: jdbc:mysql://127.0.0.1:3306/jtadmin?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true username: root #Check whether the password is correct password: root #Spring boot integrates MybatisPlus configuration mybatis-plus: #Define alias package type-aliases-package: com.jt.pojo mapper-locations: classpath:/mappers/*.xml #Turn on hump mapping configuration: map-underscore-to-camel-case: true #Add MP log print executed sql logging: level: com.jt.mapper: debug
Executing sql statements is a configuration file
<?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"> <!--namespace 1.Mapper.xml Unique identification of the profile 2.Need and Mapper Interface consistency. UserMapper.findAll(); Mapping by method Sql sentence!!! --> <mapper namespace="com.jt.mapper.DemoUserMapper"> <!--Edit update operation #An expression writing method in mybatis -- > <update id="updateUser"> update demo_user set name=#{nowName} where name=#{oldName} and sex=#{sex} </update> <!-- Complete the user warehousing operation id="Consistent with method name" sql No need to add at the end;No. in Mysql Execute in database;No problem, But if Oracle During execution, an error must be reported. --> <insert id="insertUser"> insert into demo_user(id,name,age,sex) value(null,#{name},#{age},#{sex}) </insert> <!--The query tag must have a return value resultType :Direct return POJO Data sheet table query of object resultMap: The result encapsulation applicable to association query is generally received by a third-party object problem:Can the invariant package path be optimized??? Solution: Define an alias package explain: resultType="Package name.Class name" resultType="com.jt.pojo.DemoUser" Define alias package type-aliases-package: com.jt.pojo resultType="DemoUser" You can directly return the name of the object Program parsing: First, the path is spliced according to the name of the alias package com.jt.pojo.DemoUser --> <select id="findAll" resultType="DemoUser"> select * from demo_user </select> <!--The label of update operation is for the convenience of programmers during development,program There is no difference in execution,So it can be mixed <insert id=""> </insert>--> </mapper>