简介
SpringBoot它基于Spring4.0设计,是由 Pivotal 公司提供的框架。
SpringBoot 基于 Spring 开发。不仅继承了Spring框架原有的优秀特性,它并不是用来替代 Spring 的解决方案,而和 Spring 框架紧密
结合进一步简化了Spring应用的整个搭建和开发过程。其设计目的是用来简化 Spring 应用的初始搭建以及开发过程怎么简化的呢?就是
通过提供默认配置等方式让我们更容易使用。
参考资料
SpringBoot 2.X
简介及搭建
简介
SpringBoot它基于Spring4.0设计,是由 Pivotal 公司提供的框架。
2014 年 4 月发布 Spring Boot 1.0 基于Spring4.0
2018 年 3 月 Spring Boot 2.0发布 基于Spring 5.0。
SpringBoot基于Spring开发。不仅继承了Spring框架原有的优秀特性,它并不是用来替代 Spring 的解决方案,而和 Spring 框架紧密
结合进一步简化了Spring应用的整个搭建和开发过程。其设计目的是用来简化 Spring应用的初始搭建以及开发过程怎么简化的呢?就是
通过提供默认配置等方式让我们更容易使用。
关于 SpringBoot 有一句很出名的话就是约定大于配置。采用 Spring Boot 可以大大的简化开发模式,它集成了大量常用的第三方库配
置,所有你想集成的常用框架,它都有对应的组件支持,例如 Redis、MongoDB、Dubbo、kafka,ES等等。SpringBoot 应用中这些第
三方库几乎可以零配置地开箱即用,大部分的 SpringBoot 应用都只需要非常少量的配置代码,开发者能够更加专注于业务逻辑。另外
SpringBoot通过集成大量的框架使得依赖包的版本冲突,以及引用的不稳定性等问题得到了很好的解决。
简化Spring应用开发的一个框架;
对整个企业级开发技术栈的一个大整合build anything;
J2EE开发的一站式解决方案;
配置文件和自动配置原理
热部署与日志
Swagger
EasyCode
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| ##导入宏定义 $!{define.vm}
##保存文件(宏定义) #save("/entity", ".java") ##包路径(宏定义) #setPackageSuffix("entity") ##自动导入包(全局变量) $!{autoImport.vm} import java.io.Serializable; import java.util.Date;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;
@Data @AllArgsConstructor @NoArgsConstructor @TableName("$!{tableInfo.obj.name}") @ApiModel(value="$!{tableInfo.name}对象", description="$!{tableInfo.comment}") public class $!{tableInfo.name} implements Serializable {
private static final long serialVersionUID = $!tool.serial();
#foreach($column in $tableInfo.fullColumn) #if(${column.comment}) #end #if(${column.comment})@ApiModelProperty(value = "${column.comment}")#end #if($column.name.equals('id'))@TableId(type = IdType.AUTO)#end private $!{tool.getClsNameByFullName($column.type)} $!{column.name}; #end }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| ##导入宏定义 $!{define.vm} ##设置表后缀(宏定义) #setTableSuffix("Mapper") ##保存文件(宏定义) #save("/mapper", "Mapper.java") ##包路径(宏定义) #setPackageSuffix("mapper") import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.springframework.stereotype.Component; import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
@Component public interface $!{tableName} extends BaseMapper<$!{tableInfo.name}> {
}
|
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 36 37 38 39 40 41 42
| ##引入mybatis支持 $!{mybatisSupport.vm}
##设置保存名称与保存位置 $!callback.setFileName($tool.append($!{tableInfo.name}, "Mapper.xml")) $!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mapper"))
##拿到主键 #if(!$tableInfo.pkColumn.isEmpty()) #set($pk = $tableInfo.pkColumn.get(0)) #end
<?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="$!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper">
<resultMap type="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}" id="$!{tableInfo.name}Map"> #foreach($column in $tableInfo.fullColumn) <result property="$!column.name" column="$!column.obj.name" jdbcType="$!column.ext.jdbcType"/> #end </resultMap>
<!-- 批量插入 --> <insert id="insertBatch" keyProperty="$!pk.name" useGeneratedKeys="true"> insert into $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end) values <foreach collection="entities" item="entity" separator=","> (#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end) </foreach> </insert> <!-- 批量插入或按主键更新 --> <insert id="insertOrUpdateBatch" keyProperty="$!pk.name" useGeneratedKeys="true"> insert into $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end) values <foreach collection="entities" item="entity" separator=","> (#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end) </foreach> on duplicate key update #foreach($column in $tableInfo.otherColumn)$!column.obj.name = values($!column.obj.name) #if($velocityHasNext), #end#end </insert>
</mapper>
|
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
| ##导入宏定义 $!{define.vm}
##设置表后缀(宏定义) #setTableSuffix("Service")
##保存文件(宏定义) #save("/service", "Service.java")
##包路径(宏定义) #setPackageSuffix("service")
import com.baomidou.mybatisplus.extension.service.IService; import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
public interface $!{tableName} extends IService<$!tableInfo.name> {
}
|
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
| ##导入宏定义 $!{define.vm}
##设置表后缀(宏定义) #setTableSuffix("ServiceImpl")
##保存文件(宏定义) #save("/service/impl", "ServiceImpl.java")
##包路径(宏定义) #setPackageSuffix("service.impl")
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import $!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper; import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}; import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service; import org.springframework.stereotype.Service;
@Service("$!tool.firstLowerCase($tableInfo.name)ServiceImpl") public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Mapper, $!{tableInfo.name}> implements $!{tableInfo.name}Service {
}
|
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 36 37 38 39 40 41 42 43 44 45
| ##导入宏定义 $!{define.vm}
##设置表后缀(宏定义) #setTableSuffix("Controller")
##保存文件(宏定义) #save("/controller", "Controller.java")
##包路径(宏定义) #setPackageSuffix("controller")
##定义服务名 #set($serviceName = $!tool.append($!tool.firstLowerCase($!tableInfo.name), "Service"))
##定义实体对象名 #set($entityName = $!tool.firstLowerCase($!tableInfo.name))
import $!{tableInfo.savePackageName}.service.impl.$!{tableInfo.name}ServiceImpl; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import io.swagger.annotations.Api;
@RestController @Slf4j @RequestMapping("/$!tool.firstLowerCase($!tableInfo.name)") @Api(tags = "$!{tableInfo.comment}") public class $!{tableName} { @Autowired private $!{tableInfo.name}ServiceImpl service; }
|
快速搭建
+
- 调用顺序:遵循Controller–Service接口–ServiceImpt实现类–Mapper接口模式;
- 那么在Service接口有多个ServiceImpt实现类的情况,就需要指定参数名来选择哪个ServiceImpt实现类了。
- 只有一个实现类,Controller层用
@Autowired
注入 Service接口,如果有多个实现类在实现类中用@Service定义实现类名, Controller层用 @Resource(name=”PCIimpt2”) 指定
- 都用 注入都注入 Service接口,有多个就用@Resource指定名称 只有一个就不指定 多态
1 2 3 4 5 6 7 8 9 10
| Service层(此时有两个接口实现类) @Service("PCIImpt1") class PCIImpt1 imeplements PCI{} @Service("PCIimpt2") class PCIImpt2 imeplements PCI{} Controller层 @Resource(name="PCIimpt2") private PCI pci;
|