Spring Boot学习

2.Spring Boot整合Mybatis进行CRUD

2019-03-18  本文已影响0人  长生藤
  1. 创建一个新的模块
    注意模块选择,并选择jdk版本至少为8


    模块选择.png
填元数据.png

此处可以添加自身所需依赖


添加依赖.png 完成构建.png
  1. 准备工作
    添加所需依赖
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <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>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.39</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
            <optional>true</optional>
        </dependency>
    </dependencies>

在application.properties中添加配置,其中中username和password为自身数据库的用户名和密码(以my sql为例)

## 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/db_spring?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=**
spring.datasource.password=**
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#指定实体类映射的包
mybatis.type-aliases-package=com.springboot.mybatis.entity

构建包结构


包的结构.png
  1. 进行编码
@Data
public class User {
    private Long userId;
    private String mobile;
    private String password;
    private String username;
    private String avatar;
}
public interface UserMapper {
    @Results({@Result(column = "user_id",property = "userId"),
            @Result(column = "mobile",property = "mobile"),
            @Result(column = "password",property = "password"),
            @Result(column = "username",property = "username"),
            @Result(column = "avatar",property = "avatar")
    })
    @Select("SELECT * FROM t_sys_user ")
    List<User> selectAll();

    @Results({@Result(column = "user_id",property = "userId"),
            @Result(column = "mobile",property = "mobile"),
            @Result(column = "password",property = "password"),
            @Result(column = "username",property = "username"),
            @Result(column = "avatar",property = "avatar")
    })
    @Select("SELECT * FROM t_sys_user WHERE user_id = #{userId} ")
    User getOne(Long userId);

    @Delete("DELETE FROM t_sys_user WHERE user_id =#{userId} ")
    void delete(Long userId);

    @Insert("INSERT INTO t_sys_user(mobile,password,username,avatar)"+
            "VALUES(#{mobile},#{password},#{username},#{avatar}) ")
    void insert(User user);

    @Update("UPDATE t_sys_user SET password=#{password},avatar=#{avatar} WHERE user_id=#{userId} ")
    void updata(User user);
}
public interface UserService {
    List<User> selectAll();

    User getOne(long userId);

    void delete(long userId);

    User  insert(User user);

    void updata(User user);
}
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    @Override
    public User getOne(long userId) {
        return userMapper.getOne(userId);
    }

    @Override
    public void delete(long userId) {
          userMapper.delete(userId);
    }

    @Override
    public User insert(User user) {
        userMapper.insert(user);
        return user;
    }

    @Override
    public void updata(User user) {
        userMapper.updata(user);
    }
}
@RestController
@RequestMapping(value = "/api")
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping(value = "/users",method = RequestMethod.GET)
    public List<User> selectAll(){
        return userService.selectAll();
    }

    @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
    public User getOne(@PathVariable("id") long id){
        return userService.getOne(id);
    }

    @RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)
    public void deleteUser(@PathVariable("id") long id){
        userService.delete(id);
    }

    @RequestMapping(value = "/user",method = RequestMethod.POST)
    public User addUser(@RequestBody User user){
        return userService.insert(user);
    }

    @RequestMapping(value = "/user",method = RequestMethod.PUT)
    public void update(@RequestBody User user){
        userService.updata(user);
    }

}
@SpringBootApplication
@MapperScan("com.springboot.mybatis.mapper")
public class SpringBootMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootMybatisApplication.class, args);
    }

}
  1. 使用postman验证
    其中有几点要注意
上一篇下一篇

猜你喜欢

热点阅读