SpringBoot 快速入门
1.1 快速构建SpringBoot
1.1.1 选择构建类型
1.1.2 项目描述
1.1.3 指定版本和依赖
1.1.4 创建一个controller 类
1.1.5 效果如下
1.1.6 文件下的src目录
springboot 常用注解 @Configuration 和 @Bean
1.1.6 热加载
1.1.7 修改setting 中配置
1.1.8 重新构建项目
SpringBoot Mybatis
2.1 快速构建SpringBoot
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.first</groupId>
<artifactId>project_first</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>project_first</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--热加载-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- druid连接-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--json-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!-- -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.6.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
2.1 Mybatis 注解增删查改
2.1.1 controller 控制层
/**
* @Description:查找所有用户
*/
@RequestMapping("/getAlluser")
public String findAll() {
System.out.println("查询所有");
List<StudentBean> userList = userService.findAll();
for (StudentBean user : userList) {
System.out.println("用户:" + user);
}
Map<String, Object> hash = new HashMap<>();
hash.put("data", userList);
hash.put("code", 200);
String data_json = JSON.toJSONString(hash);
return data_json;
}
2.1.2 mapper mapper
增删改查 @Insert @Delete @Update @Select 还需要在启动类添加@MapperScan 注解
@SpringBootApplication
@MapperScan("com.first.project_first.*")
public class ProjectFirstApplication {
public static void main(String[] args) {
SpringApplication.run(ProjectFirstApplication.class, args);
}
}
@Mapper
public interface StudentMapper {
List<StudentBean> findAll();
@Select("select * from easyuser")
List<StudentBean> findAlls();
@Select("select * from studentuser where id = #{id}")
StudentBean findById(@Param("id") int id);
@Insert("insert into studentuser(username,create_time) values(#{username},#{create_time})")
int insert(@Param("username") String name, @Param("create_time") String create_time);
@Update("update studentuser set username=#{username} where id=#{id}")
int update(StudentBean user);
@Delete("delete from studentuser where id=#{id}")
int delete(int id);
}
2.1.3 service UserService
@Service
public class UserService {
@Autowired
private StudentMapper userMapper;
public List<StudentBean> findAll() {
return userMapper.findAlls();
}
public StudentBean findById(int id) {
return userMapper.findById(id);
}
public int insert(String name, String createtime) {
return userMapper.insert(name, createtime);
}
public int update(StudentBean user) {
return userMapper.update(user);
}
public int delete(int id) {
return userMapper.delete(id);
}
}
2.1.4 resource 资源文件夹下的
<?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.first.project_first.mapper.StudentMapper">
<!-- List<StudentBean> findAll();-->
<select id="findAll" resultType="com.first.project_first.bean.StudentBean">
select * from easyuser
</select>
<!---->
</mapper>
2.1.5 数据库配置 application-dev.yml
server:
# 项目端口
port: 8091
servlet:
# 项目路径
context-path: /
# 连接数据库的信息
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///easyproject?serverTimezone=UTC
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
mvc:
# 视图的前缀和后缀
view:
prefix: /WEB-INF/
suffix: .jsp
# mybatis配置
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.qf.firstspringboot.entity
configuration:
map-underscore-to-camel-case: true
# 查看sql语句
logging:
level:
com.qf.firstspringboot.mapper: DEBUG
2.1.6 运行结果
代码连接地址 gitee.com/guaishoubro…