Blogger Information
Blog 87
fans 1
comment 0
visits 58337
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
springBoot 入门学习(1)
阿杰
Original
225 people have browsed it

主要内容

初步了解springBoot项目以及如何用idea快速创建springBoot项目

ssm

spring+springMvc+Mybatis

  • 配置繁琐,需要进行大量配置,门槛较高
  • springBoot可以极大简化配置,提高开发效率

mvc

Maven

Maven是一个项目管理工具,可以对Java项目进行自动化的构建和依赖管理

自行下载和安装,idea自带的maven不太好用

springBoot

  • springBoot可以极大简化配置,提高开发效率
  • 不会spring无所谓,只需要掌握java基础

启动springBoot项目

Controller

@Controller:普通控制器,返回视图页面
@RestController: 值返回数据,用于前后端分离

  1. package com.example.helloworld.controller;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class HelloController {
  6. @RequestMapping("/hello")
  7. public String hello(){
  8. return "hello world!";
  9. }
  10. }

开发环境热部署

MyBatis

  • 中安装依赖(pom.xml 文件添加)
  1. <!--MyBatisPlus依赖-->
  2. <dependency>
  3. <groupId>com.baomidou</groupId>
  4. <artifactId>mybatis-plus-boot-starter</artifactId>
  5. <version>3.4.2</version>
  6. </dependency>
  7. <!--mysql驱动依赖-->
  8. <dependency>
  9. <groupId>mysql</groupId>
  10. <artifactId>mysql-connector-java</artifactId>
  11. <version>5.1.47</version>
  12. </dependency>
  13. <!--数据连接池 druid-->
  14. <dependency>
  15. <groupId>com.alibaba</groupId>
  16. <artifactId>druid-spring-boot-starter</artifactId>
  17. <version>1.1.20</version>
  18. </dependency>
  • 配置数据库属性(application.properties文件中添加)
  1. spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
  2. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  3. spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useSSL=false
  4. spring.datasource.username=root
  5. spring.datasource.password=root
  6. mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
  • 创建mapper包(通过接口注解写mysql语句)
  1. @Mapper
  2. public interface UserMapper {
  3. //查询所有用户
  4. @Select("select * from user")
  5. public List<User> find();
  6. }
  • 启动页面加入注解(扫码数据库包)
  1. @MapperScan("com.example.mpdemo.mapper")
  2. public class MpdemoApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(MpdemoApplication.class, args);
  5. }
  6. }
  • 控制器中实例化mapper进行数据库操作
  1. @RestController
  2. public class UserController {
  3. @Autowired
  4. private UserMapper userMapper;
  5. @GetMapping("/user")
  6. public List query(){
  7. List<User> list = userMapper.find();
  8. System.out.println(list);
  9. return list;
  10. }
  11. }
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post