This is the 9th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Include a column

Spring Boot quick start

Java full stack Architect

preface

The previous article introduced the simplest components of Spring Boot and started the service to show the first Spring Boot project, not connected to the database. This paper integrates MyBatis for Spring Boot minimalist project, starts up and runs, and connects to the database to query data. All right, let’s get started on the new article.

I met MyBatis

MyBatis is an excellent persistence layer framework that supports custom SQL, stored procedures, and advanced mappings. MyBatis eliminates almost all of the JDBC code and the work of setting parameters and getting result sets. MyBatis configures and maps primitive types, interfaces, and Java POJOs to records in the database using simple XML or annotations.

To build a project with Maven, place the following dependencies in a pom.xml file:

<! -- mybatis start--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <! -- mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> < artifactId > mybatis - spring - the boot - starter < / artifactId > < version > 2.1.1 < / version > < / dependency > <! -- druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId> <version>1.1.11</version> </dependency>Copy the code

MyBatis characteristics

MyBatis can be widely used, and its advantages are inseparable. The main advantages of MyBatis are simple to learn, flexible configuration, concise tags, and support for dynamic SQL. The following is the summary of the advantages of MyBatis:

  • Easy to learn: Small and simple in itself. There is no third-party dependency, the simplest installation as long as two JAR files + configure a few SQL mapping files easy to learn, easy to use, through the documents and source code, can be more complete grasp its design and implementation.
  • Flexible configuration: MyBatis does not impose any impact on the existing design of the application or database. SQL is written in XML for unified management and optimization. All requirements for operating a database can be met with SQL statements.
  • Decoupling SQL from program code: By providing a DAO layer that separates business logic from data access logic, the system design is cleaner, easier to maintain, and easier to unit test. The separation of SQL and code improves maintainability.
  • Provide mapping labels: Support relational mapping between objects and database ORM fields
  • Provide object relational mapping label: support object relational building and maintenance
  • Provide XML tags: support for writing dynamic SQL.

MyBatis configuration

The MyBatis configuration for this tutorial is the application.properties file and, of course, the YML file.

  • Spring.datasource. url: specifies the connection address of the database server
  • Spring. The datasource. Username: database user name
  • Spring. The datasource. Password: database user password
  • Spring.datasource. Driver-class-name: specifies the name of the datasource driver class
  • Spring.datasource. type: indicates the driver type of the datasource
  • Mybatis. Mapper-locations: location of the local SQL configuration file
Spring. The datasource. Url = JDBC: mysql: / / 127.0.0.1:3306 / test? useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull spring.datasource.username=test spring.datasource.password=12345678 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource mybatis.mapper-locations=classpath*:mapper/**/*.xmlCopy the code

The project structure

The project structure is shown in the following figure, including the startup classes DemoMyBatisApplication, Controller, Service, ServiceImpl, Mapper, application.properties, and POM.xml

@ MapperScan annotations

MapperScan configures one or more package paths, automatically scans for classes in those package paths, and automatically generates proxy classes for them. Be sure to add this annotation, otherwise the startup project will report an exception.

Project start

Find the DemoMyBatisApplication start class in the project, right click and select Start.

Query data

Enter in your browser: http://127.0.0.1:8888/user/getAllUser can query to the user data. Data information is returned as shown in the following figure

On the source code

The source code used in this project can be directly used to learn, the author has been tested, can run normally. The following source code is for learning only.

pom.xml

<? The 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 > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.3.0. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>mybatis</artifactId> <version>0.0.1 -snapshot </version> <name>mybatis</name> <description>Demo project for Spring Boot and mybatis and Swagger</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <! -- commons start --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId> Commons -lang3</artifactId> <version>3.7</version> </dependency> <! -- commons end --> <! -- mybatis start--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <! -- mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> < artifactId > mybatis - spring - the boot - starter < / artifactId > < version > 2.1.1 < / version > < / dependency > <! -- druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId> <version>1.1.11</version> </dependency> <! -- mybatis end--> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

application.properties

Server. The port = 8888 # mysql spring. The datasource. Url = JDBC: mysql: / / 39.106.126.132:3306 / test? useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull spring.datasource.username=test spring.datasource.password=e2777a3c54 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource mybatis.mapper-locations=classpath*:mapper/**/*.xmlCopy the code

DemoMyBatisApplication

package com.example.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @ClassName DemoMyBatisApplication * @Description: Java full stack Architect * @date 2020/6/13 * @version V1.0 **/ @springBootApplication @MapperScan("com.example.demo.mapper") public class DemoMyBatisApplication { public static void main(String[] args) { SpringApplication.run(DemoMyBatisApplication.class, args); }}Copy the code

UserMapper.xml

<? The 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.example.demo.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example.demo.module.User"> <id column="id" jdbcType="INTEGER" property="id" /> <result column="name" jdbcType="VARCHAR" property="name" /> <result column="age" jdbcType="INTEGER" property="age" /> </resultMap> <sql id="Base_Column_List"> id, name, age </sql> <select id="getAllUser" resultType="com.example.demo.module.User"> SELECT * FROM user </select> </mapper>Copy the code

UserController

package com.example.demo.controller; import com.example.demo.module.User; import com.example.demo.service.UserService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import java.util.List; /** * @ClassName UserController * @Description: TODO * @author JavaZhan @public :Java full stack Architect * @date 2020/6/13 * @version V1.0 **/ @requestMapping ("user") @controller public class UserController { @Resource private UserService userService; @RequestMapping("getAllUser") @ResponseBody public List<User> getAllUser(){ return userService.getAllUser(); }}Copy the code

Specific implementation, the user can develop processing according to the business logic, this article does not list the specific implementation of the relevant code class, we can develop according to the need.

conclusion

The project of integrating MyBatis based on Spring Boot has been completed, and an integrated framework has been established carelessly, of course, there are more in-depth configuration Settings. This article is mainly for beginners to use the exercise, I hope this article can help you. Thanks for reading.

Author introduction: [little ajie] a love to tinker with the program ape, JAVA developers and enthusiasts. Public account [Java full stack architect] maintainer, welcome to pay attention to reading communication.

Well, thank you for reading, I hope you like it, if it is helpful to you, welcome to like the collection. If there are any shortcomings, please comment and correct them. See you next time.

Recommended Reading:

My first Spring Boot project is up!

Spring Boot column was set up over the weekend. Welcome to learn and communicate