搭建SpringMVC-SpringBoot-Mybatis框架

好记性不如烂笔头,本次将SpringMVC-SpringBoot-Mybatis三大框架整合做一次记录

一、框架的POM依赖如下所示:**
1、SpringBoot父项目依赖

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

2、web-starter模块

<!--Web模块,此模块包含了tomcat 9.0.27-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

3、Mybatis模块、Mysql数据库模块

<!--Mybatis-->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.2</version>
    </dependency>
    <!--Mysql连接-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.47</version>
    </dependency>
    <!--JDBC-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

二、配置
1、首先需要在application.properties中,配置好相关参数:

server.port=8080

#编码格式
server.tomcat.uri-encoding=utf-8

#数据库相关配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

#session生命周期
server.servlet.session.timeout=30m

#Mapper.xml文件位置
mybatis.mapper-locations=classpath:mapping/*Mapper.xml
mybatis.type-aliases-package=com.hik.entity

2、在resources包中建立mapper.xml映射文件存放目录
mapper.xml
3、在源码目录中新建mapper目录,用来存放mapper接口,并在接口类上添加相关注解

import com.hik.entity.User;
import com.hik.entity.UserAndStudent;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper 
public interface UserMapper {

    User selByName(String name);
    User selByName(User user);
    UserAndStudent sel_1(String anyString);
    void insertUser(List userList);
    List<User> findAllUser();
}

4、建立启动类AppStart.java:

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.hik.mapper") //扫描mapper接口
@SpringBootApplication 
public class AppStart {
    public static void main(String[] args) {
        SpringApplication.run(AppStart.class,args);
        System.out.println("SUCCESS...");
    }
}

至此,SpringBoot快速构建就完成了。

你可能感兴趣的:(spring,boot,mybatis)