idea 社区版+spring boot+ssm+swagger创建rest api
新手上路,出了好多错,记录一下
1.创建数据库:springBootSsmTest
2.打开IDEA创建 Spring boot项目:File——New——Project——Spring Assistant,下一步,填好包名等信息,下一步
Develop Tool选Lombok
Web选Spring Web
SQL选Jpa,Mysql,Mybatis
3.把java文件夹标识 为Source Root(右键,Mark as Directory As —— Source Root)
4.在com.my.demo下创建package命名为model,然后在这里创建 Java 类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
@Table(name = "tb_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String userName;
private String password;
private String email;
}
5.在resources下创建文件application.yml,删除原先的application.properties,粘帖如下代码:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springbootssmtest?serverTimezone=UTC
username: root
password: 123456
jpa:
hibernate:
ddl-auto: update
show-sql: true
6.运行程序,会自动生成数据表。
7.创建package命名为mapper,添加一个java类,命名为UserMapper
import java.util.List;
public interface UserMapper {
User getUser(Integer id);//根据id获取信息
boolean addUser(User user);//新增
boolean modifyUser(User user);//修改
boolean deleteUser(Integer id);//根据id删除
List<User> getAll();//获取所有
}
8.创建package命名为service,添加一个java类,命名为UserService
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUser(Integer id){
return userMapper.getUser(id);
}
public boolean addUser(User user){
return userMapper.addUser(user);
}
public boolean modifyUser(User user){
return userMapper.modifyUser(user);
}
public boolean deleteUser(Integer id){
return userMapper.deleteUser(id);
}
public List<User> getAll(){
return userMapper.getAll();
}
}
9.创建package命名为controller,创建Java类命名为UserController
@RestController
public class UserController {
@Autowired
private UserService userService;
/**
* 获取默认的数据源信息
* @return
* @throws SQLException
*/
/**
* 根据id获取信息
* @param id
* @return
* @throws SQLException
*/
@ResponseBody
@GetMapping("/user/get/{id}")
public User getUser(@PathVariable("id") Integer id){
return userService.getUser(id);
}
/**
* 获取所有信息
* @return
*/
@ResponseBody
@GetMapping("/user/list")
public List<User> getAll(){
return userService.getAll();
}
/**
* 新增信息
* @param user
* @return
*/
@ResponseBody
@PostMapping("/user/add")
public boolean addUser(@RequestBody User user){
return userService.addUser(user);
}
/**
* 修改信息
* @param user
* @return
*/
@ResponseBody
@PutMapping("/user/put")
public boolean modifyUser(@RequestBody User user){
return userService.modifyUser(user);
}
/**
* 删除信息
* @param id
* @return
*/
@ResponseBody
@DeleteMapping("/user/delete/{id}")
public boolean deleteUser(@PathVariable("id") Integer id){
return userService.deleteUser(id);
}
}
10.添加依赖,Import Change
<!-- 整合Swagger依赖方便 REST测试 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
11.打开主类,添加注解,记得改包名
@MapperScan("com.my.demo.mapper")//自动扫描的Mapper包,如果这里不指定,则需要在每个Mapper接口中添加@Mapper主键(这两种方法二选一)
@EnableSwagger2 //开启Swagger2功能,方便测试RestApi
12.在resources下创建目录mapper,添加一个文件UserMapper,记得修改文件中两处包名。
<?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.my.demo.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.my.demo.model.User">
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="user_name" jdbcType="VARCHAR" property="userName"/>
<result column="password" jdbcType="VARCHAR" property="password"/>
<result column="email" jdbcType="VARCHAR" property="email"/>
</resultMap>
<!-- 根据id获取信息 -->
<select id="getUser" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select * from tb_user t where t.id = #{id}
</select>
<!-- 获取所有信息 -->
<select id="getAll" resultMap="BaseResultMap">
select * from tb_user t
</select>
<!-- 新增 -->
<insert id="addUser" parameterType="user">
insert into tb_user values(default,#{userName},#{password},#{email})
</insert>
<!-- 修改 -->
<update id="modifyUser" parameterType="user">
update tb_user set user_name=#{userName},password=#{password},email=#{email} where id = #{id}
</update>
<!-- 删除 -->
<delete id="deleteUser" parameterType="java.lang.Integer">
delete from tb_user where id = #{id}
</delete>
</mapper>
13.在application.yml中添加,记得改包名,顶格,和spring同一层次。
# Mybatis相关配置
mybatis:
# 对象别名包
type-aliases-package: com.my.demo.model
# Mybatis mapper.xml映射文件配置
mapper-locations:
- classpath:mappers/*.xml
14.运行程序吧。