Advanced springboot JDBC, Druid, mybatis, swagger, springmvc, mail

Time:2022-5-8

1.Springboot-JDBC

  1. After integrating JDBC with springboot, spring boot starter JDBC is introduced to operate the database through jdbctemplate.

  2. Import dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
  1. Operate the database through jdbctemplate to add, delete, modify and query
@RestController
public class UserController {

    @Resource
    private JdbcTemplate jdbcTemplate;

    //from fhadmin.cn
    @GetMapping("/delete")
    public String delete() {
        String sql = "delete from tb_user where id = 9";
        jdbcTemplate.update(sql);
        return "delete ok";
    }

    @GetMapping("/update")
    public String update() {
        String sql = "update tb_user set name= 'tom9' where id = 9";
        jdbcTemplate.update(sql);
        return "update ok";
    }

    @GetMapping("/insert")
    public String insert() {
        String sql = "insert into tb_user (id,name,age) values (10,'tom10',10)";
        jdbcTemplate.update(sql);
        return "insert ok";
    }

    @GetMapping("/get")
    public List<Map<String, Object>> getList() {
        String sql = "select * from tb_user";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return list;
    }
}

Back to the top

2.Springboot-Druid

  1. Import dependency
<!--  https://mvnrepository.com/artifact/com.alibaba/druid  -->
  1. application. Yaml configure Druid connection pool
spring:
  1. Druid connection pool configuration and druiddatasource binding in yaml
@Configuration
  1. Configure the servlet that accesses Druid
@Configuration
  1. Configure filter for Druid monitoring
@Configuration

Back to the top

3.Springboot-Mybatis

  1. Import dependency
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.0</version>
</dependency>
  1. mapper. XML storage location
mybatis:
  1. Two ways to configure Dao layer interface

    1. Use @ mapper annotation
    @Mapper
    @Repository
    public interface UserDao {
    
        List<User> getList();
    }
    1. Configure @ mappercans on the startup class
    @SpringBootApplication
    @MapperScans({
            @MapperScan("com.my.springboot.dao")
    })
    public class Demo05Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Demo05Application.class, args);
        }
    
    }

Back to the top

4.Springboot-Swagger

  1. Import dependency
<!--  3.0 and springboot integration, direct import starter -- >
  1. to configure
@Configuration
  1. Swagger access address.http://localhost:8080/swagger-ui/index.html
  2. The configuration is only accessible in swagger, Dev and test environments
//from fhadmin. cn
  1. Swagger configuration grouping
//from fhadmin. cn
  1. Swagger annotation
//The entity class will not be scanned. The interface that needs to be scanned and the entity class object can be configured by swagger from fhadmin cn

Back to the top

5.Springboot-SpringMVC

  1. The web The servlet and filter in XML are configured into the container.
@Configuration
  1. Configure view parser

    1. Mode 1
    @Configuration
    1. Mode 2
    public class MyWebMvcConfigurer implements WebMvcConfigurer {
  2. Other configurations

@Configuration
  1. exception handling
@ControllerAdvice

This work adoptsCC agreementThe author, link and reprint of this article must be indicated