1.新建項目java
2.點擊下一步,type選擇Gradlemysql
3.而後再點擊Next,選擇web,mysql,mybatisweb
4.點擊finish,項目結構以下spring
5.配置build.gradle文件sql
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'java'
}數據庫
apply plugin: 'io.spring.dependency-management'apache
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'瀏覽器
repositories {
mavenCentral()mybatis
maven { url 'https://repo.spring.io/snapshot' }
maven { url 'https://repo.spring.io/milestone' }
}app
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
好了,這樣就成功啦