版本信息
springboot 2.6.6+gradle7.4.1+mybatis3+swagger2
目录结构
gradle配置文件
plugins {
id 'org.springframework.boot' version '2.6.6'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
//mybatis的*Mappper.xml默认不会被gradle编译,需要添加资源目录
sourceSets {
main {
resources {
srcDirs = ['src/main/java','src/main/resources']
}
}
}
repositories {
mavenLocal()
maven {url 'https://maven.aliyun.com/repository/central'}
maven {url 'https://maven.aliyun.com/repository/public'}
maven {url 'https://maven.aliyun.com/repository/google'}
maven {url 'https://maven.aliyun.com/repository/gradle-plugin'}
maven {url 'https://maven.aliyun.com/repository/spring'}
maven {url 'https://maven.aliyun.com/repository/spring-plugin'}
maven {url 'https://maven.aliyun.com/repository/apache-snapshots'}
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-aop:2.6.6'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-freemarker'
implementation 'org.springframework.boot:spring-boot-starter-mustache'
implementation 'io.springfox:springfox-swagger2:3.0.0'
implementation 'io.springfox:springfox-swagger-ui:2.10.0'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.2'
implementation 'org.springframework.session:spring-session-data-redis'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
application.properties
spring.profiles.active=dev
spring.application.name="yiyi-boot"
#mapper文件路径
mybatis.mapperLocations=classpath*:com/example/yiyijavafull/models/mapper/**/*.xml
#实体路径别名
mybatis.type-aliases-package=com.example.yiyijavafull.models.domain
#输出日志
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis.configuration.mapUnderscoreToCamelCase=true
mybatis.configuration.call-setters-on-nulls=true
application-dev.properties
server.port=8520
spring.datasource.url=jdbc:mysql://ip:3306/study?useUnicode=true&characterEncoding=utf-8&useSSL=false&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.minimum-idle=8
spring.datasource.hikari.auto-commit=false
spring.datasource.hikari.pool-name=yiyiHikariCP
spring.datasource.hikari.validation-timeout=3000
spring.datasource.hikari.connection-timeout=18000
spring.datasource.hikari.idle-timeout=18000
#spring.thymeleaf.suffix=.html
spring.mvc.view.suffix=.html
spring.mustache.enabled=false
spring.thymeleaf.enabled=false
spring.redis.database=2
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
应用入口配置dao 扫包路径
package com.example.yiyijavafull;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.yiyijavafull.models.dao")
public class YiyijavaFullApplication {
public static void main(String[] args) {
SpringApplication.run(YiyijavaFullApplication.class, args);
}
}
swagger配置
package com.example.yiyijavafull;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.yiyijavafull.ctrls"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("接口文档")
.description("spring boot项目")
.version("1.0")
.build();
}
}
controller使用
package com.example.yiyijavafull.ctrls;
import com.example.yiyijavafull.models.domain.Test;
import com.example.yiyijavafull.services.impl.TestServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.net.http.HttpRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Api(tags ="TestRestCtrl",value = "测试")
@RestController
public class TestRestCtrl {
@Autowired
private TestServiceImpl testService;
/**
* 查询list
*/
@ApiOperation("查询")
@GetMapping("/list")
public Object list(){
List<Test> testList = testService.getList();
Map<String,Object> map = new HashMap<>();
map.put("status","ok");
map.put("data",testList);
return map;
}
/**
* 根据ids 批量查询
*/
@ApiOperation("根据ids 批量查询")
@GetMapping("/listByIds")
// public Object listByIds( String [] ids)
public Object listByIds(@RequestParam String [] ids){
System.out.println(ids.length+"get传数组");
List<Test> testList = testService.getByIds(ids);
Map<String,Object> map = new HashMap<>();
map.put("status","ok");
map.put("data",testList);
return map;
}
/**
* 新增
*/
@ApiOperation("新增test")
@PostMapping("/list")
public int addTest(Test test){
int count = testService.addTest(test);
return count;
}
/**
* 修改
*/
@ApiOperation("修改")
@PutMapping("/list")
public int updateTest(@RequestBody Test test){
int count = testService.updateTest(test);
return count;
}
}
使用模版文件Controller
开启模版
spring.mvc.view.suffix=.html //使用html作为模版
spring.mustache.enabled=false//使用mustache
spring.thymeleaf.enabled=false//使用thymeleaf
package com.example.yiyijavafull.ctrls;
import com.example.yiyijavafull.models.domain.Test;
import com.example.yiyijavafull.services.impl.TestServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
public class TestCtrl {
@Autowired
private TestServiceImpl testService;
@RequestMapping("/index")
public String index(){
return "index";//模版文件名
}
@GetMapping("/index2")
public String index2(){
return "index";
}
@RequestMapping("/index3/{id}")
public ModelAndView index3(@PathVariable(name="id") Integer ids){
ModelAndView mv = new ModelAndView();
Map<String,Object> params = new HashMap<>();
params.put("name","hello");
params.put("id",ids);
mv.addObject("data",params);
mv.setViewName("index");//模版文件名
return mv;
}
@RequestMapping("/test")
public ModelAndView testIndex(@RequestParam(defaultValue = "default",required = false) String name){
ModelAndView mv = new ModelAndView();
Map<String,Object> params = new HashMap<>();
params.put("test",name);
mv.setViewName("/test");//模版文件名
return mv;
}
}
dao文件
package com.example.yiyijavafull.models.dao;
import com.example.yiyijavafull.models.domain.Test;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface TestDao {
public List<Test> listAll();
public Test getById(Integer id);
public List<Test> getByIds(@Param("idList") String [] ids);
public int insertUser(Test test);
public int updateUser(Test test);
}
service文件
package com.example.yiyijavafull.services.impl;
import com.example.yiyijavafull.models.dao.TestDao;
import com.example.yiyijavafull.models.domain.Test;
import com.example.yiyijavafull.services.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class TestServiceImpl implements TestService {
@Autowired
private TestDao testDao;
@Override
public List<Test> getList() {
List<Test> testList = testDao.listAll();
return testList;
}
@Override
public Test getById(Integer id) {
Test test = testDao.getById(id);
return test;
}
@Override
public List<Test> getByIds(String[] ids) {
List <Test> testList = testDao.getByIds(ids);
return testList;
}
@Override
public int insertUser(Test test) {
int count = testDao.insertUser(test);
return count;
}
@Override
public int addTest(Test test) {
int count = testDao.insertUser(test);
return count;
}
@Override
public int updateTest(Test test) {
int count = testDao.updateUser(test);
return count;
}
}
package com.example.yiyijavafull.services;
import com.example.yiyijavafull.models.domain.Test;
import java.util.List;
public interface TestService {
public List<Test> getList();
public Test getById(Integer id);
public List<Test> getByIds(String [] ids);
public int insertUser(Test test);
public int addTest(Test test);
public int updateTest(Test test);
}
aop
package com.example.yiyijavafull.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class TestAop {
@Pointcut("execution(public * com.example.yiyijavafull.ctrls.*.*(..))")
public void log(){
System.out.println("@Pointcut切点==========");
}
@Before("log()")
public void doBefore(JoinPoint jp){
System.out.println(" @Before class method"+jp);
}
@After("log()")
public void doAfter(JoinPoint jp){
System.out.println(" @After class method: "+jp);
}
@AfterReturning("log()")
public void doAfterReturning(JoinPoint jp){
System.out.println("@AfterReturning: "+jp);
}
@AfterThrowing(pointcut = "log()")
public void doAfterThrowing(JoinPoint jp){
System.out.println("@AfterThrowing: "+jp);
}
@Around("log()")
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Long startTime = System.currentTimeMillis();
System.out.println("@Around ---before");
Object result = proceedingJoinPoint.proceed();
long time = System.currentTimeMillis() - startTime;
System.out.println("@around ---after"+time+"ms"+"result="+result);
return result;
}
}