【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
版本要求:jdk1.8以上
框架版本介绍:springboot 2.x
mybatis-plus 3.x
Maven 3.x以上
辅助工具介绍:lombok 简化代码
Swagger-bootstrap-ui 生成接口文档
Yml文件
注意:1、springboot1.x与2.x版本改动较大,具体请阅看官方文档。
官方地址:https://spring.io/projects/spring-boot/
2、mybatis-plus 3.x与2.x 版本改动较大,具体请查阅官方文档。
解释说明:
springboot优点在于简化配置,全家桶自动集成优秀框架,基于注解开发。
综上所有优点,我们选择了lombok,与mybatis-plus ,还有swagger-ui。这三个都是基于注解使用。我认为选择其他的感觉优点鸡肋。
pom.xml文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- web项目需要引入此jar -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot 测试jar -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入Druid依赖,阿里巴巴所提供的数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
<!-- oracle 连接驱动 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>12.1.0.1</version>
</dependency>
<!--简化代码小工具 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
</dependency>
<!--mybatis Plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<!-- Swagger -->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.8.0.RELEASE</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.21</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
application.yml 文件内容:
server:
port: 9000
#servlet:
# context-path: /card
spring:
###文件上传 springboot版本2.0.4
servlet:
multipart:
max-file-size: 200MB
max-request-size: 200MB
enabled: true
datasource:
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@ip:1521:orcl
username: root
password: root
validation-query: "SELECT 'X' FROM DUAL"
max-wait: 10000 #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制
max-idle: 10 #最大空闲数,数据库连接的最大空闲时间。超过空闲时间,数据库连接将被释放。设为0表示无限制
min-idle: 5 ##最小空闲连接:连接池中容许保持空闲状态的最小连接数量,低于这个数量将创建新的连接
max_active: 50 ##连接池的最大数据库连接数。设为0表示无限制
initial-size: 5 #初始化连接:连接池启动时创建的初始化连接数量
test-on-borrow: false
test-while-idle: true
remove_abandoned: true #超过removeAbandonedTimeout时间后,是否进 行没用连接(废弃)的回收(默认为false,调整为true)
remove_abandoned_timeout: 180 #超过时间限制,回收没有用(废弃)的连接(默认为 300秒,调整为180)
time-between-eviction-runs-millis: 18800
pool-prepared-statements: true
#mybatis 配置文件映射
mybatis-plus:
mapper-locations: classpath:/mapper/*Mapper.xml
typeAliasesPackage: com.siyuan.user.model
global-config:
#字段策略 0:"忽略判断",1:"非 NULL 判断可以插入空字符串"),2:"非空判断"
field-strategy: 1
cache-enabled: false
目录结构:
app启动类内容:
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.siyuan.user.mapper")
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
loginController控制层类容:
import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.siyuan.user.mapper.UserMapper;
import com.siyuan.user.model.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping("/login")
@Api(value = "/",description="接口文档")
public class LoginController {
@Resource
UserMapper userMapper;
@GetMapping("/index")
@ApiOperation(value = "用户登录", tags = {"返回用户信息"}, notes = "务必提交json格式")
public String index(){
return "hello word!";
}
@GetMapping("/demo01")
@ApiOperation(value = "demo测试2", tags = {"返回用户信息"}, notes = "务必提交json格式")
public String demo01(){
List<User> list = userMapper.selectList(null);
return list.toString();
}
@GetMapping("/demo02")
@ApiOperation(value = "demo测试3", tags = {"返回用户信息"}, notes = "务必提交json格式")
public String demo02(){
int list = userMapper.findOne();
return list+"";
}
}
mapper dao层接口内容:
public interface UserMapper extends BaseMapper<User> {
public int findOne();
}
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.siyuan.user.mapper.UserMapper">
<select id="findOne" resultType="int">
select 1 from dual
</select>
</mapper>
model层内容:
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("表明")
public class User {
@TableField("字段名称")
private String name;
}
swagger-ui内容:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
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 //在springboot里面专门为了加载配置文件的标签
@EnableSwagger2 //自动加载配置文件
public class SwaggerConfig extends WebMvcConfigurerAdapter {
@Bean
public Docket createH5RestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("h5")
.select()
.apis(RequestHandlerSelectors.basePackage("com.siyuan.user.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("我的接口文档")
.description("这是我的swaggerui生成的接口文档")
.version("1.0.0.0")
.build();
}
//添加ResourceHandler
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("doc.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}
启动App类的main方法,访问:http://localhost:9000/doc.html
如下图:
进入调试接口:
当然也可以直接访问相对于的链接得到相对应的数据。
总结:在集成mybatis-plus的时候需要注意实体类里面的一些注解,@tableName @TableField
常见问题:
1、调用mybatis-plus中自带方法出现方面未绑定错误
解决办法:检查jar是否冲突,检查扫描路径是否正确。
开发中又遇到什么问题可以在下面留言哦 我会回复的。也可以加QQ群:600922504
来源:oschina
链接:https://my.oschina.net/960823/blog/3144547