IntelliJ IDEA 整合 Spring Boot 2.1.1 + mybatis + mysql 8.0.13 使用application.properties配置文件 微服务框架整合

IntelliJ IDEA 整合 Spring Boot 2.1.1 + mybatis + mysql 8.0.13 使用application.properties配置文件

 

一、环境介绍

            1、JDK:java version "1.8.0_60"


                             Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
                             Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

            2、IntelliJ IDEA: IntelliJ IDEA 2018.3.1 x64

            3、mysql 8.0.13 x64位(阿里云服务器上安装好的,安装教程后续出)

二、建项目

1、建项目

            IntelliJ IDEA 整合 Spring Boot 2.1.1 + mybatis + mysql 8.0.13 使用application.properties配置文件 微服务框架整合_第1张图片

IntelliJ IDEA 整合 Spring Boot 2.1.1 + mybatis + mysql 8.0.13 使用application.properties配置文件 微服务框架整合_第2张图片IntelliJ IDEA 整合 Spring Boot 2.1.1 + mybatis + mysql 8.0.13 使用application.properties配置文件 微服务框架整合_第3张图片IntelliJ IDEA 整合 Spring Boot 2.1.1 + mybatis + mysql 8.0.13 使用application.properties配置文件 微服务框架整合_第4张图片IntelliJ IDEA 整合 Spring Boot 2.1.1 + mybatis + mysql 8.0.13 使用application.properties配置文件 微服务框架整合_第5张图片IntelliJ IDEA 整合 Spring Boot 2.1.1 + mybatis + mysql 8.0.13 使用application.properties配置文件 微服务框架整合_第6张图片IntelliJ IDEA 整合 Spring Boot 2.1.1 + mybatis + mysql 8.0.13 使用application.properties配置文件 微服务框架整合_第7张图片

建好后的项目结构                                                                            建包:Controller、mapper、model、service、impl

                                                                                                                及resources下的 mapper文件夹,建好后如下

IntelliJ IDEA 整合 Spring Boot 2.1.1 + mybatis + mysql 8.0.13 使用application.properties配置文件 微服务框架整合_第8张图片                                        IntelliJ IDEA 整合 Spring Boot 2.1.1 + mybatis + mysql 8.0.13 使用application.properties配置文件 微服务框架整合_第9张图片

2、建相应的文件

       A、UserController.java

package com.example.demo.Controller;

import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping(value = "/user/")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping(value = "all")
    public List findAllUser() {

        return userService.findAllUser();
    }
}

B、UserMapper.java

package com.example.demo.mapper;

import com.example.demo.model.User;

import java.util.List;

public interface UserMapper {

    List selectAllUser();
}

C、User.java

package com.example.demo.model;

public class User {
    private Integer userId;

    private String userName;

    private String password;

    private String phone;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName == null ? null : userName.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone == null ? null : phone.trim();
    }
}

D、UserService.java

package com.example.demo.service;

import com.example.demo.model.User;

import java.util.List;

public interface UserService {

    List findAllUser();
}

E、UserServiceImpl.java

package com.demo.service.impl;

import com.demo.mapper.UserMapper;
import com.demo.model.User;
import com.demo.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service(value = "userService")
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;//这里会报错,但是并不会影响

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

F、UserMapper.xml





    

完成后的结构

IntelliJ IDEA 整合 Spring Boot 2.1.1 + mybatis + mysql 8.0.13 使用application.properties配置文件 微服务框架整合_第10张图片

三、准备数据

          1、使用IntelliJ IDEA连接数据库:

                       IntelliJ IDEA 整合 Spring Boot 2.1.1 + mybatis + mysql 8.0.13 使用application.properties配置文件 微服务框架整合_第11张图片 

                       IntelliJ IDEA 整合 Spring Boot 2.1.1 + mybatis + mysql 8.0.13 使用application.properties配置文件 微服务框架整合_第12张图片 

                      IntelliJ IDEA 整合 Spring Boot 2.1.1 + mybatis + mysql 8.0.13 使用application.properties配置文件 微服务框架整合_第13张图片

         2、建表 和 数据

                   点这里用SQL建表                                                 

                   IntelliJ IDEA 整合 Spring Boot 2.1.1 + mybatis + mysql 8.0.13 使用application.properties配置文件 微服务框架整合_第14张图片

