SpringBoot Basics - Getting Started Project

Create project

Create a Spring Boot project

overall project directory  

  • java 

①controller layer: control layer, responsible for method calls

②entity layer: entity layer, which stores Java Beans (attributes + get, set, toString methods)

③mapper layer: docking with mapper in resources

④Service layer: business layer, which handles various services

⑤Application class: equivalent to the main method of the entire project

  • resources

①mapper: store the Mapper.xml file, related to the database

②application.properties: is the configuration file of Spring Boot

SpringBoot integrates SpringMVC

①Open pom.xml, introduce SpringMVC dependencies, put the following code in the <dependencies></dependencies> tag ( can not overwrite other code inside ), CTRL+SHIFT+O refresh dependencies

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

SpringBoot integrates Mybatis

①Open pom.xml, introduce Mybatis dependencies, put the following code in the <dependencies></dependencies> tag ( can not overwrite other code inside ), CTRL+SHIFT+O refresh dependencies

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

②Change the extension of the configuration file application to .yml or .yaml [ Generally use .yml/.yaml, because of low redundancy, easy operation, and ease of use ]

 ③ Write a configuration file

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/QCBY_DB?useUnicode=true&characterEncoding=utf-8
    username: root
    password: 密码
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  mapper-locations: classpath:mapper/*.xml  #对应resources/mapper映射xml文件所在路径
  type-aliases-package: com.qcby.entity  #对应实体类路径

practice test 

① The database creates a user table and inserts data

CREATE TABLE `user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '用户名',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

②In the entity package, create the User entity class

public class User {
    private Long id;
    private String name;

    //get,set,toString方法
}

③Create the UserMapper interface under the mapper package and add the @Mapper annotation

@Mapper
public interface UserMapper {

}

④Create a UserMapper.xml file in the resources/mapper directory to query the data in the database

<?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.qcby.mapper.UserMapper">
    <select id="findAll" resultType="com.qcby.entity.User">
        select * from user
    </select>
</mapper>

⑤ Write the UserMapper interface

@Mapper
public interface UserMapper {
    public List<User> findAll();
}

⑥ Under the service package, create the UserService interface

public interface UserService {
    public List<User> findAll();
}

⑦Create the impl package under the service package, then create the UserServiceImpl class, inherit the UserService interface, and add the @Service annotation

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

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

⑧ Under the controller package, create the UserController class and add the @RestController annotation

@RestController//等价于@Controller + @ResponseBody
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserServiceImpl service;

    @RequestMapping("findAll")
    public List<User> findAll(){
        return this.service.findAll();
    }
}

⑨ Edit the SpringBootProjectApplication class and add the @MapperScan annotation

@MapperScan("com.qcby.mapper")
@SpringBootApplication
public class SpringBootProjectApplication {

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

}

⑩Start the project and visit http://localhost:8080/user/findAll in the browser 

Guess you like

Origin blog.csdn.net/weixin_46899412/article/details/123452123