快速搭建SpringBoot+Mybatis的项目

开发环境: jdk1.8,maven3.6,MySQL5.6

开发工具: idea

前置条件:会搭建SSM框架

1、搭建项目:

步骤如下,不详细讲了,以截图的形式展示:

快速搭建SpringBoot+Mybatis的项目_第1张图片

快速搭建SpringBoot+Mybatis的项目_第2张图片

这之后直接next,fininsh就创建好了。

其次,在pom.xml里面配置依赖。



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.3.RELEASE
         
    
    com.example
    spitngboottest
    1.0-SNAPSHOT
    war
    spitngboottest
    Demo project for Spring Boot

    
        1.8
    

    
        
        
            org.projectlombok
            lombok
            1.16.16
        

        
        
            com.alibaba
            druid
            1.1.12
        


        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        

        
        
            mysql
            mysql-connector-java
            runtime
        


        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


2、SpringBoot的配置

在项目创建之后,可以发现基本的结构与ssm框架没啥区别,那区别到底在哪儿呢。

2.1 appellation配置文件

记得ssm框架整合时有各种application-xxx.xml,繁琐的配置让初学者头大好久。SpringBoot提倡“习惯优于配置”的理念,再也不会为了配置文件繁琐而头疼。此外,在项目中你可以发现没有了web.xml这个配置文件。那初始化的配置文件都在哪里配置呢?答案就在下面:

在我们创建完项目后,在/src/main/resources目录下有一个叫application.propertites的配置文件。然后我们不采用这种文件,而是采用application.yml这种格式的配置文件。两者的区别,不详细讲了,自行百度。(注意:SpringBoot项目默认会扫描/src/main/resources目录下名字叫application的配置文件,如果同时存在application.yml,application.properties,那么默认扫描application.properites)。SpringBoot项目的配置文件就是在这个东西下配置的。那么我现在配置一些基础的吧:

server:
  #端口号
  port: 8080
spring:
  #数据源配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: user
    password: 123
    url: jdbc:mysql://localhost:3306/db_springboot?serverTimezone=UTC&characterEncoding=utf-8&useUnicode=true
mybatis:
  #映射配置文件路径
  mapper-locations: classpath:mybatis/mapper/*.xml
  #核心配置文件路径
  config-location: classpath:mybatis/mybatis-config.xml

上面的默认配置基本是这样的一个格式,也就是yml格式。(也可以是yaml,自行百度yaml格式,口语读作丫妹儿,滑稽= =)。

2.2 Mybatis配置文件

我主要着重将SpringBoot,Mybatis就快速的过了。配置文件目录为/src/main/resources/mybatis/mybatis-config.xml





    
    
    
        
        
        
    

3、项目的目录结构(以具体user例子讲述)

3.1 总体结构

快速搭建SpringBoot+Mybatis的项目_第3张图片

3.2 UserController

package com.example.spitngboottest.controller;

import com.example.spitngboottest.model.UserDo;
import com.example.spitngboottest.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Package com.example.spitngboottest.controller
 * @date: 2019-02-22
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    
    @GetMapping("/user/selectAll")
    public List selectAll() {
        return userService.selectAll();
    }
}

3.3 UserService & UserServiceImpl

package com.example.spitngboottest.service;

import com.example.spitngboottest.model.UserDo;

import java.util.List;

/**
 * @Package com.example.spitngboottest.service
 * @date: 2019-02-22
 */
public interface UserService {
    
    /**
     * 查询所有用户
     * @param 
     * @return List
     * @throws
     * @date 2019-02-22 
     */
    List selectAll();
}
package com.example.spitngboottest.service;

import com.example.spitngboottest.mapper.UserMapper;
import com.example.spitngboottest.model.UserDo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Package com.example.spitngboottest.service
 * @date: 2019-02-22
 */
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;

    @Override
    public List selectAll() {
        return userMapper.selectAll();
    }
}

3.4 UserMapper & UserMapper.xml

package com.example.spitngboottest.mapper;

import com.example.spitngboottest.model.UserDo;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Package com.example.spitngboottest.mapper
 * @date: 2019-02-22
 */
@Component
public interface UserMapper {
    
    /**
     * 查询所有用户
     * @param 
     * @return List
     * @throws       
     * @date 2019-02-22
     */
    List selectAll();
}




    

4、SpringBoot启动类(重点)

Springboot项目与spring项目不同之处,它有自己的启动类,不打包放在tomcat容器中也可以运行。可能会好奇,spring可以通过application-xxx.xml进行项目扫包,SpringBoot怎么扫包。

首先先将SpringBoot启动类有一个@SpringBootApplication注解。有了这个注解,SpringBoot项目就会默认去扫描当前包及其子包下的所有文件。注意,这里有个坑,mapper包扫不到。所以为了扫描mapper包,需要用到@MapperScan,并且在mapper接口上写上@Component。这是解决办法之一,还有别的方法,自行百度。

快速搭建SpringBoot+Mybatis的项目_第4张图片                   

 快速搭建SpringBoot+Mybatis的项目_第5张图片

5、运行截图

运行启动类,输入url,截图如下:

快速搭建SpringBoot+Mybatis的项目_第6张图片

6、SpringBoot常用的注解

@SpringBootApplication:一般用于启动类,包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解。

@ComponentScan:组件扫描,可自动发现和装配一些Bean。可以扫描带有@Component 注解的类以及它的的子注解@Service、@Repository、@Controller的类。

@EnableAutoConfiguration:开启自动配置。

@Configuration:等同于spring的XML文件,进行配置。

@ImportResource:用来加载第三方xml配置文件。

@Value:注入properties配置的属性的值。

你可能感兴趣的:(SpringBoot)