CREATE TABLE user(
  userId INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  userName VARCHAR(255) NOT NULL ,
  password VARCHAR(255) NOT NULL ,
  phone VARCHAR(255) NOT NULL
) ENGINE=INNODB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8;
INSERT INTO test.user (userId, userName, password, phone) VALUES (1, '1', '1', '1');
INSERT INTO test.user (userId, userName, password, phone) VALUES (2, '2', '2', '2');

四、修改配置

A、DemoApplication.java 启动类 加上:@MapperScan("com.example.demo.mapper")
package com.example.demo;

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

@SpringBootApplication
@MapperScan("com.example.demo.mapper")//TODO 将项目中对应的mapper类的路径加进来就可以了
public class DemoApplication {

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

}

看一眼pom.xml,个人认为这个不需要动什么,开始都忘记贴出来了



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.1.RELEASE
         
    
    com.example
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

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


B、application.properties 配置文件

注意:mybatis.mapper-locations  的值  classpath要带*,冒号后不要有空格

#------------------配置虚拟文件路径--start------------------------------
#windows时启用
spring.http.multipart.location = E:/item/demo/src/main/resources/
#linux时启用
#spring.http.multipart.location  =  /root/item/demo/
spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/, classpath:/static/, classpath:/public/, file:${spring.http.multipart.location}
#-----------------------------------------配置虚拟文件路径--end-----------------------------------------------
#端口号
server.port = 8080
server.tomcat.uri-encoding = UTF-8
#-----------------------------------------数据库--start------------------------------------------------------
#基础配置
spring.datasource.name = lol
spring.datasource.url = jdbc:mysql://47.106.000.000:3306/lol?useUnicode = true&characterEncoding = UTF-8&allowMultiQueries = true
spring.datasource.username = root
spring.datasource.password = **********
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
spring.datasource.max-idle = 10
spring.datasource.initial-size = 5
spring.datasource.druid.filters = stat

#获取连接等待超时时间
spring.datasource.max-wait = 60000
#间隔多久进行一次检测,检测需要关闭的空闲连接
spring.datasource.time-between-eviction-runs-millis = 60000
#一个连接在池中最小生存的时间
spring.datasource.min-evictable-idle-time-millis = 300000
spring.datasource.validation-query = SELECT 'x'
spring.datasource.test-while-idle = true
spring.datasource.test-on-borrow = false
spring.datasource.test-on-return = false
#打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
spring.datasource.pool-prepared-statements = false
spring.datasource.max-pool-prepared-statement-per-connection-size = 20
#----------------------------------------------数据库--end----------------------------------------------------
#----------------------------------------------mybatis--start----------------------------------------------------
## 说明:classpath*:mapper/*.xml  classpath要带*,冒号后不要有空格
mybatis.mapper-locations = classpath*:mapper/*.xml
mybatis.type-aliases-package =com.demo.model

五、测试

IntelliJ IDEA 整合 Spring Boot 2.1.1 + mybatis + mysql 8.0.13 使用application.properties配置文件 微服务框架整合_第15张图片

如果你觉得此文章帮到你了,可以给我们熬到周日晚上1点多一点鼓励,拿起手机打开支付宝,扫描下方二维码,让马云给我们奖励金,谢谢

一、扫描红码领取红包,二、使用红包付款,三、扫描蓝码付款,付款比红包多一分即可,谢谢您的鼓励!

 

IntelliJ IDEA 整合 Spring Boot 2.1.1 + mybatis + mysql 8.0.13 使用application.properties配置文件 微服务框架整合_第16张图片

你可能感兴趣的:(工具设置,框架整合)