先说一句,这个是spring+maven+mybatis项目框架的搭建文章。
强烈推荐大家使用springboot搭建项目,搭建过程会轻松很多,减少很多解决配置问题的时间。
详情请移步:使用springboot搭建maven+mybatis视频讲解
以下是使用idea搭建spring+maven+mybatis项目框架 正文:
需要准备的环境:
idea ,jdk1.8,Maven 3.2.3(我自己的可能比较老,建议下载相对新的版本),mysql-8.0.17(需最低jdk1.8),MYSQLworkbench
提前将idea与Maven、jdk配置好。注:配置完ide红线报错没关系!可以run。
一、首先使用idea新建一个Maven webapp项目
点击下一步,Finish,空项目搭建完成。
二、搭建目录结构
这里列出的是搭建完了之后所有的目录和文件,先把目录文件建起来:
这里的目录建好之后还需要设置一下,让idea识别目录作用,选择File-Project Structure,设置方法如图:
三、配置文件内容
- pom.xml
pom里使用的版本与自身配置需要对应。如果没有1.8jdk建议安装,否则容易和很多新版的应用不兼容。
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>gloria.learn</groupId>
<artifactId>SpringLearn</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<!-- 设置项目编码编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- spring版本号 -->
<spring.version>4.3.5.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.4.1</mybatis.version>
</properties>
<repositories>
<repository>
<id>nexus-aliyun</id>
<name>nexus-aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<!-- java ee -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- 实现slf4j接口并整合 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.2</version>
</dependency>
<!-- JSON -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>
<!-- 数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
<scope>runtime</scope>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis/spring整合包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>