springboot+mybatis+mysql項目搭建,含示例Demo

寫在前面

本文主要描述瞭如何使用IntelliJ,從零開始搭建一個springboot+mybatis+mysql項目。文章可能還有很多不足,請大家諒解,歡迎大佬提意見。

本文使用的東西

  1. IntelliJ 2019
  2. springboot
  3. mybatis
  4. mysql

1.新建項目

1.打開IntelliJ,選擇“Spring Initializr”,點擊下一步。
在這裏插入圖片描述
2.項目的type固定爲“Maven Project”,其他的包名等可以根據現實情況改動。
在這裏插入圖片描述
3.添加依賴,點擊“web”選中“spring web”添加該依賴,這個是我們web項目的基礎。實際上還要添加MyBatis和mysql的依賴,我們稍後添加。點擊下一步之後,直接點擊“完成”,完成項目創建。創建項目要等待一小段時間,等待依賴下載添加完成。
在這裏插入圖片描述
4.項目創建好之後直接點擊右上角的三角形運行。
在這裏插入圖片描述在這裏插入圖片描述
5.打來瀏覽器,輸入“http://localhost:8080”,出現如下圖,表示項目搭建成功了,但是我們的項目還沒有頁面。
在這裏插入圖片描述

2.添加頁面

2.添加一個“controller”包,該包用於存放控制器
在這裏插入圖片描述
2.在“controller”包中新建一個“HelloController”的類。
在這裏插入圖片描述
3.使用“@Controller”將新建的“HelloController”類註釋爲控制器。
在這裏插入圖片描述
4.添加“hello”方法,使用“@RequestMapping("/hello")”註釋使其監聽到“hello”的請求,使用“@ResponseBody”註釋返回字符串內容,不會用字符串查找jsp頁面或者html頁面。
在這裏插入圖片描述
5.點擊右上角紅色正方形停止運行,再點擊三角形從新啓動,打來瀏覽器輸入“http://localhost:8080/hello”,可以看到我們return的字符串。
在這裏插入圖片描述

3.搭建mvc框架

3.1添加依賴

1.在pom.xml的project標籤中加入以下內容,配置阿里雲代理倉庫

    <repositories>
        <repository>
            <id>maven-ali</id>
            <url>http://maven.aliyun.com/nexus/content/repositories/central</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
                <checksumPolicy>fail</checksumPolicy>
            </snapshots>
        </repository>
    </repositories>

在這裏插入圖片描述
2.添加依賴,在pom.xml文件的“dependencies”標籤下加入下面兩個依賴。

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

在這裏插入圖片描述

3.2 新建數據庫

在mysql中新建一個“springboot””數據庫“user”表,表中包含“uid”和“name”兩個字段,隨便插入幾條數據,等一下用於測試。
在這裏插入圖片描述
在這裏插入圖片描述

3.3 搭建MVC框架

1.在項目中新建以下包和文件
在這裏插入圖片描述
2.文件內容爲
controller”包的“HelloController”接口:

package com.nineya.springboot.controller;

import com.nineya.springboot.entity.User;
import com.nineya.springboot.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {
    @Autowired
    @Qualifier("impl1") //取得bean,指定bean名稱爲impl1
    private HelloService helloService;

    @ResponseBody
    @RequestMapping("/hello")   //監聽hello請求
    public String hello(){
        User user=helloService.getUser(1);  //從Service取得uid爲1的數據
        return "hello "+user.getName()+", uid="+user.getUid()+".";
    }
}

entity”包的“User”類:

package com.nineya.springboot.entity;

//用戶的實體類
public class User {
    private long uid;
    private String name;

    public User() {
    }

    public User(long uid, String name) {
        this.uid = uid;
        this.name = name;
    }

    public long getUid() {
        return uid;
    }

    public void setUid(long uid) {
        this.uid = uid;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", name='" + name + '\'' +
                '}';
    }
}


service”包的“HelloService”接口:

package com.nineya.springboot.service;

import com.nineya.springboot.entity.User;

//服務接口
public interface HelloService {
    User getUser(long uid);  //通過uid取得用戶名稱
}

service.impl”包的“HelloServiceImpl”類,繼承“HelloService”接口:

package com.nineya.springboot.service.impl;

import com.nineya.springboot.mapper.UserMapper;
import com.nineya.springboot.entity.User;
import com.nineya.springboot.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("impl1")    //註釋爲服務,指定bean名稱爲impl1
public class HelloServiceImpl implements HelloService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User getUser(long uid) {
        return userMapper.getUser(uid);
    }
}

mapper”包的“UserMapper”接口:

package com.nineya.springboot.mapper;

import com.nineya.springboot.entity.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper //註釋爲Mapper
public interface UserMapper {
    User getUser(long uid);
}

SpringbootApplication”類添加掃描Mapper接口

package com.nineya.springboot;

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

@MapperScan("com.nineya.springboot.mapper")//使用MapperScan批量掃描所有的Mapper接口
@SpringBootApplication
public class SpringbootApplication {

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

}

resources”的“mapper”下的“UserMapper.xml”文件:

<?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.nineya.springboot.mapper.UserMapper">
    <!-- application.yml中type-aliases-package:未配置則需要用全名-->
    <select id="getUser" resultType="User">
        SELECT * FROM user where uid=#{0}
    </select>
</mapper>

resources”目錄下的“application.yml”文件:

server:
  port: 8080   #配置本服務器的端口爲8080
spring:
  #數據庫連接配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.138.39:3306/springboot?useSSL=false&serverTimezone=CST&allowMultiQueries=true
    username: root
    password: 361654768

#mybatis的相關配置
mybatis:
  #mapper配置文件
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.nineya.springboot.entity
  #開啓駝峯命名
  configuration:
    map-underscore-to-camel-case: true

3.4運行結果

成功取得數據庫數據。
在這裏插入圖片描述

3.5項目源代碼

我把項目源代碼打包放在了百度網盤,需要的話點擊鏈接前往下載》》Demo項目下載地址
提取碼:kzaa

4.總結

本文只是簡單描述了springboot+mybatis+mysql項目搭建,有不清楚的地方歡迎評論留言,看到的我都會回覆的。本文到此結束,有什麼不足的地方請大家不吝指正。

發佈了44 篇原創文章 · 獲贊 29 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章