springboot+mybatis-plus+mysql项⽬完整搭建(⼗分详尽!)
springboot 接⼝项⽬完整搭建
⽹上有很多关于SpringBoot的教程和博⽂,但是有的确实不够详细,因为业务场景不同,所以所需的⼯程结构也不同,最近开始参与⼀些Java的项⽬开发,涉及的都是⽐较常⽤和基础的,在此我把详细搭建项⽬的过程记录下来,包括热部署配置,l配置⽂件,依赖项及entity层,mapper层,controller层,service层的代码编写等过程,以备后续查看。
⽂章⽬录
1. IDEA安装SpringAssistant插件
如果IDEA是社区版,就要先安装⼀个叫SpringAssistant插件,⽤它来创建Spring项⽬⽐较省事,当然也可以不装它,⽤其他⽅式创建SpringBoot项⽬。安装的⽅法很简单,就是先搜索出来,点安装,安装之后需要重启IDEA。
2. 创建SpringBoot项⽬
2.1 新建项⽬
点击新建Project后,⽤SpringAssistant插件创建SpringBoot项⽬,如图
注意新建项⽬时,必须要连接互联⽹。之后输⼊⼀写必备的项⽬名称等信息,就创建完成了。
2.2 配置热部署及端⼝
在域名统计⽬录下创建包⽬录controller,entity,service,config,分别⽤于放置控制器,实体,服务,配置等类⽂件。
将原来⽣成的配置⽂件更名为 l,打开后配置服务启动的端⼝号,配置热部署
<!--devtools热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
IDEA配置热部署
1. “File”->“Settings”->“Build,Execution,Deplyment”->“Compiler”
选中打勾"Build project automatically"
2. 组合键:“Shift+Ctrl+Alt+/”“选择"Registry”,
选中打勾"compiler.automake.allow.when.app.running"
server:
port:8088
spring:
devtools:
restart:
enabled:true  #设置开启热部署
additional-paths: src/main/java #重启⽬录
exclude: WEB-INF/**
freemarker:
cache:false    #页⾯不加载缓存,修改即时⽣效
编写接⼝运⾏项⽬
在controller下创建第⼀个控制器HomeController
@Controller
@RequestMapping("/test")
public class HomeController {
@RequestMapping("/hello.json")
public@ResponseBody String Hello(){
return"Hello World!";
}
}
测试
修改类–>保存:应⽤会重启
修改配置⽂件–>保存:应⽤会重启
修改页⾯–>保存:应⽤不会重启,但会重新加载,页⾯会刷新
3. 配置Mysql及mybatis-plus
⾸先也是配置安装依赖项,这⾥采⽤druid数据源类库包。
<!-- mvnrepository/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<!-- mvnrepository/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
然后再在配置⽂件l中配置数据源及mybatis-plus的相关配置
datasource:
name: sp
driver-class-name: sql.cj.jdbc.Driver
url: jdbc:mysql://:3906/mylearnlab?characterEncoding=utf8&useSSL=false username: xxx
password: xxxxxxxx
type: com.alibaba.druid.pool.DruidDataSource
filters: stat
maxActive:20
initialSize:1
maxWait:60000
minIdle:1
timeBetweenEvictionRunsMillis:60000
minEvictableIdleTimeMillis:300000
validationQuery: select 'x'
testWhileIdle:true
testOnBorrow:false
testOnReturn:false
poolPreparedStatements:true
maxOpenPreparedStatements:20
# 配置打印sql语句
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
最后还要在程序⼊⼝main⽅法上添加注解
@SpringBootApplication(scanBasePackages ={"cn.zhousonglin"})
@MapperScan("cn.zhousonglin")
public class MyspringlearnApplication {
public static void main(String[] args){
SpringApplication.run(MyspringlearnApplication.class, args);
}
}
4. 编写service及mapper层
在域名主⽬录下新建mapper包和service包⽬录,在service⽬录下在新建impl包⽬录,具体的⼯程⽬录结构最后实现如下:4.1 项⽬⼯程结构
4.2 entity实体层代码
@Data
@TableName(value ="tb_user")
public class TbUser {
@TableId("id")
private int id;
@TableField("name")
private String name;
@TableField("age")
private int age;
@TableField("roleId")
private int roleId;
@TableField(exist =false)
private String role;
}
4.3 mapper 层代码
public interface TbUserMapper extends BaseMapper<TbUser>{
}
4.4 service 接⼝层代码
public interface IUserService {
List<TbUser>GetAllUsers();
}
4.5 service的实现层impl代码
@Service
public class UserService implements IUserService {
@Autowired
private TbUserMapper userMapper;
@Override
public List<TbUser>GetAllUsers(){
List<TbUser> userList = userMapper.selectList(null);
return  userList;
}
}
5. 编写controller层及运⾏
@Controller
@RequestMapping("/test")
public class HomeController {
@Autowired
private IUserService userService;
@RequestMapping("/getAllUsers.json")
public@ResponseBody Object GetAllUsers(){
return  userService.GetAllUsers();
}
mysql配置与安装过程}
6. 简单打包部署
⾸先要先配置l中Maven插件的⼊⼝类名称
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>learnlab.MyspringlearnApplication</mainClass> </configuration>
</plugin>
</plugins>
</build>
其次在Main⽅法中增加构建代码

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。