Spring Boot Tutorial (ix): Spring Boot integration Mapper4__springboot

Source: Internet
Author: User
Tags spring boot tutorial
I. Preparation of the project

Direct use of the source code of the previous chapter, Spring Boot Tutorial (eight): Spring boot integrated pagehelper paging plugin II, adding MAPPER4 dependencies

<!--mapper4-->
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactid >mapper-spring-boot-starter</artifactId>
    <version>2.0.2</version>
</dependency >
Third, modify the Startup class @mapperscan package, note the package path!!!

no longer adopt MyBatis Org.mybatis.spring.annotation.MapperScan, but use Mapper4 's: Tk.mybatis.spring.annotation.MapperScan, pay attention to package path !!!

Package com.songguoliang.springboot;

Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
Import Tk.mybatis.spring.annotation.MapperScan;

/** *
 @Description *
 @Author SGL *
 @Date 2018-05-02 14:51 * *
@SpringBootApplication
@ Mapperscan ("Com.songguoliang.springboot.mapper") public
class Application {public
    static void main (String [] args) {
        springapplication.run (application.class, args);
    }
}
Iv. Create your own mapper base class

It is recommended that you create your own mapper base class, and in your own mapper base class, we can define the public method that suits us according to our project needs. If you do not want to use your own creation, you can inherit tk.mybatis.mapper.common.Mapper directly.

Package com.songguoliang.springboot.base;

Import Tk.mybatis.mapper.common.Mapper;
Import Tk.mybatis.mapper.common.MySqlMapper;

/**
 * @Description its own mapper base class, can not be put under mapper
 * @Author SGL
 * @Date 2018-05-07 16:57
 /
Public Interface basemapper<t> extends mapper<t>,mysqlmapper<t>{
}

Note: This interface cannot be mapper in the same table as the table, that is, not in the @mapperscan scanned package v. Configuration file Modification

In the Application.properties configuration file, add the following configuration:

Mapper.mappers=com.songguoliang.springboot.base.basemapper
mapper.not-empty=false
mapper.identity= Mysql
vi. modification of Usermapper

Make Usermapper inherit the parent Mapper:com.songguoliang.springboot.base.BaseMapper we created

Package com.songguoliang.springboot.mapper;

Import Com.github.pagehelper.Page;
Import Com.songguoliang.springboot.base.BaseMapper;
Import Com.songguoliang.springboot.entity.User;

/** *
 @Description *
 @Author SGL *
 @Date 2018-05-02 15:02
* * Public interface Usermapper basemapper<user> {

    page<user> getusers ();
}
vii. modification of UserService

Add a method to get an object from a primary key in UserService:

Public User Selectbyid (long id) {return
    Usermapper.selectbyprimarykey (ID);
}

Here we call the Mapper4 Selectbyprimarykey () method directly, so there is no need to write SQL in mapper files, nor do we need to define methods in Usermapper, which are done by Mapper4.

You can abstract out a baseservice as a UserService parent class, defining some of our common methods. viii. modification of Usercontroller

Add a service to get the user based on the ID

@GetMapping ('/user/{id} ') Public
user Selectuserbyid (@PathVariable (' id ') Long ID) {
    return Userservice.selectbyid (ID);
}
ix. modifying entity classesThe entity adds a @table annotation to specify the table name and, if the table name is the same as the entity name, does not need to be set, because our table name is Tbl_user, so we need to specify. A @id annotation is added to the primary key, and the Union primary key requires @id on each attribute of the primary key. If you do not add the annotation, when you use the Xxxbyprimarykey method, all fields are treated as primary keys, where user_id= appear? and user_name=? And user_age=? This condition.
Package com.songguoliang.springboot.entity;

Import Javax.persistence.Id;
Import javax.persistence.Table;

/** *
 @Description *
 @Author SGL *
 @Date 2018-05-02 14:59
/@Table (name = "Tbl_user")
public class User {
    @Id
    private Long userId;
    Private String userName;
    Private Integer userage;

    Omit getter, setter method
}
10, start the service, test

The browser enters HTTP://LOCALHOST:8080/USER/1 and gets the following:

This article is mainly to explain how to integrate Mapper4, the use of Mapper4, please refer to the Mapper4 author's GitHub





Source:
GitHub
Code cloud

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.