前提条件
JAVA安装
Gradle安装
创建项目


配置项目设置
指定自己的gradle的安装位置,以及仓库位置(用户主目录)

用户主目录: Gradle仓库目录用于存储全局配置属性和初始化脚本以及缓存和日志文件。
结构

build.gradle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| plugins { id 'java' id 'org.springframework.boot' version '2.7.7' id 'io.spring.dependency-management' version '1.0.15.RELEASE' }
group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8'
configurations { compileOnly { extendsFrom annotationProcessor } }
repositories { mavenLocal() mavenCentral()
}
dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.3.0' compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.mysql:mysql-connector-j' annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' }
tasks.named('test') { useJUnitPlatform() }
|

其他
application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| server: port: 9874
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: root url: jdbc:mysql://127.0.0.1:3306/demo?characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10&useSSL=false
mybatis: mapper-locations: classpath*:/mapper/**/*Mapper.xml type-aliases-package: com.**.entity configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl map-underscore-to-camel-case: true
|
UserMapper.xml
1 2 3 4 5 6 7 8
| <?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.gradledemo.dao.UserDao"> <select id="users" resultType="com.example.gradledemo.entity.UserEntity"> SELECT * FROM t_user limit 10 </select> </mapper>
|
UserDao
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.example.gradledemo.dao;
import com.example.gradledemo.entity.UserEntity; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update;
import java.util.List;
@Mapper public interface UserDao { @Select("select count(*) from t_user") String userCount();
List<UserEntity> users();
@Update("UPDATE t_user SET age=#{add} WHERE id=#{id}") Integer upDateAge(int add,int id);
@Select("SELECT age FROM t_user WHERE id=#{id}") Integer getdateAge(int id); }
|
UserEntity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.example.gradledemo.entity;
import lombok.*;
import java.util.Date; import java.util.Objects;
@Data public class UserEntity { private Long id; private String name; private String email; private String phone; private String password; private Integer age; private Integer sex; private Integer status; private Date creationTime; private String site; }
|
UserController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.example.gradledemo.controller;
import com.example.gradledemo.dao.UserDao; import com.example.gradledemo.entity.UserEntity; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController public class UserController {
@Autowired private UserDao userDao; @GetMapping("/user") public ResponseEntity<List<UserEntity>> getUser(){ return ResponseEntity.ok(userDao.users()) ; }
}
|
GradleDemoApplication
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.example.gradledemo;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class GradleDemoApplication {
public static void main(String[] args) { SpringApplication.run(GradleDemoApplication.class, args); }
}
|