Springboot Manual construction Project-configuring MyBatis TK Framework

Source: Internet
Author: User

Recently has been learning springboot and cloud, internet companies are now more inclined to micro-service this piece, the prospect is a bright, especially in the springboot development of the cloud part, is a set of distributed overall solution, learn this piece at least in the past few years are very popular;

Since study for a long time, landing practice for good;

Project git URL: https://github.com/David-BIQI/manage.git (project using newer springboot2.0 and JDK8)

Referenced code specification: Https://github.com/xwjie/PLMCodeTemplate.git (This is a set of code specifications can be landed, followed by the wind Brother learning a lot)

The project has been initialized, connected to the database, configured with the MyBatis TK framework, plus the paging plugin < the principle of connecting the mybatis, as well as the principle of pagination implementation >

Application.yml under MyBatis configuration:

# MyBatis Package Scan, there is the mapping file settings mybatis TK plugin use mybatis:    type-aliases-package:package Com.biqi.model    Mapper-locations:classpath:mapper/*.xml    #配置驼峰下划线    configuration:      map-underscore-to-camel-case:true

  

The rack package in POM:

<!--mybatis TK framework--><dependency><groupid>tk.mybatis</groupid><artifactid> mapper-spring-boot-starter</artifactid><version>1.2.4</version></dependency><!--- Paging plugin--><dependency><groupid>com.github.pagehelper</groupid><artifactid> pagehelper-spring-boot-starter</artifactid><version>1.2.3</version></dependency><!- --mybatis tk Frame--

  

Examples of TK implementations:

1. Define an interface

Package Com.common.mybatis;import Tk.mybatis.mapper.common.mapper;import tk.mybatis.mapper.common.mysqlmapper;/**    * Description: To achieve the basic SQL function of adding and removing changes * @Package com.common.mybatis  * @author  xiebq @date    June 7, 2018 9:53:34  */public interface mymapper<t> extends Mapper<t>, mysqlmapper<t> {    //fixme special note that the interface cannot be scanned, Otherwise it will go wrong}

DAO Inheritance Interface

Package Com.biqi.dao;import Org.apache.ibatis.annotations.mapper;import Org.apache.ibatis.annotations.Select; Import Com.biqi.model.user;import com.common.mybatis.mymapper;/**    * @Package Com.biqi.dao  * @author  XIEBQ @date    June 7, 2018 Morning 9:55:30  * * @Mapperpublic interface Userdao extends mymapper<user> {    /**     * Test Database connection     * @return     *    /@Select ("SELECT count (*) from User")    Integer countuser ();            int CountUser2 ();    }

Specific use

     Publicuser Getuserbyid (Integer ID) {User user=Userdao.selectbyprimarykey (ID); returnuser; }     PublicThe Integer saveuser (user user) {User.setid (NULL);        User.setcreateby (Superuserid); User.setcreated (NewDate ());        Userdao.insertusegeneratedkeys (user); returnUser.getid (); }     PublicBoolean deleteuser (Integer id) {User olduser=Userdao.selectbyprimarykey (ID); Notnull (Olduser,"User id:" +id+ "does not exist");        Userdao.deletebyprimarykey (ID); return true; }

Examples of pagination:

Public pagedto<user> listpage (integer page, integer size) {//TK MyBatis is queried        Example Example = new Example (User. Class);        Paged Query        pagehelper.startpage (page, size);        Add Query Condition        //example.createcriteria (). Andequalto ("id", XX);        Example.orderby ("created"). Desc ();        list<user> list = Userdao.selectbyexample (example);        Paged Query--Jofaine here, cannot be paged pagehelper.startpage (page, size);        pageinfo<user> PageInfo = new pageinfo<user> (list), return new pagedto<> (list, pageinfo.gettotal ());}

XML file < Do not like to write SQL statements in Interfaces >

<?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.biqi.dao.UserDao" ><select id= "CountUser2" resulttype= "Java.lang.Integer" >select Count (*) from User </select></mapper>

The above steps do a single table of additions and deletions, as well as the basic completion of the page. The following is the ability of TK to complete the following query,

Springboot Manual construction Project-configuring MyBatis TK Framework

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.