1.开发工具:IDEA,JDK1.8,MYSQL
1.创建工程File--->New--->Project,回车
2.选择Spring Initializr
3.点击next
4.选择依赖项
5.完成创建
6.准备建表语句,本次使用mysql,建表语句如下:
CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(32) NOT NULL, `password` varchar(50) NOT NULL, `realname` varchar(32) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1
7.看下刚才创建项目的依赖pom.xml
<?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.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lym</groupId>
<artifactId>springboot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-demo</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-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
8.创建实体类,如下:
package com.lym.springbootdemo.entity;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.io.Serializable;
@Getter
@Setter
@ToString
public class User implements Serializable {
private Integer id;
private String userName;
private String passWord;
private String realName;
}
9.创建mapper接口
package com.lym.springbootdemo.mapper;
import com.lym.springbootdemo.entity.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper {
User Sel(int id);
int deleteUserById(int id);
int saveUser(User user);
int updateUserById(User user);
}
10.创建service层接口
package com.lym.springbootdemo.service;
import com.lym.springbootdemo.entity.User;
import com.lym.springbootdemo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
UserMapper userMapper;
public User Sel(int id){
return userMapper.Sel(id);
}
public int deleteUserById(int id){
return userMapper.deleteUserById(id);
}
public int saveUser(User user){
return userMapper.saveUser(user);
}
public int updateUserById(User user){
return userMapper.updateUserById(user);
}
}
11.创建controller
package com.lym.springbootdemo.controller;
import com.lym.springbootdemo.entity.User;
import com.lym.springbootdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* @Description: 用户类的controller类
* @Author: lym
* @CreateDate: 2020/3/20 13:12
*/
@RestController
@RequestMapping("/testboot")
public class UserController {
@Autowired
private UserService userService;
/**
* 根据用户ID,查询用户
* @param id
* @return
*/
@RequestMapping("getUser/{id}")
public String getUser(@PathVariable int id){
User user = userService.Sel(id);
if(user !=null){
return userService.Sel(id).toString();
}else{
return null;
}
}
/**
* 删除用户
* @param id
* @return
*/
@RequestMapping("deleteUserById/{id}")
//@PathVariable spring3.0后新增的功能,接收请求路径中的占位符的值。即,可以将URL中占位符参数{xxx},如题中的{id},绑定到处理器类的方法形参中@PathVariable(“xxx“)
public int deleteUserById(@PathVariable int id){
return userService.deleteUserById(id);
}
/**
* 增加用户
* @param user
* @return
*/
@RequestMapping(value="/saveUser",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public int saveUser(@RequestBody(required = false) User user){
return userService.saveUser(user);
}
}
12.修改启动类,增加扫描注解
package com.lym.springbootdemo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.lym.springbootdemo.mapper") //扫描的mapper
@SpringBootApplication
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
13.准备编写配置文件application.yml
spring:
profiles:
active: dev
14.编写application-dev.ym
server:
port: 8080
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapping/*.xml
type-aliases-package: com.lym.springbootdemo.entity
#showSql
logging:
level:
com:
lym:
springbootdemo:
mapper: debug
15.最后完成mapper SQL文件的编写
<?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.lym.springbootdemo.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.lym.springbootdemo.entity.User">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="userName" jdbcType="VARCHAR" property="userName" />
<result column="passWord" jdbcType="VARCHAR" property="passWord" />
<result column="realName" jdbcType="VARCHAR" property="realName" />
</resultMap>
<insert id="saveUser" parameterType="com.lym.springbootdemo.entity.User">
insert into user (username,password,realname) values (#{userName},#{passWord},#{realName})
</insert>
<update id="updateUserById" parameterType="com.lym.springbootdemo.entity.User">
update user set name=#{userName} where id = #{id}
</update>
<delete id="deleteUserById" parameterType="int">
delete from user where id=#{id}
</delete>
<select id="Sel" resultType="com.lym.springbootdemo.entity.User">
select * from user where id = #{id}
</select>
</mapper>
16.根据建表语句,完成数据库的创建。
17.自测阶段
18.经过测试,符合预期结果,可以完成数据的查询,删除,保存操作
19.以上是框架的搭建过程。小白初次练习,如有问题,烦请指正。