Springboot mybatis environment construction

Posted by whit555 on Fri, 18 Feb 2022 17:29:04 +0100

This article is the original article of CSDN blogger "shining sun", which follows the CC 4.0 BY-SA copyright agreement. For reprint, please attach the source link of the original text and this statement.
Original link: https://blog.csdn.net/qq_16804847/article/details/116227307

1.1 environment switching

1.1.1 business requirements

Generally, the software will run in 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 production environment
Problem: if you need to modify the configuration file manually every time you switch the environment, it will cause a lot of inconvenience
Requirements: if the impact of environment switching can be simplified

1.1.2 multi environment editing

Requirement: if multi environment test is adopted, the data items 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

 
  • 1
  • 2
  • 3
  • 4

Default environment name:

#Default environment options
spring:
  profiles:
    #Default environment configuration name
    active: test

 
  • 1
  • 2
  • 3
  • 4
  • 5

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.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 # configuration port
Servlet: #0 web project publishing path
Context path: / # / indicates 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 # configuration port
Servlet: #0 web project publishing path
Context path: / # / indicates 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
  • 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

1.2 hot deployment

1.2.1 requirements 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

1.2.2 import jar package

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

 
  • 1
  • 2
  • 3
  • 4
  • 5

1.2.3 configuring the IDEA environment

Key combination: ctrl + shift + alt + / or ctrl + alt + a

2. Check automatic compilation

3). Start IDEA automatic compilation

2. Spring boot integrates Mybatis

2.1 import database

2.1.1 installing SqlYog

1). Unzip the installation package

2). Run sql file
Complete the cracking according to the cracking code in the data, enter the user name and password and click the link

3). Correct presentation

2.1.2 import database

1). New database

2). Import data

2.2 creating a SpringBoot 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>
<span class="token generics function"><span class="token punctuation">&lt;</span>parent<span class="token punctuation">&gt;</span></span>
	<span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
	<span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>spring<span class="token operator">-</span>boot<span class="token operator">-</span>starter<span class="token operator">-</span>parent<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
	<span class="token generics function"><span class="token punctuation">&lt;</span>version<span class="token punctuation">&gt;</span></span><span class="token number">2.4</span><span class="token number">.5</span><span class="token operator">&lt;</span><span class="token operator">/</span>version<span class="token operator">&gt;</span>
	<span class="token operator">&lt;</span>relativePath<span class="token operator">/</span><span class="token operator">&gt;</span> <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> lookup parent from repository <span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>parent<span class="token operator">&gt;</span>

<span class="token generics function"><span class="token punctuation">&lt;</span>properties<span class="token punctuation">&gt;</span></span>
	<span class="token generics function"><span class="token punctuation">&lt;</span>java<span class="token punctuation">.</span>version<span class="token punctuation">&gt;</span></span><span class="token number">1.8</span><span class="token operator">&lt;</span><span class="token operator">/</span>java<span class="token punctuation">.</span>version<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>properties<span class="token operator">&gt;</span>
<span class="token generics function"><span class="token punctuation">&lt;</span>dependencies<span class="token punctuation">&gt;</span></span>

	<span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>spring<span class="token operator">-</span>boot<span class="token operator">-</span>starter Start item import jar Package can complete automatic configuration
		There is no link to the database for the time being
	<span class="token operator">--</span><span class="token operator">&gt;</span>
	<span class="token generics function"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
		<span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
		<span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>spring<span class="token operator">-</span>boot<span class="token operator">-</span>starter<span class="token operator">-</span>jdbc<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
	<span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>

	<span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>Introducing database driver <span class="token operator">--</span><span class="token operator">&gt;</span>
	<span class="token generics function"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
		<span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>mysql<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
		<span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>mysql<span class="token operator">-</span>connector<span class="token operator">-</span>java<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
		<span class="token generics function"><span class="token punctuation">&lt;</span>scope<span class="token punctuation">&gt;</span></span>runtime<span class="token operator">&lt;</span><span class="token operator">/</span>scope<span class="token operator">&gt;</span>
	<span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>

	<span class="token generics function"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
		<span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
		<span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>spring<span class="token operator">-</span>boot<span class="token operator">-</span>starter<span class="token operator">-</span>web<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
	<span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>
	<span class="token generics function"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
		<span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>org<span class="token punctuation">.</span>mybatis<span class="token punctuation">.</span>spring<span class="token punctuation">.</span>boot<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
		<span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>mybatis<span class="token operator">-</span>spring<span class="token operator">-</span>boot<span class="token operator">-</span>starter<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
		<span class="token generics function"><span class="token punctuation">&lt;</span>version<span class="token punctuation">&gt;</span></span><span class="token number">2.1</span><span class="token number">.4</span><span class="token operator">&lt;</span><span class="token operator">/</span>version<span class="token operator">&gt;</span>
	<span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>

	<span class="token generics function"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
		<span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
		<span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>spring<span class="token operator">-</span>boot<span class="token operator">-</span>devtools<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
		<span class="token generics function"><span class="token punctuation">&lt;</span>scope<span class="token punctuation">&gt;</span></span>runtime<span class="token operator">&lt;</span><span class="token operator">/</span>scope<span class="token operator">&gt;</span>
		<span class="token generics function"><span class="token punctuation">&lt;</span>optional<span class="token punctuation">&gt;</span></span><span class="token boolean">true</span><span class="token operator">&lt;</span><span class="token operator">/</span>optional<span class="token operator">&gt;</span>
	<span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>

	<span class="token generics function"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
		<span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>org<span class="token punctuation">.</span>projectlombok<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
		<span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>lombok<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
		<span class="token generics function"><span class="token punctuation">&lt;</span>optional<span class="token punctuation">&gt;</span></span><span class="token boolean">true</span><span class="token operator">&lt;</span><span class="token operator">/</span>optional<span class="token operator">&gt;</span>
	<span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>
	<span class="token generics function"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
		<span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
		<span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>spring<span class="token operator">-</span>boot<span class="token operator">-</span>starter<span class="token operator">-</span>test<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
		<span class="token generics function"><span class="token punctuation">&lt;</span>scope<span class="token punctuation">&gt;</span></span>test<span class="token operator">&lt;</span><span class="token operator">/</span>scope<span class="token operator">&gt;</span>
	<span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>dependencies<span class="token operator">&gt;</span>

