Springboot and MyBatis Integration

Source: Internet
Author: User

  (1) The introduction of the jar package in Pom.xml, as follows: There is no need to introduce SPRING-BOOT-STARTER-JDBC dependencies, because this dependency is already included in Mybatis-spring-boot-starter

        <dependency>            <groupId>mysql</groupId>            <artifactid>mysql-connector-java</ artifactid>        </dependency>        <dependency>            <groupid>org.mybatis.spring.boot</ groupid>            <artifactId>mybatis-spring-boot-starter</artifactId>            <version>1.1.1</ Version>        </dependency>

  (2) in the Application.properties configuration database connection information, as follows:

#mysql数据库配置spring. Datasource.url=jdbc:mysql://172.31.19.20:3306/springboot Spring.datasource.username=Rootspring.datasource.password=rootspring.datasource.driver-  Class-name=com.mysql.jdbc.driver

(3) Create a new user entity class as follows:

 PackageSpringboot.domain; Public classUser {PrivateString ID; PrivateString name; PrivateString age;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString getage () {returnAge ; }     Public voidsetage (String age) { This. Age =Age ; }     PublicString getId () {returnID; }     Public voidsetId (String id) { This. ID =ID; } @Override PublicString toString () {return"User [id=" + ID + ", name=" + name + ", age=" + Age + "]"; }    }

(4) New Usermapper class

 PackageSpringboot.dao;ImportOrg.apache.ibatis.annotations.Delete;ImportOrg.apache.ibatis.annotations.Insert;ImportOrg.apache.ibatis.annotations.Mapper;ImportOrg.apache.ibatis.annotations.Param;ImportOrg.apache.ibatis.annotations.Select;Importorg.apache.ibatis.annotations.Update;ImportSpringboot.domain.User; @Mapper Public Interfaceusermapper {@Insert ("INSERT into User (Name,age) VALUES (#{name},#{age})")    intAddUser (@Param ("name") String name, @Param ("Age") String age); @Select ("SELECT * from user where ID =#{id}") User FindByID (@Param ("id") String ID); @Update ("Update user set Name=#{name} where Id=#{id}")    voidUpdatabyid (@Param ("id") String ID, @Param ("name") String name); @Delete ("Delete from user where Id=#{id}")    voidDeletebyid (@Param ("id") String ID); }

  Using the MyBatis annotated version here, it is quite handy to have an XML file that corresponds to the DAO layer, which is less than before.

(5) The service layer code is as follows:

 PackageSpringboot.service;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Service;ImportSpringboot.dao.UserMapper;ImportSpringboot.domain.User; @Service Public classUserService {@AutowiredPrivateUsermapper Usermapper;  PublicUser FindByID (String id) {returnUsermapper.findbyid (ID); }         Public intAddUser (String name,string age) {returnUsermapper.adduser (name,age); }         Public voidUpdatabyid (String id,string name) {Usermapper.updatabyid (id,name); }         Public voidDeletebyid (String id) {Usermapper.deletebyid (ID); }}

 (6) Controller layer, the code is as follows:

 PackageSpringboot.web;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestParam;ImportOrg.springframework.web.bind.annotation.RestController;ImportSpringboot.domain.User;ImportSpringboot.service.UserService; @RestController Public classHellocontroller {@AutowiredPrivateUserService UserService; @RequestMapping ("/adduser")     Public intAddUser (@RequestParam ("name") String name, @RequestParam ("Age") (String age) {returnUserservice.adduser (name, age); } @RequestMapping ("/finduser")     PublicUser Finduser (@RequestParam ("id") (String ID) {returnUserservice.findbyid (ID); } @RequestMapping ("/updatabyid")     PublicString Updatabyid (@RequestParam ("id") string ID, @RequestParam ("name") (String name) {Try{Userservice.updatabyid (ID, name); } Catch(Exception e) {return"Error"; }        return"Success"; } @RequestMapping ("/deletebyid")     PublicString Deletebyid (@RequestParam ("id") (String ID) {Try{Userservice.deletebyid (ID); } Catch(Exception e) {return"Error"; }        return"Success"; }}

Springboot and MyBatis Integration

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.