(a) Build your own Springboot back-end framework integration MyBatis

Source: Internet
Author: User

One: Building the infrastructure with the idea tool

1. Open idea, upper left corner file→new→project,

2. Click Next

3. Click Next, configure as, here we select database MySQL and persistent layer framework MyBatis

4. Click Next, select Working directory, click Finish to start building

5. After the creation is complete, the project directory structure is as follows

Second: Configure database information

Add the following database configuration in the Application.properties file

  

Spring.datasource.url=jdbc:mysql://localhost:3306/demo? usessl=false&useunicode=true&characterencoding=utf-8&zerodatetimebehavior=converttonull& Transformedbitisboolean=true&autoreconnect=true&failoverreadonly=falsespring.datasource.username= Database user name spring.datasource.password= database password Spring.datasource.driverclassname=com.mysql.jdbc.driver

  

Three: Create a database UserInfo table
CREATE TABLE ' user_info ' (  ' id ' int (+) NOT NULL auto_increment,  ' user_name ' varchar (255) ' DEFAULT NULL,  

  

Four: Create a project base directory structure

Model Storage Entity Class

Package Com.example.demo.model;import javax.persistence.column;import javax.persistence.id;/** * @author  * @ Description: * @time 2018/4/18 11:55 */public class UserInfo {    /**     * PRIMARY KEY */    @Id    private String id;
   
    /**     * User name     *    /@Column (name = "user_name")    private String userName;    private String password;    Public String getId () {        return ID;    }    public void SetId (String id) {        this.id = ID;    }    Public String GetUserName () {        return userName;    }    public void Setusername (String userName) {        this.username = userName;    }    Public String GetPassword () {        return password;    }    public void SetPassword (String password) {        this.password = password;    }}
   

Mapper

<?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.example.demo.dao.UserInfoMapper" >    <resultmap id= "Baseresultmap" type= " Com.example.demo.model.UserInfo ">        <id column=" id "jdbctype=" INTEGER "property=" id "/>        <result column= "user_name" jdbctype= "VARCHAR" property= "UserName"/>    </resultMap>    <sql id= "Base_column _list ">      id,user_name    </sql>    <select id=" Selectbyid "parametertype=" Java.lang.Integer " resultmap= "Baseresultmap" >        select        <include refid= "Base_column_list"/> from        user_info        WHERE id = #{id,jdbctype=varchar}    </select></mapper>

DAO layer

Package Com.example.demo.dao;import Com.example.demo.model.userinfo;import org.apache.ibatis.annotations.param;/* * * @author  * @Description: * @time 2018/4/18 11:54 */public interface Userinfomapper {    UserInfo Selectbyid (@Param ("id") Integer ID);}

Service

Package Com.example.demo.service;import com.example.demo.model.userinfo;/** * @author  * @Description: * @time 2018 /4/18 11:56 */public interface Userinfoservice {    UserInfo Selectbyid (Integer ID);}

Serviceimpl

Package Com.example.demo.service.impl;import Com.example.demo.dao.userinfomapper;import Com.example.demo.model.userinfo;import Com.example.demo.service.userinfoservice;import Org.springframework.stereotype.service;import javax.annotation.resource;/** * @author  * @Description: * @time 2018/4/18 11:56 */@Servicepublic class Userinfoserviceimpl implements userinfoservice{    @Resource    Private Userinfomapper Userinfomapper;    Public UserInfo Selectbyid (Integer id) {        return Userinfomapper.selectbyid (ID);}    }

Controller

Package Com.example.demo.controller;import Com.example.demo.model.userinfo;import Com.example.demo.service.userinfoservice;import Org.springframework.web.bind.annotation.postmapping;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.restcontroller;import javax.annotation.resource;/** * @author  * @ Description: * @time 2018/4/18 11:39 */@RestController @requestmapping ("UserInfo") public class Userinfocontroller {    @Resource    private userinfoservice userinfoservice;    @PostMapping ("/hello") public    String Hello () {        return "Hello Springboot";    }    @PostMapping ("/selectbyid") public    UserInfo Selectbyid (Integer id) {        return Userinfoservice.selectbyid (ID);}    }

Configuration Java class for MyBatis

Package Com.example.demo.core.configurer;import Org.apache.ibatis.session.sqlsessionfactory;import Org.mybatis.spring.sqlsessionfactorybean;import Org.mybatis.spring.mapper.mapperscannerconfigurer;import Org.springframework.context.annotation.bean;import Org.springframework.context.annotation.configuration;import Org.springframework.core.io.support.pathmatchingresourcepatternresolver;import Org.springframework.core.io.support.resourcepatternresolver;import javax.sql.datasource;/** * @ClassName: Mybatisconfigurer * @Description: Mybatis configuration * @author * @date January 20, 2018 PM 4:03:46 * */@Configurationpublic class Mybatis Configurer {@Bean public sqlsessionfactory Sqlsessionfactorybean (DataSource DataSource) throws Exception {Sqlse      Ssionfactorybean factory = new Sqlsessionfactorybean ();      Factory.setdatasource (DataSource);      Factory.settypealiasespackage ("Com.example.demo.model"); Add XML directory Resourcepatternresolver resolver = new Pathmatchingresourcepatternresolver ();      Factory.setmapperlocations (Resolver.getresources ("Classpath:mapper/*.xml"));   return Factory.getobject (); } @Bean Public Mapperscannerconfigurer mapperscannerconfigurer () {Mapperscannerconfigurer mapperscannerconfigure      R = new Mapperscannerconfigurer ();      Mapperscannerconfigurer.setsqlsessionfactorybeanname ("Sqlsessionfactorybean");      Mapperscannerconfigurer.setbasepackage ("Com.example.demo.dao");   return mapperscannerconfigurer; }}

  

@Configuration indicates that the file is a configuration file

@Bean indicates that the method is a <bean id= "" In a traditional XML configuration file ></Bean>

where Factory.settypealiasespackage ("Com.example.demo.model") represents the storage path of the model in the project;

Factory.setmapperlocations (Resolver.getresources ("Classpath:mapper/*.xml")); indicates the mapper.xml storage path;

Mapperscannerconfigurer.setbasepackage ("Com.example.demo.dao"); represents the storage path of the DAO layer

Five: Run the project

Find DemoApplication, right-click, select Run DemoApplication

Above content source online, if there is infringement please contact me!!!

(a) Build your own Springboot back-end framework integration MyBatis

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.