Eclipse创建springboot+mybatis+gradle项目
- 2019 年 10 月 8 日
- 筆記
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

好了,这样就成功啦