成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

Springboot下使用Mybatis

rollback / 2324人閱讀

摘要:最近有項(xiàng)目需要使用語(yǔ)言開(kāi)發(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ǔ)言開(kāi)發(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ě)

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

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

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

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

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

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

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



    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編寫(xiě)相關(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ù)操作層編寫(xiě) 實(shí)體層編寫(xiě)

在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層編寫(xiě)

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

在mapper包下新建ProductMapper接口,編寫(xiě)相關(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);
}

控制器編寫(xiě)

在main目錄src文件夾下新建package,命名為controller,該package作為MVC中的控制器,主要編寫(xiě)了產(chǎn)品添加及查詢(xú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://systransis.cn/yun/67237.html

相關(guān)文章

  • 單手?jǐn)]了個(gè)springboot+mybatis+druid

    摘要:配置想想,我們需要哪些數(shù)據(jù)庫(kù)要用到,數(shù)據(jù)庫(kù)連接池要用到橋接器要用到,因此要倉(cāng)庫(kù)點(diǎn)我去倉(cāng)庫(kù)中找到搜索這些加進(jìn)去。 本文旨在用最通俗的語(yǔ)言講述最枯燥的基本知識(shí) 最近身邊的程序員掀起了學(xué)習(xí)springboot的熱潮,說(shuō)什么學(xué)會(huì)了springboot在大街上就可以橫著走、什么有了springboot媽媽再也不擔(dān)心我的編程了、什么BAT都喜歡的框架...聽(tīng)得作者那個(gè)心癢癢的,于是找了個(gè)時(shí)間,下載...

    adie 評(píng)論0 收藏0
  • SpringBoot2.0之五 優(yōu)雅整合SpringBoot2.0+MyBatis+druid+Pa

    摘要:當(dāng)禁用時(shí),所有關(guān)聯(lián)對(duì)象都會(huì)即時(shí)加載。不同的驅(qū)動(dòng)在這方便表現(xiàn)不同。參考驅(qū)動(dòng)文檔或充分測(cè)試兩種方法來(lái)決定所使用的驅(qū)動(dòng)。需要適合的驅(qū)動(dòng)。系統(tǒng)默認(rèn)值是設(shè)置字段和類(lèi)是否支持駝峰命名的屬性。 ??上篇文章我們介紹了SpringBoot和MyBatis的整合,可以說(shuō)非常簡(jiǎn)單快捷的就搭建了一個(gè)web項(xiàng)目,但是在一個(gè)真正的企業(yè)級(jí)項(xiàng)目中,可能我們還需要更多的更加完善的框架才能開(kāi)始真正的開(kāi)發(fā),比如連接池、分...

    hatlonely 評(píng)論0 收藏0
  • 新手也能實(shí)現(xiàn),基于SpirngBoot2.0+ 的 SpringBoot+Mybatis 多數(shù)據(jù)源配

    摘要:下面基于,帶著大家看一下中如何配置多數(shù)據(jù)源。注意版本不一致導(dǎo)致的一些小問(wèn)題。配置配置兩個(gè)數(shù)據(jù)源數(shù)據(jù)庫(kù)和數(shù)據(jù)庫(kù)注意事項(xiàng)在配置數(shù)據(jù)源的過(guò)程中主要是寫(xiě)成和。五啟動(dòng)類(lèi)此注解表示啟動(dòng)類(lèi)這樣基于的多數(shù)據(jù)源配置就已經(jīng)完成了,兩個(gè)數(shù)據(jù)庫(kù)都可以被訪問(wèn)了。 在上一篇文章《優(yōu)雅整合 SpringBoot+Mybatis ,可能是你見(jiàn)過(guò)最詳細(xì)的一篇》中,帶著大家整合了 SpringBoot 和 Mybatis...

    shiina 評(píng)論0 收藏0
  • SpringBoot非官方教程 | 第七篇:SpringBoot開(kāi)啟聲明式事務(wù)

    摘要:準(zhǔn)備階段以上一篇文章的代碼為例子,即整合,上一篇文章是基于注解來(lái)實(shí)現(xiàn)的數(shù)據(jù)訪問(wèn)層,這篇文章基于的來(lái)實(shí)現(xiàn),并開(kāi)啟聲明式事務(wù)。創(chuàng)建實(shí)體類(lèi)數(shù)據(jù)訪問(wèn)層接口層用戶(hù)減塊用戶(hù)加塊,聲明事務(wù),并設(shè)計(jì)一個(gè)轉(zhuǎn)賬方法,用戶(hù)減塊,用戶(hù)加塊。 springboot開(kāi)啟事務(wù)很簡(jiǎn)單,只需要一個(gè)注解@Transactional 就可以了。因?yàn)樵趕pringboot中已經(jīng)默認(rèn)對(duì)jpa、jdbc、mybatis開(kāi)啟了事事...

    tyheist 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<