1. 程式人生 > >Eclipse建立springboot+mybatis+gradle專案

Eclipse建立springboot+mybatis+gradle專案

開發十年,就只剩下這套架構體系了! >>>   

1.新建專案

2.點選下一步,type選擇Gradle

3.然後再點選Next,選擇web,mysql,mybatis

4.點選finish,專案結構如下

5.配置build.gradle檔案

plugins {
    id 'org.springframework.boot' version '2.1.3.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()

    maven { url 'https://repo.spring.io/snapshot' }
    maven { url 'https://repo.spring.io/milestone' }
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter')
    testCompile('org.springframework.boot:spring-boot-starter-test')

    compile 'mysql:mysql-connector-java'
    compile 'org.springframework.boot:spring-boot-devtools'
    //配置mybatis 資料來源
    compile("org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.0")
    testCompile('org.mybatis.spring.boot:mybatis-spring-boot-starter-test:1.3.0')

    //使用 Controller 的時候需要引入 web 包
    compile('org.springframework.boot:spring-boot-starter-web')

}
6.資料庫連線和mybatis配置 application.properties

#基本配置
spring.datasource.url=jdbc:mysql://localhost:9306/wise_secretgarden?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
#使用mysql
spring.jpa.database = mysql
#是否顯示sql語句
spring.jpa.show-sql=true
#mybatis配置
mybatis.typeAliasesPackage=com.example.demo.model 
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

7.在src/main/resources目錄下新建mybatis資料夾,在mybatis資料夾下建立mapper資料夾

8.建好實體類以及介面與實現介面和controller包跟類

9.mapper類

package com.example.demo.dao;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import com.example.demo.model.Coach;

@Mapper
public interface CoachMapper {

     @Select("select * from coach")
     List<Coach>selectAll();
     
     List<Coach>select();
}

10.Coach.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.example.demo.dao.CoachMapper">

<select id="select"  resultType="coach">
select * from coach
</select>
   
</mapper>

11.service介面類

package com.example.demo.service;

import java.util.List;

import com.example.demo.model.Coach;

public interface CoachService {
    List<Coach>selectAll();
    
    List<Coach>select();

}
 

12.service實現類

import java.util.List;

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

import com.example.demo.dao.CoachMapper;
import com.example.demo.model.Coach;

@Service
public class CoachServiceImpl implements CoachService {
    
    @Autowired
    private CoachMapper cMapper;

    @Override
    public List<Coach> selectAll() {
        return cMapper.selectAll();
    }

    @Override
    public List<Coach> select() {
        return cMapper.select();
    }

}
13.controller類

package com.example.demo.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import com.example.demo.model.Coach;
import com.example.demo.service.CoachService;


@RestController
public class TesrController {
    
    @Autowired
    private CoachService coachService;
    
    
    @RequestMapping("/hello")
    public String hello() {
        return "hello world3";
    }

    @RequestMapping("/select")
    public List<Coach> select() {
        return coachService.selectAll();
    }
    
    @RequestMapping("/selects")
    public List<Coach> selects() {
        return coachService.select();
    }
}
 

14.程式入口類

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;

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

@SpringBootApplication
@MapperScan("com.example.demo.dao")
public class Hello2Application {

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

}

執行主入口

在瀏覽器輸入http://localhost:8080/selects

好了