美文网首页Spring Boot学习
2.Spring Boot整合Mybatis进行CRUD

2.Spring Boot整合Mybatis进行CRUD

作者: 长生藤 | 来源:发表于2019-03-18 18:12 被阅读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. 进行编码
    • 编写entity实体类,其中属性对应数据库中表的数据
    @Data
    public class User {
        private Long userId;
        private String mobile;
        private String password;
        private String username;
        private String avatar;
    }
    
    • 在Mapper中编写需要使用到的sql语句,在结果集中column为数据库属性名,property为idea中属性名,还有就是sql语句在最后空一格为了方便拼接
    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);
    }
    
    • service中写个实现mapper中方法的接口
    public interface UserService {
        List<User> selectAll();
    
        User getOne(long userId);
    
        void delete(long userId);
    
        User  insert(User user);
    
        void updata(User user);
    }
    
    • 在serviceimpl中实现service中接口的方法
    @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);
        }
    }
    
    • 对serviceImpl中的方法进行测试,以便排查错误

    • 在controller中写入RESTful请求

    @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);
        }
    
    }
    
    • 打开启动类,记得加上注释,不然可能找不到mapper,最后运行
    @SpringBootApplication
    @MapperScan("com.springboot.mybatis.mapper")
    public class SpringBootMybatisApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringBootMybatisApplication.class, args);
        }
    
    }
    
    • 出现以下结果说明运行成功


      启动.png
    1. 使用postman验证
      其中有几点要注意
    • 要注意动作如get,put,delete,post等的区别
    • 结果为200即为成功
    • 使用put和post时要注意一下几处


      post.png

    相关文章

      网友评论

        本文标题:2.Spring Boot整合Mybatis进行CRUD

        本文链接:https://www.haomeiwen.com/subject/mlubmqtx.html