• Uncategorized
  • 0

Basic use of SpringBoot (integrating SSM)

Hits: 0

Basic use of SpringBoot

Create a Maven project

Introduce a unified parent project and the required launcher in pom.xml

Add the configuration file application.properties of the SpringBoot project under resources—override the default configuration (port number)

Create a bootstrap class – this class must be under the parent package, so that the subpackage-class can be scanned

Integrate SpringMVC

Introduce the launcher

Create Controller class

Configure the interceptor

First, you need to customize an interceptor to implement the HandlerInterceptor interface

Configure the interceptor

Customize a Java configuration class (using @Configuration) to implement the WebMvcConfigurer interface

Test – Browser Access

Integrate data sources

Introduce the starter of jdbc, the driver of mysql

Add four major configurations in application.properties

Integrate Mybatis

Introduce the launcher of Mybatis

Override default configuration

Integrate Universal Mapper

Introduce the launcher

Add an entity class – you need to use the annotation @Table, the name attribute corresponds to the table name

Add the interface of the Mapper layer

Add Service class

Wrote a query all method, a delete method

Overall test

Summarize

general idea

1. Build the basic application of springboot

2. Integrate SpringMVC

3. Integrate data sources

4. Integrate Mybatis, Mapper

Personal summary

If this blog is helpful to you, please remember to leave a message + like + favorite.

Create a Maven project

Introduce the unified parent project and the required launcher in [pom.xml]

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.7.RELEASE</version>
    </parent>

Add the configuration file application.properties of the SpringBoot project under resources— override the default configuration (port number)

/**
 * @Author: Promsing
 * @Date: 2021/5/30 - 22:43
 * @Description : Bootstrap class
 * @version: 1.0
 */
@SpringBootApplication
public class UserApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class,args);
    }
}

Create a bootstrap class — this class must be under the parent package so that subpackages can be scanned – class

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

Integrate SpringMVC

Introduce the launcher

/**
 * @Author: Promsing
 * @Date: 2021/5/30 - 22:36
 * @Description : Controller class of User table
 * @version: 1.0
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService service;

    @GetMapping("test")
    @ResponseBody
    public String test(){
        return "hello User";
    }

    @GetMapping("findAll")
    @ResponseBody
    public String findAll(){
        List<Account> users = service.findAll();
        System.out.println(users);
        return "findAll";
    }

}

Create the Controller class

/**
 * @Author: Promsing
 * @Date: 2021/5/30 - 23:06
 * @Description : mvc interceptor
 * @version: 1.0
 */
@Component
public class MyInterceptor implements HandlerInterceptor {
    /**
     * Pre-method -- Handler executes before execution
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */ 
    @Override 
    public  boolean  preHandle (HttpServletRequest request, HttpServletResponse response, Object handler)  throws Exception {
         //true release 
        //false intercept 
        System.out.println( "The pre-method is executing!" );
         return  true ;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println( "Post method is executing!" );
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println( "Complete method is executing!" );
    }
}

Configure the interceptor

First, you need to customize an interceptor to implement the HandlerInterceptor interface

/**
 * @Author: Promsing
 * @Date: 2021/5/30 - 23:15
 * @Description : configure the interceptor
 * 1.java configuration class
 * 2. Implement WebMvcConfigurer
 * @version: 1.0
 */ 
@Configuration 
public  class  WebMvcConfiguration  implements  WebMvcConfigurer  {
     //Inject custom interceptor 
    @Autowired 
    private MyInterceptor myInterceptor;

    @Override 
    public  void  addInterceptors (InterceptorRegistry registry)  {
         //Pass in the interceptor object to set the path--chain programming 
        registry.addInterceptor(myInterceptor).addPathPatterns( "/**" );
    }
}

Configure the interceptor

Customize a Java configuration class (using @Configuration) to implement the WebMvcConfigurer interface

    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

Test – Browser Access

Integrate data sources

Introduce the starter of jdbc, the driver of mysql

## Specify the database name 
spring.datasource.username =root
 spring.datasource.password =root 
 #Note--note the specified datasource name spring.datasource.data-password 
