第一次写文章,写不好的地方还请大家提出建议或意见
一 创建项目
二 建表语句
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for User
-- ----------------------------
DROP TABLE IF EXISTS `User`;
CREATE TABLE `User` (
`id` varchar(20) NOT NULL COMMENT 'ID',
`loginname` varchar(100) DEFAULT NULL COMMENT '登陆名称',
`password` varchar(100) DEFAULT NULL COMMENT '密码',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户';
-- ----------------------------
-- Records of User
-- ----------------------------
INSERT INTO `User` VALUES ('1065182897650143232', 'user', '123456');
INSERT INTO `User` VALUES ('1065184625611116544', 'clm', '654321');
三 代码
Maven依赖
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.1.RELEASE
com.clmcws
spring-boot-mybatis-mvc
0.0.1-SNAPSHOT
spring-boot-mybatis-mvc
Demo project for Spring Boot
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
项目结构:
启动类:
package com.clmcws;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 启动类
*/
@SpringBootApplication
@MapperScan("com.clmcws.dao") //扫描mapper类包的路径
public class SpringBootMybatisMvcApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisMvcApplication.class, args);
}
}
创建实体类:
package com.clmcws.bean;
import lombok.Data;
import java.io.Serializable;
/**
*
* @author Ccc
* @since 2018-11-21
*/
@Data
public class User implements Serializable {
private static final long serialVersionUID = 1L;
/**
* ID
*/
private String id;
/**
* 登陆名称
*/
private String loginname;
/**
* 密码
*/
private String password;
}
编写控制层:
package com.clmcws.controller;
import com.clmcws.bean.User;
import com.clmcws.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* @author Ccc
* @date 2018/12/17 0017 下午 7:49
*/
@RestController
@RequestMapping("/user")
public class testController {
@Autowired
private UserMapper userMapper;
@GetMapping
public Object getAllUser(){
return userMapper.selectAllUser();
}
@PutMapping("/{userId}")
public String updateUser(@PathVariable("userId")Integer userId, @RequestBody User user){
user.setId(userId);
int res = userMapper.updateUserById(user);
if (res>0){
return "修改用户信息成功";
}
return "修改用户信息失败";
}
@DeleteMapping("/{userId}")
public String deleteUser(@PathVariable("userId")Integer userId){
int res = userMapper.deleteUserById(userId);
if (res>0){
return "删除用户信息成功";
}
return "删除用户信息失败";
}
}
持久层接口:
package com.clmcws.dao;
import com.clmcws.bean.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
/**
*
* 用户 Mapper 接口
*
*
* @author Ccc
* @since 2018-11-21
*/
public interface UserMapper {
@Select("select id,loginname,password from User")
List selectAllUser();
@Delete("delete from User where id = #{userId}")
int deleteUserById(@Param("userId") Integer userId);
@Update("update User set loginname = #{loginname},password = #{password} where id = #{id}")
int updateUserById(User user);
}
配置文件:
注意修改为自己的数据库密码
#端口号
server:
port: 8081
#数据源
spring:
application:
name: User
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/spring_boot?useunicode=true&characterEncoding=utf8
username: root
password: 1234
#Mybatis
mybatis:
mapper-locations:
type-aliases-package: com.clmcws.bean
#日志
logging:
level:
com:
clmcws:
dao: debug
``` java
接下来是测试工作:
启动springboot:
看到这些说明已经启动成功了
接下来使用postman测试下结果,也可以使用其他测试工具
修改用户信息成功
源码:https://pan.baidu.com/s/1BXG_kXQF_1irUaiOup3AwQ