SpringBoot learning notes 02

1. Details of springboot

1.1 environment switching

explain:
Software generally runs in many different environments There is a dev environment in the development phase Testing will be carried out after development There will be a test environment The final project is deployed to the user's service There will be a production environment
 
Question:
If you need to modify the configuration file manually every time you switch the environment, it will cause a lot of inconvenience
 
Requirements:
How to simplify the impact of environment switching. = = > Multi environment editing with yml

1) Multi environment editing:

requirement:
If multi environment testing is adopted, the data items in each environment should be consistent Otherwise, missing may lead to abnormal project startup (that is to say, if I have a name for the same person, you should also have a name)

Multi environment configuration:
Key syntax "- environment segmentation

1.1) definition of environmental Name:

spring:
  config:
    activate:
      on-profile: Environment name (custom)

1.2) use default environment:

#Default environment options
spring:
  profiles:
    #Default environment configuration name
    active: Environment name (customized)

1.3) environmental test:

#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.YMl configuration file has 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 value of dept attribute supports UTF-8 by default
dept:
  id: 100
  name: Finance Department

#Environmental 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 value of dept attribute supports UTF-8 by default
dept:
  id: 100
  name: Group headquarters

1.2 hot deployment

1.2.1 problem introduction:

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

1.2.2 instructions for use:

1) Import jar package

<!--Support hot deployment -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-devtools</artifactId>
</dependency>

2) Configuring the IDEA environment

2.1) call the configuration menu:
Key combination (ctrl + shift + alt + / or ctrl + alt + a)

2.2) check automatic compilation:

2.3) start automatic compilation of IDEA

2.4) test effect:
After modifying the source code, press ctrl+s to see whether the console is restarted

2. Spring boot integrates Mybatis

2.1 preparation

2.1.1 installing SqlYog

You can view the corresponding installation tutorial by yourself

2.1.2 SqlYog import database

1). New database

2). Import data

2.2 spring boot integration Mybatis project

2.2.1 create project:

2.2.2 import POM XML file

<?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 Start item import jar Package can complete automatic configuration
			There is no link to the database for the time being
		-->
		<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.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>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>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

Supplementary notes:
There is a scope attribute in dependency. What is its function?
 
Indicates the scope of the jar package of MAVEN:
1.test scope means that the test scope is valid, and this dependency will not be used during compilation and packaging
2.compile scope refers to the valid scope of compilation, and dependencies will be stored during compilation and packaging
3.provided dependency is valid during compilation and testing. The war package generated last will not be added. For example:
Servlet API, because the servlet API Tomcat server already exists, it will conflict if it is repackaged
4.runtime is dependent at runtime and not at compile time
The default dependency range is compile

2.2.3 JDBC data source configuration

#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
    password: root

About Parameter Description:
1. Servertimezone = GMT% 2B8% 2B "+" the new version of the driver requires that the time zone be configured
2. & useunicode = true & characterencoding = utf8 using Unicode encoding requires character UTF-8 encoding
3. & autoreconnect = true whether to reconnect automatically
4. & allowmultiqueries = true whether to allow batch operations to execute multiple sql at the same time!

2.2.4 Mybatis related configuration

#Spring boot integrates Mybatis configuration
mybatis:
  #Define alias package
  #In order to omit a lot of repetitive code later, for example, the return value type of resulttype can be abbreviated
  type-aliases-package: com.jt.pojo
  mapper-locations: classpath:/mybatis/mappers/*.xml
  #Turn on hump mapping
  configuration:
    map-underscore-to-camel-case: true

Supplement:
1. Since SpringBoot has the out of the box principle, we need to configure relevant information
2. Define the path name of the alias package: type aliases package: class
3. Location of mapping file: mapper locations
Note: in resource, click "." It represents points, not grades! If you want to rank, use the slash "/"

2.2.5 edit Mapper interface / mapping file


1) Mapping 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">

    <update id="update">
        update demo_user set name = #{newName} where name =#{oldName} and
                                                     sex = #{sex}
    </update>
    <!--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 the third-party object

        problem:Can the invariant package path be optimized???
        Solution: Define 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 when developing,program
    There is no difference in execution,So it can be mixed
    <insert id="">
    </insert>-->
    <insert id="addUser">
        insert into demo_user(id,name,age,sex) values (#{id},#{name},#{age},#{sex})
    </insert>
</mapper>

Keywords: Java Spring Boot

Added by conquest on Sun, 27 Feb 2022 05:48:55 +0200