Introduction
As can be seen from Boot in the Spring Boot project name, Spring Boot's role is to create and start new projects based on Spring framework. Its purpose is to help developers easily create applications based on Spring frameworks at standalone and product-level. Spring Boot will select the most suitable Spring subproject and third-party open source libraries to integrate. Most Spring Boot applications require very little configuration to run quickly.
Spring Boot contains the following features:
Create Spring apps that can run independently.
Embed directly into Tomcat or Jetty servers without the need to deploy WAR files.
Provides recommended basic POM files to simplify Apache Maven configuration.
Automatically configure Spring frameworks based on project dependencies as much as possible.
Provides features that can be used directly in a production environment, such as performance metrics, application information, and application health checks.
There is no code generation, and no XML configuration files.
Okay, so much said above is to lay the foundation for the following text. Interested friends can continue reading.
Everyone knows that springboot only takes seconds to build a spring framework.
Let me introduce the perfect fusion of springboot and mybatis:
First: Create a maven project called springboot-mybatis. Remember: you must maven. Those who don’t understand maven can make up for maven knowledge by themselves. I won’t introduce maven here.
The complete configuration of pom.xml is given below:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>springboot-mybatis</groupId><artifactId>springboot-mybatis</artifactId><version>1.0.0</version><packaging>war</packaging><name>springBoot-mybatis</name><description>SpringBoot-mybatis</name><description>SpringBoot project</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.2.RELEASE</version><relativePath/></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></properties><dependencies> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>my batis-spring-boot-starter</artifactId><version>1.1.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.21</version></dependency></depe ndencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><configuration><skip>true</skip></configuration></plugin></plugin></build></project>
Then create a startup class:
package org.shenlan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/*** Created by wangwei on 2016/9/2.*/@SpringBootApplicationpublic class Application {public static void main(String[] args){SpringApplication.run(Application.class,args);}} Such a complete springboot project is completed, isn't it very simple?
Next, you can organize the things with mybatis.
First, create the configuration file: application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driversserver.port=1111
Here server.port=1111 defines the port for changing the project, and the default is 8080.
Then, define a Java entity class:
package org.shenlan.web;/*** Created by wangwei on 2016/9/2.*/public class User {private Integer id;private String name;private Integer age;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}} Here, the fields of the entity class must correspond to the fields of the database, otherwise they will have to be given an alias.
After that, define a dao interface:
package org.shenlan.web;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;import org.apache.ibatis.annotations.Select;/*** Created by Administrator on 2016/9/2.*/@Mapperpublic interface UserMapper {@Select("select * from user where name = #{name}")User findUserByName(@Param("name")String name);} @Mapper is a key step for us to integrate with mybatis, and it will be done with just one annotation.
Hahaha, finally let’s write a test class:
package org.shenlan.web;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;/*** Created by wangwei on 2016/9/2.*/@RestController@RequestMapping({"/home"})public class UserController {@AutowiredUserMapper userMapper;@RequestMapping(value = "/user")@ResponseBodypublic String user(){User user = userMapper.findUserByName("Wang Wei"); return user.getName()+"-----"+user.getAge();}}@RestController is the corresponding restful style controller. @RequestMapping can correspond to an array Open the browser and enter: http://localhost:1111/home/user
The effects are as follows:
The above is a detailed explanation of the integration examples of springboot and mybatis introduced to you (perfect integration). I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!