Springboot and MyBatis Integration examples (perfect fusion) _java

Source: Internet
Author: User
Tags xmlns

Brief introduction

As you can see from the boot in the Spring boot project name, the role of spring boot is to create and launch a new project based on the spring framework. It is designed to help developers easily create stand-alone and product-level applications based on the Spring framework. Spring Boot selects the most appropriate spring subprojects and third-party open source libraries for consolidation. Most Spring Boot applications require very little configuration to run quickly.

Spring Boot contains the following features:

Create a Spring application that can run independently.

Embed a Tomcat or Jetty server directly, and you do not need to deploy a WAR file.

Provides a recommended base POM file to simplify the Apache Maven configuration.

Automatically configure the Spring framework as much as possible based on project dependencies.

Provides functions that can be used directly in a production environment, such as performance metrics, application information, and application health checks.

No code generation, no XML configuration file.

Well, it says so much is to pave the way below, interested friends continue to read it.

As you all know, it only takes seconds for Springboot to build a spring frame.

Here is a brief introduction to the perfect fusion of Springboot and MyBatis:

First: Create a maven project called Springboot-mybatis, and remember: Maven, don't know Maven can make up for Maven's knowledge, and here's no introduction to MAVEN.

The full 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>spring Boot 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> Mybatis-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> </ dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid > &LT;ARTIFACTID&GT;SPRing-boot-maven-plugin</artifactid> </plugin> <plugin> <groupid>org.apache.maven.plugins </groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip> true</skip> </configuration> </plugin> </plugins> </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.
*
/@SpringBootApplication Public
class Application {public
static void Main (string[] args) {
Springapplication.run (Application.class,args);
}

Such a complete Springboot project is complete, is not very simple.

The next thing you can do is to sort out the stuff with MyBatis.

First, create the configuration file: Application.properties

Spring.datasource.url=jdbc:mysql://localhost:3306/test
Spring.datasource.username=root
Spring.datasource.password=root
Spring.datasource.driver-class-name=com.mysql.jdbc.driver
server.port=1111

Here server.port=1111 is defined as the port of the Change project, 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;
}
}

The field of the entity class here corresponds to the field of the database, otherwise the alias will be taken.

After that, define a DAO's 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 the Administrator on 2016/9/2
. * *
@Mapper public
interface Usermapper {
@Select ("Select * from user where name = #{name}")
user find Userbyname (@Param ("name") String name);

@Mapper is the key step we need to merge with MyBatis, as long as one annotation is done.

Ha ha, finally we have to write a test class bar:

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 {
@Autowired
Usermapper Usermapper;
@RequestMapping (value = "/user")
@ResponseBody public
String User () {
User user = Usermapper.finduserbyname ("Wang Wei");
return User.getname () + "-----" +user.getage ();
}
@RestController is the corresponding restful-style controller, @RequestMapping which can correspond to an array oh

Open Browser, enter: Http://localhost:1111/home/user

The effect is as follows:

The above is a small series to introduce the springboot and MyBatis integration examples (perfect fusion), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.