初步了解springBoot项目以及如何用idea快速创建springBoot项目
spring+springMvc+Mybatis
Maven是一个项目管理工具,可以对Java项目进行自动化的构建和依赖管理
自行下载和安装,idea自带的maven不太好用
@Controller:普通控制器,返回视图页面
@RestController: 值返回数据,用于前后端分离
package com.example.helloworld.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello world!";
}
}
<!--MyBatisPlus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<!--mysql驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--数据连接池 druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version>
</dependency>
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useSSL=false
spring.datasource.username=root
spring.datasource.password=root
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
@Mapper
public interface UserMapper {
//查询所有用户
@Select("select * from user")
public List<User> find();
}
@MapperScan("com.example.mpdemo.mapper")
public class MpdemoApplication {
public static void main(String[] args) {
SpringApplication.run(MpdemoApplication.class, args);
}
}
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/user")
public List query(){
List<User> list = userMapper.find();
System.out.println(list);
return list;
}
}