springboot articles] seven. crud springboot in

Based on [springboot articles] VI. The transaction control springboot

springboot of crud

Project Preparation

  1. Create a project day64-springboot-05-crud
  2. Project Configuration Reference the last chapter
  3. New Database day64-springboot-crud
    DROP TABLE IF EXISTS `user`;
    CREATE TABLE `user` (
      `id` int(8) NOT NULL AUTO_INCREMENT,
      `name` varchar(20) DEFAULT NULL,
      `pwd` varchar(20) DEFAULT NULL,
      `age` int(3) DEFAULT NULL,
      `money` double(8,2) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
    
    INSERT INTO `user` VALUES ('1', 'wpj', '123456', '23', '10000.00');
    INSERT INTO `user` VALUES ('2', 'czh', '123456', '22', '10000.00');
    
  4. Create a User entity class
    package com.wpj.bean;
    
    import com.baomidou.mybatisplus.annotations.TableId;
    import com.baomidou.mybatisplus.annotations.TableName;
    import com.baomidou.mybatisplus.enums.IdType;
    import lombok.Data;
    
    @Data
    @TableName("user")
    public class User {
    
        @TableId(type = IdType.AUTO)
        private Integer id;
    
        private String name;
    
        private String pwd;
    
        private Integer age;
        
        private Double money;
    
    }
    
  5. Create a mapper interface and the service and its impl
    package com.wpj.mapper;
    
    import com.baomidou.mybatisplus.mapper.BaseMapper;
    import com.wpj.bean.User;
    
    public interface IUserMapper extends BaseMapper<User> {
    
    }
    
    package com.wpj.service;
    
    import com.wpj.baseservice.IBaseService;
    import com.wpj.bean.User;
    
    public interface IUserService extends IBaseService<User> {
    
    }
    
    package com.wpj.service.impl;
    
    import com.baomidou.mybatisplus.mapper.BaseMapper;
    import com.baomidou.mybatisplus.plugins.Page;
    import com.wpj.baseservice.impl.BaseServiceImpl;
    import com.wpj.bean.User;
    import com.wpj.mapper.IUserMapper;
    import com.wpj.service.IUserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    
        @Autowired
        private IUserMapper iUserMapper;
    
        @Override
        protected BaseMapper<User> getBaseMapper() {
            return iUserMapper;
        }
    }
    
  6. Create a mapper file (also interfaces with the same name)
    <?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.wpj.mapper.IUserMapper">
        
        
    </mapper>
    
  7. application.properties amended as application.yml
    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/day64-springboot-crud
        username: root
        password: 123456
    
    mybatis-plus:
      type-aliases-package: com.wpj.bean
      mapper-locations: classpath:mapper/*.xml
    
  8. Open package scanning
    package com.wpj.day64springboot05crud;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication(scanBasePackages = "com.wpj")
    @MapperScan(basePackages = "com.wpj.mapper")
    public class Day64Springboot05CrudApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(Day64Springboot05CrudApplication.class, args);
        }
    
    }
    
  9. Test
    Here Insert Picture Description
    10. Test environment complete. . . . . . . I started doing CRUD.
Published 42 original articles · won praise 8 · views 2320

Guess you like

Origin blog.csdn.net/TheNew_One/article/details/104083218