• springboot mybatis搭建


    非常easy直接写,没有搭建成分

    1、目录

    2、

    @RestController
    public class UserController {
        @RequestMapping("/hello")
        public String index() {
            return "Hello World";
        }
    
        @Autowired
        UserService userService;
    
        @RequestMapping(value = "/list", method = RequestMethod.GET)
        public List<User> getAccounts() {
            return userService.getUserList();
        }
    }

    3、

    @Mapper
    public interface UserMapper {
    
        @Select("SELECT * FROM tb_user WHERE id = #{id}")
        User getUserById(Integer id);
    
        @Select("SELECT * FROM tb_user")
        public List<User> getUserList();
    
    }

    4、

    @Service
    public class UserService {
        @Autowired
        private UserMapper userMapper;
    
        public List<User> getUserList() {
            return userMapper.getUserList();
        }
    }

    6、

    public class User {
        private int id;
        private String username;
        private int age;
    
        public User(int id, String username, int age) {
            this.id = id;
            this.username = username;
            this.age = age;
        }
        public User(Long id, String username, Long age) {
            this.id = Math.toIntExact(id);
            this.username = username;
            this.age = Math.toIntExact(age);
        }
      //不知道为啥报错,就按照提示来呗
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }

    7、

    application.properties

    spring.datasource.url=jdbc:mysql://localhost:3306/test
    spring.datasource.username=root
    spring.datasource.password=123456
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  • 相关阅读:
    CF 852E--Casinos and travel(思维)
    Sigma Sigma Cnk --- CF 575H
    CF1316E-Team Building(dp+贪心)
    cf1316C--Primitive Primes
    cf1323--Count Subrectangles(思维)
    CF252B--鸽巢原理
    HDU3652--容斥+数位dp
    DP--HDU4571(Floyd+dp)
    HDU4219--Randomization?(树形概率DP)
    HDU5945--Fxx and Game dp(单调队列优化)
  • 原文地址:https://www.cnblogs.com/cnchengv/p/9884142.html
Copyright © 2020-2023  润新知