spring.datasource.url =jdbc:mysql:///ssm
 spring .datasource.driver-class-name =com.mysql.jdbc.Driver

Add four major configurations in application.properties

 <dependency>
       <groupId>org.mybatis.spring.boot</groupId>
       <artifactId>mybatis-spring-boot-starter</artifactId>
       <version>1.3.2</version>
 </dependency>

Integrate Mybatis

Introduce the launcher of Mybatis

<dependency>
     <groupId>tk.mybatis</groupId>
     <artifactId>mapper-spring-boot-starter</artifactId>
     <version>2.0.4</version>
</dependency>

Override default configuration

/**
 * @Author: Promsing
 * @Date: 2021/5/30 - 23:54
 * @Description : Account entity class
 * @version: 1.0
 */
@Table()
public class Account {

    private Integer id;
    private String name;
    private Double money;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ",  + name + '\'' +
                ", money=" + money +
                '}';
    }
}

Integrate Universal Mapper

Introduce the launcher

/**
 * @Author: Promsing
 * @Date: 2021/5/31 - 17:28
 * @Description : DAO interface--Mapper
 * @version: 1.0
 */
@Mapper
public interface UserMapper extends tk.mybatis.mapper.common.Mapper<Account> {

      //Due to the use of Mapper annotations, no other methods need to be written 
}

Add entity class — need to use the annotation @Table, the name attribute corresponds to the table name

/**
 * @Author: Promsing
 * @Date: 2021/5/31 - 0:11
 * @version: 1.0
 */
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public List<Account> findAll(){
       return userMapper.selectAll();
//        return dao.findAll();
    }
    @Transactional  //Integrated transaction -- transaction annotation 
    public  void  deleteUserById (Long id ) {
         this .userMapper.deleteByPrimaryKey(id);
    }

}

Add the interface of the Mapper layer

http://localhost:8888/user/findAll
http://localhost:8888/user/test

Add Service class

Wrote a query all method, a delete method

/**
 * @Author: Promsing
 * @Date: 2021/5/31 - 0:11
 * @version: 1.0
 */
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public List<Account> findAll(){
       return userMapper.selectAll();
//        return dao.findAll();
    }
    @Transactional  //Integrated transaction -- transaction annotation 
    public  void  deleteUserById (Long id ) {
         this .userMapper.deleteByPrimaryKey(id);
    }

}

Overall test

http://localhost:8888/user/findAll
http://localhost:8888/user/test

Summarize

general idea

1. Build the basic application of springboot

Introduce a unified parent project and the required launcher

Add bootstrap class using @SpringBootApplication (this is a composite annotation containing @EnableAutoConfiguration+@ComponentScan+@SpringBootConfiguration)

2. Integrate SpringMVC

Modify the port (server.port=8888)

Add a custom interceptor: implement the HandlerInterceptor interface

Configuration interceptor: customize a configuration class (@Configuration) to implement the WebMvcConfigurer interface

3. Integrate data sources

Introduce jdbc starter, mysql driver

Configure four properties

4. Integrate Mybatis, Mapper

Introduce the launcher

Add @Mapper annotation to interface

mapper inherits Mapper

Personal summary

Spring Boot is a [framework] that simplifies Spring development . It is used to monitor the development of spring applications. The convention is greater than the configuration, and the complexity is simplified. Just run can create an independent, product-level application.

Compared with integrating [SSM] , SpringBoot reduces a lot of configuration files and automatically configures it. There is no need to introduce too many jar packages, only one launcher is needed.

Built-in Tomcat, just start the bootstrap class directly

When we use Spring Boot, we only need to configure the corresponding Spring Boot to use all Spring components. In short, spring boot integrates many excellent frameworks, and we do not need to manually write a bunch of xml configurations ourselves.

after configuration. Essentially, Spring Boot is Spring, and it does Spring Bean configuration that you would do without it.

If this blog is helpful to you, please remember to leave a message + like + favorite .

You may also like...

Leave a Reply

Your email address will not be published.