亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

Springboot下使用Mybatis

rollback / 2353人閱讀

摘要:最近有項(xiàng)目需要使用語(yǔ)言開發(fā)數(shù)據(jù)接口,整體框架需要符合微服務(wù)架構(gòu),在網(wǎng)上查找了相關(guān)資料,最終選定了的架構(gòu),此文主要記錄在下的使用,一方面作為學(xué)習(xí)日記,另一方面也希望對(duì)正在學(xué)習(xí)的朋友們有一定幫助。最后,整個(gè)項(xiàng)目完成后目錄結(jié)構(gòu)如下

最近有項(xiàng)目需要使用java語(yǔ)言開發(fā)數(shù)據(jù)接口,整體框架需要符合微服務(wù)架構(gòu),在網(wǎng)上查找了相關(guān)資料,最終選定了Springcloud+Springboot的架構(gòu),此文主要記錄Mybatis在Springboot下的使用,一方面作為學(xué)習(xí)日記,另一方面也希望對(duì)正在學(xué)習(xí)springboot的朋友們有一定幫助。
全文包含3部分內(nèi)容:

項(xiàng)目搭建

數(shù)據(jù)庫(kù)配置

數(shù)據(jù)操作層編寫

項(xiàng)目創(chuàng)建

本文中使用的編輯器為Intellij Idea,創(chuàng)建流程也是基于Intellij,具體步驟如下:

在New Project面板中中選擇Spring Initializr,點(diǎn)擊Next按鈕,如下圖:

填寫項(xiàng)目的相關(guān)信息,Type一行根據(jù)使用習(xí)慣選擇Maven或者Gradle就好。

選擇項(xiàng)目的相關(guān)依賴,該文章中Web選項(xiàng)里選擇了Web,Sql選項(xiàng)里面勾選Mybatis及Mysql后按著向?qū)瓿身?xiàng)目創(chuàng)建,如下圖:

最終生成的項(xiàng)目目錄結(jié)構(gòu)如下圖:

如下為項(xiàng)目的依賴配置文件代碼:



    4.0.0

    com.example
    demo
    0.0.1-SNAPSHOT
    jar

    demo
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.4.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.0
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


數(shù)據(jù)庫(kù)配置

在Springboot中,數(shù)據(jù)庫(kù)的配置非常簡(jiǎn)單,在resources文件夾下的application.propertites編寫相關(guān)配置即可,喜歡yml格式的也可以換成application.yml,具體如下:

mybatis.type-aliases-package=com.aryan.entity

spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = demopc

需注意第一行配置,com.aryan.entity指定了后面的mybatis數(shù)據(jù)庫(kù)實(shí)體從哪個(gè)包進(jìn)行查找。

數(shù)據(jù)庫(kù)操作層編寫 實(shí)體層編寫

在main的src目錄下新建package,命名為entity,作為mybatis的數(shù)據(jù)庫(kù)實(shí)體層,與前面數(shù)據(jù)庫(kù)配置相對(duì)應(yīng)。

創(chuàng)建Product實(shí)體,代碼如下:

package com.aryan.entity;

import java.io.Serializable;

/**
 * Created by Aryan on 2017/6/25.
 */
public class ProductEntity implements Serializable {
    private Long id;
    private String name;
    private String code;

    public ProductEntity() {
        super();
    }
    public ProductEntity(Long id, String name, String code) {
        super();
        this.id = id;
        this.name = name;
        this.code = code;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public String toString() {
        return "ProductEntity{" +
                "id=" + id +
                ", name="" + name + """ +
                ", code="" + code + """ +
                "}";
    }
}
Dao層編寫

在main目錄的src文件夾下新建package,命名為mapper,該package作為項(xiàng)目的dao層映射,將sql語(yǔ)句與調(diào)用的方法關(guān)聯(lián)起來(lái),整體使用起來(lái)比較靈活。

在mapper包下新建ProductMapper接口,編寫相關(guān)數(shù)據(jù)庫(kù)操作方法,具體代碼如下:

package com.aryan.mapper;

import com.aryan.entity.ProductEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * Created by Aryan on 2017/6/25.
 */
public interface ProductMapper {
    @Select("SELECT * FROM products")
    List getAll();

    @Select("SELECT * FROM Products WHERE id = #{id}")
    ProductEntity getOne(Long id);

    @Insert("INSERT INTO products(name,code) VALUES(#{name}, #{code})")
    void insert(ProductEntity product);
}

控制器編寫

在main目錄src文件夾下新建package,命名為controller,該package作為MVC中的控制器,主要編寫了產(chǎn)品添加及查詢的路由,具體代碼如下:

package com.aryan.controller;

import com.aryan.entity.ProductEntity;
import com.aryan.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Aryan on 2017/6/25.
 */
@RestController
public class ProductController {
    @Autowired
    private ProductMapper productMapper;

    @RequestMapping(value = "/products")
    public List getAll() {
        return productMapper.getAll();
    }

    @RequestMapping(value = "/product/{id}")
    public ProductEntity getOne(@PathVariable Long id) {
        return productMapper.getOne(id);
    }

    @RequestMapping(value = "/product/add", method = RequestMethod.POST)
    public void insert(@RequestBody ProductEntity product) {
        productMapper.insert(product);
    }
}
運(yùn)行項(xiàng)目

運(yùn)行項(xiàng)目前首先需要在Application入口文件中加上dao層配置,添加@MapperScan("com.aryan.mapper")注解,代碼如下:

package com.aryan;

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

@SpringBootApplication
@MapperScan("com.aryan.mapper")
public class SpringBootMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootMybatisApplication.class, args);
    }
}

直接運(yùn)行該項(xiàng)目, 運(yùn)行成功后,在進(jìn)行api訪問(wèn)時(shí)別忘記先把數(shù)據(jù)庫(kù)表生成好, 然后訪問(wèn)http://localhost:8080/products 查看數(shù)據(jù)庫(kù)數(shù)據(jù),通過(guò)Postman或者Fiddler采用Post訪問(wèn)http://localhost:8080/product... 并傳遞產(chǎn)品數(shù)據(jù),成功后可以查看表數(shù)據(jù)確認(rèn)接口是否運(yùn)行正常。
最后,整個(gè)項(xiàng)目完成后目錄結(jié)構(gòu)如下:

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/67237.html

Failed to recv the data from server completely (SIZE:0/8, REASON:closed)