dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.5.2'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.5.4'
implementation group: 'org.springframework.boot', name: 'spring-boot-devtools', version: '2.5.4'
implementation 'org.projectlombok:lombok:1.18.8'
annotationProcessor 'org.projectlombok:lombok:1.18.8'
implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.23'
implementation group: 'com.alibaba', name: 'druid', version: '1.2.6'
implementation group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.4.3'
implementation group: 'com.alibaba', name: 'fastjson', version: '1.2.76'
}
- 配置yml
server:
port: 8080
spring:
application:
name: gradle-demo01
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.96.192:3306/dbtest?useUnicode=true&characterEncoding=UTF-8
username: root
password: 123456
mvc:
view:
suffix: ".html"
mybatis:
configuration:
map-underscore-to-camel-case: true
mapperLocations: classpath:mapper/*.xml
logging:
level:
com:
chnq:
gradledemo01: debug
- 配置启动类
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.chnq.gradledemo01.mapper")
public class GradleDemo01Application {
public static void main(String[] args) {
SpringApplication.run(GradleDemo01Application.class, args);
}
}
- 创建数据库
- 编写配置类
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@Configuration
@MapperScan("com.chnq.gradledemo01.mapper")
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
- 编写实体类、mapper、service、controller