I remember when I first came into contact with SpringBoot, I was surprised that there was such a convenient framework in the world. I immediately exclaimed: SpringBoot is the best framework in the world. Ha ha!

At the beginning with the tutorial exercise to build a framework, portal: Spring Boot + JPA + Bootstrap + Thymeleaf simple add, delete, change and check Demo

Later, I entered a new company and was busy with tasks. Today, I forgot some of them. It seems that a good memory is better than a bad pen. So write this article SpringBoot integration Mybatis, make a note.

This chapter mainly builds the framework, the following chapter realizes the login registration and the interceptor configuration: SpringBoot integration Mybatis complete Detail version 2: registration, login, interceptor configuration

This chapter project source download: springBoot integration mybatis complete detailed version

Github address: github.com/wjup/spring…

After this exercise, you can download itOne hour to quickly churn out a short URL generation projectSource code (welcome star), is also very suitable for beginners practice.

It’s getting late. Let’s get back to business. Here we go


IDE: IDEA, DB: mysql

  • Create a New Spring Initializr project

  • Create the file structure of the project and the version of the JDK

  • Select the dependencies required by the project

  • Change the project name and finish

  • Let’s look at the POM after it’s built
 
Copy the code
  • <? The XML version = "1.0" encoding = "utf-8"? >
  • The < 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 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
  • The < modelVersion > 4.0.0 < / modelVersion >
  •  
  • <groupId>com.example</groupId>
  • <artifactId>demo</artifactId>
  • < version > 0.0.1 - the SNAPSHOT < / version >
  • <packaging>jar</packaging>
  •  
  • <name>demo</name>
  • <description>Demo project for Spring Boot</description>
  •  
  • <parent>
  • <groupId>org.springframework.boot</groupId>
  • <artifactId>spring-boot-starter-parent</artifactId>
  • < version > at 2.0.5. RELEASE < / version >
  • <relativePath/> <! -- lookup parent from repository -->
  • </parent>
  •  
  • <properties>
  • <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  • <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  • < Java version > 1.8 < / Java. Version >
  • </properties>
  •  
  • <dependencies>
  • <dependency>
  • <groupId>org.springframework.boot</groupId>
  • <artifactId>spring-boot-starter-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>
  • The < version > 1.3.2 < / version >
  • </dependency>
  •  
  • <dependency>
  • <groupId>mysql</groupId>
  • <artifactId>mysql-connector-java</artifactId>
  • <scope>runtime</scope>
  • </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>
  • Modifying a Configuration File

Instead of using the application.properties file, this article uses the more concise application.yml file. Delete the application.properties file from the resource folder and create the application.yml configuration file (note: Properties ****); / / create two yML files (application.yml and application-dev.yml). Take a look at the content separately

application.yml

 
Copy the code
  1. spring:
  2. profiles:
  3. active: dev

