本文介绍如何通过idea创建一个基础的基于原生Springboot+mybatis的应用。
欢迎评论,指正。编写不易,如需转载还请注明出处
第一步:创建项目
选择Spring 初始化 选择服务地址和项目java版本
第二步:设置pom
配置说明和maven一致,不多废话,留意java version本地是否存在,不存在也无法下一步,建议选择Maven POM
第三步:选择组件
常用的选上,剩下的我们可以按需集成。如果不知道组件功能,选中下面有说明,英语不好翻译一下就成
注:如果下一步后出现 Error:connect timed out异常,说明当前网络环境不太好,这种生成方式还是基于https://start.spring.io/生成的。项目删了重新再来一遍就OK。
第四步:修改maven,配置基础环境
刚建好的项目大概是这个样子:
非常的基础、简洁,不用慌。我们一点一点来设置,先搞定maven,Ctrl+Alt+S打开Settings 如果快捷键不好用应该是和别的软件冲突了。冲突了就File -> settings。打开Settings 搜索maven 设置成自己的本地库
重新写下pom文件设置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.first</groupId>
<artifactId>my-first-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>my-first-project</name>
<description>my first framework</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- SpringBoot Start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!--<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>-->
</exclusions>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--通用mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.1.5</version>
</dependency>
<!--pagehelper 分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- 整合redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- SpringBoot End -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Apache -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- 轮询分词 -->
<dependency>
<groupId>com.hankcs</groupId>
<artifactId>hanlp</artifactId>
<version>portable-1.7.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
build下项目,Ctrl+Alt+Shift+S 打开Project Structure界面选择Libraries,这里看到需要的jar全都加载进来了。
第五步:创建配置文件和启动类
projectStartInit通过实现ApplicationRunner接口,实现启动后运行代码
package com.first.start.project;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
/**
* Description:项目启动后执行的代码
* <p>
* ClassName: ProjectStartInit
* date: 2020/8/11 15:47
*
* @author jo.li
* @version 1.0
* @since JDK 1.8
*/
@Component
public class ProjectStartInit implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("hello SpringBoot~!!!!");
}
}
StartMyProject项目启动类,里面两个注解
package com.first.start;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Description:SpringBoot启动类
* <p>
* ClassName: com.first.start.StartMyProject
* date: 2020/8/11 15:42
*
* @author jo.li
* @version 1.0
* @since JDK 1.8
*/
@SpringBootApplication
@MapperScan(basePackages = "com.first.start.*")
public class StartMyProject {
public static void main(String[] args) {
SpringApplication.run(StartMyProject.class, args);
}
}
配置文件
server:
port: 12345
tomcat:
uri-encoding: utf-8
max-http-form-post-size: 104857600
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/wework?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 123456
application:
name: first-project
jackson:
time-zone: GMT+8
# redis:
# host: ip
# port: 端口
# password:
servlet:
multipart:
max-file-size: 30MB
max-request-size: 100MB
freemarker:
cache: false
charset: UTF-8
content-type: text/html; charset=utf-8
expose-request-attributes: true
expose-session-attributes: true
suffix: .html
template-loader-path: classpath:/templates
request-context-attribute: request
check-template-location: true
expose-spring-macro-helpers: true
allow-request-override: true
allow-session-override: true
settings:
classic_compatible: true #处理空值
template_update_delay: 0
datetime_format: yyyy-MM-dd HH:mm:ss
number_format: 0.##
# data:
# mongodb:
# host: ip
# port: 端口
# database: 库名
mvc:
static-path-pattern: /static/**
#mybatis
mybatis:
type-aliases-package: com.first.start.entity
# mybatis 别名扫描
mapper-locations:
- classpath:mapper/*.xml
#日志
logging:
config: classpath:logback-spring.xml
level:
com.first: debug
因为是娱乐项目,所以redis和mongodb就没有加
启动的方式比较多,就不赘述了,多项目启动还是Services界面操作顺手,也是我的习惯操作。
遇到的问题:
1. application.yml配置文件无法正确识别?
试试把项目文件格式设置成UTF-8然后build一下
如果还不能解决,就看看projectStructure中是不是加载了这个配置文件
2. logback-spring.xml日志配置文件加载不上抛异常?
异常 :java.io.FileNotFoundException: class path resource [logback-spring.xml] cannot be resolved to URL be
我是build一下就解决了,如果不能解决,看下org.springframework.util.ResourceUtils第16行