<span class="token generics function"><span class="token punctuation">&lt;</span>build<span class="token punctuation">&gt;</span></span>
	<span class="token generics function"><span class="token punctuation">&lt;</span>plugins<span class="token punctuation">&gt;</span></span>
		<span class="token generics function"><span class="token punctuation">&lt;</span>plugin<span class="token punctuation">&gt;</span></span>
			<span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
			<span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>spring<span class="token operator">-</span>boot<span class="token operator">-</span>maven<span class="token operator">-</span>plugin<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
			<span class="token generics function"><span class="token punctuation">&lt;</span>configuration<span class="token punctuation">&gt;</span></span>
				<span class="token generics function"><span class="token punctuation">&lt;</span>excludes<span class="token punctuation">&gt;</span></span>
					<span class="token generics function"><span class="token punctuation">&lt;</span>exclude<span class="token punctuation">&gt;</span></span>
						<span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>org<span class="token punctuation">.</span>projectlombok<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
						<span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>lombok<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
					<span class="token operator">&lt;</span><span class="token operator">/</span>exclude<span class="token operator">&gt;</span>
				<span class="token operator">&lt;</span><span class="token operator">/</span>excludes<span class="token operator">&gt;</span>
			<span class="token operator">&lt;</span><span class="token operator">/</span>configuration<span class="token operator">&gt;</span>
		<span class="token operator">&lt;</span><span class="token operator">/</span>plugin<span class="token operator">&gt;</span>
	<span class="token operator">&lt;</span><span class="token operator">/</span>plugins<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>build<span class="token operator">&gt;</span>

</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
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85

2.2.3 scope of mavenjar package

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.4 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

 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

About Parameter Description:
1. Servertimezone = GMT% 2B8% 2B "+" the new version of the driver requires that the time zone must 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.5 Mybatis related configuration

#Spring boot integrates Mybatis configuration
mybatis:
  #Define alias package
  type-aliases-package: com.jt.pojo
  mapper-locations: classpath:/mybatis/mappers/*.xml
  #Turn on hump mapping
  configuration:
    map-underscore-to-camel-case: true

 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2.2.6 edit Mapper interface / mapping file

2.2.7 submit Mapper interface to container management

2.2.8 Mybatis test

Edit the test class and pay attention to the writing of package path

@SpringBootTest
public class TestSpringBoot {
<span class="token annotation punctuation">@Autowired</span>
<span class="token keyword">private</span> DemoUserMapper userMapper<span class="token punctuation">;</span>

<span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testFindAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>

    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>userMapper<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

2.3 about binding exceptions

2.3.1 description of error reporting

Binding exception: binding exception between mapper interface and xml Mapping file

	org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.jt.mapper.DemoUserMapper.findAll

 
  • 1

Code Troubleshooting:
1). Check the path of Mapper interface

2). Check mapper XML import process

3). Check mapper XML Mapping File

4). Inspection method name
Check whether the mapper interface method name is consistent with the label ID

5). Empty / recompile project
1).clean project

2).build project

6). Copy all my code and test it Paste only POM XML and src files

7). Check the directory hierarchy and whether it is divided by / or not

2.3.2 database password

The default database password is root/root
When creating a database password, start with the number 0 012345
Problem explanation: the data password is compiled If the initial letter is 0, it will be deleted automatically
Solution: if it starts with a special number, "012345" is wrapped in quotation marks

2.4 about Mapper interface proxy object creation process

Description: when springBoot starts, load POM XML file Flow chart of instantiation object

2.5 Mybatis warehousing exercise

2.5.1 editing Mapper interface

2.5.2 edit mapper XML file

<!--
		Complete the user warehousing operation id="Consistent with method name"
		sql No need to add at the end;Number in Mysql Execute in database;No problem,
		But if Oracle During execution, an error is bound to be reported.
	-->
	<insert id="insertUser">
		insert into demo_user(id,name,age,sex)
			values(null,#{name},#{age},#{sex})
	</insert>

 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2.5.3 service call

 	//New user
    @Test
    public void testInsert(){
        DemoUser user = new DemoUser();
        user.setId(null).setName("mybatis information").setAge(18).setSex("male");
        userMapper.insertUser(user);
    }

 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2.6 Mybatis update exercise

2.6.1 Mapper interface

2.6.2 edit mapper XML Mapping File

In mapper Update operation in XML file

2.6.3 edit test class