application-dev.yml

 
Copy the code
  1. server:
  2. port: 8080
  3.  
  4. spring:
  5. datasource:
  6. username: root
  7. password: 1234
  8. url: jdbc:mysql://localhost:3306/springboot? useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
  9. driver-class-name: com.mysql.jdbc.Driver
  10.  
  11. mybatis:
  12. mapper-locations: classpath:mapping/*Mapper.xml
  13. type-aliases-package: com.example.entity
  14.  
  15. #showSql
  16. logging:
  17. level:
  18. com:
  19. example:
  20. mapper : debug

The meaning of the two files is:

A configuration method for configuring multiple sets of environments in a project. Because a project now have a lot of environment, development environment, test environment, production environment, production environment, the parameters of each environment is different, so we can put the parameters of each environment configuration to yml file, so want to use the environment which only needs to be in the main configuration file with write line such as the application configuration file. The yml

Note: In Spring Boot, the multi-environment configuration file name should meet the application-{profile}. Yml format, where {profile} corresponds to your environment identity, for example:

Application-dev. yml: development environment application-test.yml: test environment application-prod.yml: application dev. The specific configuration file to be loaded in the production environment needs to be set in the application.yml file using the spring.profiles.active property, which corresponds to the {profile} value.

In addition, it is better not to have Chinese annotations in the configuration file, which will report errors.

Solution (not tested) : Spring Boot Application.yml file Chinese annotation garbled

Next move the startup file to com.example, and the SpringBoot startup class cannot be placed in the Java directory!! You have to have a bag to put it in

Otherwise an error will be reported:

Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
Copy the code

This is notable because it is sometimes difficult to recognize this error in the project directory in IDEA and it is also easy to miss some classes, portal: SpringBoot cannot scan controllers

                         

  • Then start creating entity classes to implement the business process

Create packages controller, Entity, mapper, and Service. Create a mapping folder under Resources and use it to write SQL statements. You can also use annotations to write them directly in mapper files. The code is posted directly below

Database table structure (previously small project table, directly used)

 
Copy the code
  1. CREATE TABLE `user` (
  2. `id` int(32) NOT NULL AUTO_INCREMENT,
  3. `userName` varchar(32) NOT NULL,
  4. `passWord` varchar(50) NOT NULL,
  5. `realName` varchar(32) DEFAULT NULL,
  6. PRIMARY KEY (`id`)
  7. ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

entity.java

 
Copy the code
  1. package com.example.entity;
  2.  
  3. / * *
  4. * @Author:wjup
  5. * @Date: 2018/9/26 0026
  6. * @Time: 14:39
  7. * /
  8. public class User {
  9. private Integer id;
  10. private String userName;
  11. private String passWord;
  12. private String realName;
  13.  
  14. public Integer getId() {
  15. return id;
  16. }
  17.  
  18. public void setId(Integer id) {
  19. this.id = id;
  20. }
  21.  
  22. public String getUserName() {
  23. return userName;
  24. }
  25.  
  26. public void setUserName(String userName) {
  27. this.userName = userName;
  28. }
  29.  
  30. public String getPassWord() {
  31. return passWord;
  32. }
  33.  
  34. public void setPassWord(String passWord) {
  35. this.passWord = passWord;
  36. }
  37.  
  38. public String getRealName() {
  39. return realName;
  40. }
  41.  
  42. public void setRealName(String realName) {
  43. this.realName = realName;
  44. }
  45.  
  46. @Override
  47. public String toString() {
  48. return "User{" +
  49. "id=" + id +
  50. ", userName='" + userName + '\'' +
  51. ", passWord='" + passWord + '\'' +
  52. ", realName='" + realName + '\'' +
  53. '} ';
  54. }
  55. }

UserController.java

 
Copy the code
  1. package com.example.controller;
  2.  
  3. import com.example.entity.User;
  4. import com.example.service.UserService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RestController;
  10.  
  11. / * *
  12. * @Author:wjup
  13. * @Date: 2018/9/26 0026
  14. * @Time: 14:42
  15. * /
  16.  
  17. @RestController
  18. @RequestMapping("/testBoot")
  19. public class UserController {
  20.  
  21. @Autowired
  22. private UserService userService;
  23.  
  24. @RequestMapping("getUser/{id}")
  25. public String GetUser(@PathVariable int id){
  26. return userService.Sel(id).toString();
  27. }
  28. }

UserService.java

 
Copy the code
  1. package com.example.service;
  2.  
  3. import com.example.entity.User;
  4. import com.example.mapper.UserMapper;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7.  
  8. / * *
  9. * @Author:wjup
  10. * @Date: 2018/9/26 0026
  11. * @Time: 15:23
  12. * /
  13. @Service
  14. public class UserService {
  15. @Autowired
  16. UserMapper userMapper;
  17. public User Sel(int id){
  18. return userMapper.Sel(id);
  19. }
  20. }

UserMapper.java

 
Copy the code
  1. package com.example.mapper;
  2.  
  3. import com.example.entity.User;
  4. import org.apache.ibatis.annotations.Select;
  5. import org.springframework.stereotype.Repository;
  6.  
  7. / * *
  8. * @Author:wjup
  9. * @Date: 2018/9/26 0026
  10. * @Time: 15:20
  11. * /
  12. @Repository
  13. public interface UserMapper {
  14.  
  15. User Sel(int id);
  16. }

UserMapping.xml

 
Copy the code
  1. <? The XML version = "1.0" encoding = "utf-8"? >
  2. <! DOCTYPE mapper PUBLIC "- / / mybatis.org//DTD mapper / 3.0 / EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.example.mapper.UserMapper">
  4.  
  5. <resultMap id="BaseResultMap" type="com.example.entity.User">
  6. <result column="id" jdbcType="INTEGER" property="id" />
  7. <result column="userName" jdbcType="VARCHAR" property="userName" />
  8. <result column="passWord" jdbcType="VARCHAR" property="passWord" />
  9. <result column="realName" jdbcType="VARCHAR" property="realName" />
  10. </resultMap>
  11.  
  12. <select id="Sel" resultType="com.example.entity.User">
  13. select * from user where id = #{id}
  14. </select>
  15.  
  16. </mapper>
  • Final frame construction

  • MapperScan(“com.example.mapper”); MapperScan(“com.example.mapper”)
 
Copy the code
  1. package com.example;
  2.  
  3. import org.mybatis.spring.annotation.MapperScan;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6.  
  7. @mapperscan ("com.example.mapper") // Scan the mapper
  8. @SpringBootApplication
  9. public class DemoApplication {
  10.  
  11. public static void main(String[] args) {
  12. SpringApplication.run(DemoApplication.class, args);
  13. }
  14. }
  • Finally, the browser enters the address to see:http://localhost:8080/testBoot/getUser/1

The test was successful, and the basic framework was built

  • Finally, how to change the alphabet of the characters displayed at startup, that is, change the place of the red box icon

TXT file named banner.txt. TXT. This type of character should be spelled out in the following way. As follows:

Enter the generated letters directly, the system will automatically convert, and then copy the converted characters to the new banner. TXT file, restart the project can be.


— The road ahead is long, I see no ending, yet high and low I will search