Spring-boot + Mybatis整合

項(xiàng)目結(jié)構(gòu)圖如下:

Paste_Image.png

下面分步驟一一講解:
第一步信粮,在pom.xml中添加相應(yīng)依賴:
<pre>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>spring-boot-sample-helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-boot-sample-helloworld</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    
    <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.0.0</version>
    </dependency>
    
    <dependency>  
        <groupId>mysql</groupId>  
        <artifactId>mysql-connector-java</artifactId>  
    </dependency> 
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project>

</pre>

第二步棕洋,新建application.properties
<pre>
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/jeesite
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

server.port=8080
server.session.timeout=10
server.tomcat.uri-encoding=UTF-8

mybatis-config路徑

mybatis.config=classpath:mybatis-config.xml

mapper文件路徑

mybatis.mapperLocations=classpath:mappers/*.xml

domain object's package

mybatis.typeAliasesPackage=com.example.springbootsamplehelloworld.bean
</pre>

<pre>
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

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

}

</pre>

第三步,創(chuàng)建bean,mapper,service,controller
<pre>
package com.example.springbootsamplehelloworld.bean;

import java.util.Date;

public class AppMessage {
private String id;

private String message;

private Date senddate;

public AppMessage(){
    
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id == null ? null : id.trim();
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message == null ? null : message.trim();
}

public Date getSenddate() {
    return senddate;
}

public void setSenddate(Date senddate) {
    this.senddate = senddate;
}

}

</pre>

<pre>
package com.example.springbootsamplehelloworld.mapper;

import java.util.List;

import com.example.springbootsamplehelloworld.bean.AppMessage;

public interface AppMessageMapper {

int deleteByPrimaryKey(String id);

int insert(AppMessage record);

int insertSelective(AppMessage record);

AppMessage selectByPrimaryKey(String id);

int updateByPrimaryKeySelective(AppMessage record);

int updateByPrimaryKey(AppMessage record);

List<AppMessage> selectAll();

List<AppMessage> getMessById(String id);

public void init();

}

</pre>

<pre>
package com.example.springbootsamplehelloworld.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.springbootsamplehelloworld.bean.AppMessage;
import com.example.springbootsamplehelloworld.mapper.AppMessageMapper;

@Service
public class AppMessageService {

@Autowired
private AppMessageMapper mapper;

public List<AppMessage> getMessage(){
     List<AppMessage> list = new ArrayList<AppMessage>();
     list.add(mapper.selectByPrimaryKey("1"));
     //list = mapper.selectAll();
     return list;
}

public List<AppMessage> getAllMessage(){
     List<AppMessage> list = new ArrayList<AppMessage>();
     list = mapper.selectAll();
     return list;
}

public int addMessage(AppMessage appMessage) {
    return mapper.insert(appMessage);
}

public List<AppMessage> getMessageById(String id) {
    return mapper.getMessById(id);
}

public int delMessage(String id) {
    return mapper.deleteByPrimaryKey(id);
}

}

<pre>

<pre>
package com.example.springbootsamplehelloworld.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.springbootsamplehelloworld.bean.AppMessage;
import com.example.springbootsamplehelloworld.service.AppMessageService;

@RestController
@RequestMapping("/appmessage")
public class APPMessageController {

@Autowired
private AppMessageService service;

@RequestMapping("/getThree")
public List<AppMessage> getThreeForMessage(){
    
    List<AppMessage> list = service.getMessage();        
    return list;
}

@RequestMapping("/getAll")
public List<AppMessage> getAllMessage(){
    
    List<AppMessage> list = service.getAllMessage();
    int num = list.size();
    if(null!=list && num>3){
        for (int i = 0; i < num-3; i++) {
            list.remove(0);
        }
    }
    return list;
}

@RequestMapping("/getByID")
public List<AppMessage> getMessageById(@RequestParam("id") String id){
    List<AppMessage> list = service.getMessageById(id);
    int num = list.size();
    if(null!=list && num>5){
        for (int i = 0; i < num-5; i++) {
            list.remove(0);
        }
    }
    return list;
}

@RequestMapping(value = "/add",method = RequestMethod.POST)
public int addMessage(@RequestBody AppMessage appMessage){
    return service.addMessage(appMessage);
}

@RequestMapping(value="/delMessageById")
public int delMessageById(@RequestParam("id") String id){
        return service.delMessage(id);
}

}

</pre>

需要注意的地方:
1卵慰,bean中需要有無參構(gòu)造器(如果存在有參構(gòu)造器抬吟,則編譯器不會(huì)自動(dòng)添加無參構(gòu)造器)

Paste_Image.png

第四步渐行,訪問

Paste_Image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市叠骑,隨后出現(xiàn)的幾起案子李皇,更是在濱河造成了極大的恐慌,老刑警劉巖宙枷,帶你破解...
    沈念sama閱讀 219,589評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件掉房,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡慰丛,警方通過查閱死者的電腦和手機(jī)卓囚,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,615評(píng)論 3 396
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來诅病,“玉大人哪亿,你說我怎么就攤上這事∠桶剩” “怎么了蝇棉?”我有些...
    開封第一講書人閱讀 165,933評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)芥永。 經(jīng)常有香客問我篡殷,道長(zhǎng),這世上最難降的妖魔是什么埋涧? 我笑而不...
    開封第一講書人閱讀 58,976評(píng)論 1 295
  • 正文 為了忘掉前任板辽,我火速辦了婚禮奇瘦,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘劲弦。我一直安慰自己耳标,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,999評(píng)論 6 393
  • 文/花漫 我一把揭開白布邑跪。 她就那樣靜靜地躺著麻捻,像睡著了一般。 火紅的嫁衣襯著肌膚如雪呀袱。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,775評(píng)論 1 307
  • 那天郑叠,我揣著相機(jī)與錄音夜赵,去河邊找鬼。 笑死乡革,一個(gè)胖子當(dāng)著我的面吹牛寇僧,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播沸版,決...
    沈念sama閱讀 40,474評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼嘁傀,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了视粮?” 一聲冷哼從身側(cè)響起细办,我...
    開封第一講書人閱讀 39,359評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蕾殴,沒想到半個(gè)月后笑撞,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,854評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡钓觉,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,007評(píng)論 3 338
  • 正文 我和宋清朗相戀三年茴肥,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片荡灾。...
    茶點(diǎn)故事閱讀 40,146評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡瓤狐,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出批幌,到底是詐尸還是另有隱情础锐,我是刑警寧澤,帶...
    沈念sama閱讀 35,826評(píng)論 5 346
  • 正文 年R本政府宣布逼裆,位于F島的核電站郁稍,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏胜宇。R本人自食惡果不足惜耀怜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,484評(píng)論 3 331
  • 文/蒙蒙 一恢着、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧财破,春花似錦掰派、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,029評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至俊性,卻和暖如春略步,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背定页。 一陣腳步聲響...
    開封第一講書人閱讀 33,153評(píng)論 1 272
  • 我被黑心中介騙來泰國打工趟薄, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人典徊。 一個(gè)月前我還...
    沈念sama閱讀 48,420評(píng)論 3 373
  • 正文 我出身青樓杭煎,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國和親卒落。 傳聞我的和親對(duì)象是個(gè)殘疾皇子羡铲,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,107評(píng)論 2 356

推薦閱讀更多精彩內(nèi)容

  • 對(duì)Mybatis注解方式有興趣的,可以查看我的另一篇:Spring boot Mybatis 整合(注解版)[ht...
    Winter_Chen閱讀 15,259評(píng)論 2 23
  • 首先聲明:第一次寫儡毕。如果有什么問題的話也切!順著網(wǎng)線過來打我啊~~開始吧。 第一步:創(chuàng)建maven項(xiàng)目 對(duì)maven不...
    點(diǎn)帝閱讀 675評(píng)論 0 5
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理妥曲,服務(wù)發(fā)現(xiàn)贾费,斷路器,智...
    卡卡羅2017閱讀 134,672評(píng)論 18 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,827評(píng)論 6 342
  • 【0821今日剽悍】 用力過猛的后果就是后勁不足檐盟!應(yīng)該要學(xué)會(huì)勞逸結(jié)合褂萧,不要過度消耗自己的精力和意志力,要不然恢復(fù)起...
    好聽的暖陽閱讀 327評(píng)論 2 0