配置IDEA/創(chuàng)建springboot+maven項目

打開并配置IDEA

?IDEA更換工作空間:

(也可不更換,就用原來的)

關(guān)閉當(dāng)前工作空間:


創(chuàng)建新項目/選擇新的工作空間

然后會有彈框,x掉。

配置MAVEN


配置JDK:


模型自動創(chuàng)建springboot項目:(也可手動創(chuàng)建)



等待一會兒。

修改目錄結(jié)構(gòu)

引入依賴:(pom.xml)

```xml

<?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.1.6.RELEASE</version>

? ? ? ? <relativePath/> <!-- lookup parent from repository -->

? ? </parent>

? ? <groupId>com.baizhi</groupId>

? ? <artifactId>yingx_zhangyk</artifactId>

? ? <version>0.0.1-SNAPSHOT</version>

? ? <name>yingx_zhangyk</name>

? ? <description>Demo project for Spring Boot</description>

? ? <properties>

? ? ? ? <java.version>1.8</java.version>

? ? </properties>

? ? <dependencies>

? ? ? ? <!--web支持的jar springboot的啟動器-->

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>org.springframework.boot</groupId>

? ? ? ? ? ? <artifactId>spring-boot-starter-web</artifactId>

? ? ? ? </dependency>

? ? ? ? <!--測試支持的jar-->

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>org.springframework.boot</groupId>

? ? ? ? ? ? <artifactId>spring-boot-starter-test</artifactId>

? ? ? ? ? ? <!-- 只在test測試?yán)锩孢\行 -->

? ? ? ? ? ? <scope>test</scope>

? ? ? ? </dependency>

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>org.projectlombok</groupId>

? ? ? ? ? ? <artifactId>lombok</artifactId>

? ? ? ? ? ? <version>1.18.4</version>

? ? ? ? ? ? <scope>provided</scope>

? ? ? ? </dependency>

? ? ? ? <!--整合mybatis-->

? ? ? ? <!--mybatis和springboot的整合包-->

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>org.mybatis.spring.boot</groupId>

? ? ? ? ? ? <artifactId>mybatis-spring-boot-starter</artifactId>

? ? ? ? ? ? <version>2.0.0</version>

? ? ? ? </dependency>

? ? ? ? <!--數(shù)據(jù)源-->

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>com.alibaba</groupId>

? ? ? ? ? ? <artifactId>druid</artifactId>

? ? ? ? ? ? <version>1.0.29</version>

? ? ? ? </dependency>

? ? ? ? <!--數(shù)據(jù)庫驅(qū)動-->

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>mysql</groupId>

? ? ? ? ? ? <artifactId>mysql-connector-java</artifactId>

? ? ? ? ? ? <version>5.1.47</version>

? ? ? ? </dependency>

? ? ? ? <!--mybatis的核心jar-->

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>org.mybatis</groupId>

? ? ? ? ? ? <artifactId>mybatis</artifactId>

? ? ? ? ? ? <version>3.4.6</version>

? ? ? ? </dependency>

? ? ? ? <!-- 給內(nèi)嵌tomcat提供jsp解析功能的jar-->

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>org.apache.tomcat.embed</groupId>

? ? ? ? ? ? <artifactId>tomcat-embed-jasper</artifactId>

? ? ? ? </dependency>

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>com.fasterxml.jackson.core</groupId>

? ? ? ? ? ? <artifactId>jackson-databind</artifactId>

? ? ? ? ? ? <version>2.9.8</version>

? ? ? ? </dependency>

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>jstl</groupId>

? ? ? ? ? ? <artifactId>jstl</artifactId>

? ? ? ? ? ? <version>1.2</version>

? ? ? ? </dependency>

? ? ? ? <!--引入fastjson-->

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>com.alibaba</groupId>

? ? ? ? ? ? <artifactId>fastjson</artifactId>

? ? ? ? ? ? <version>1.2.47</version>

? ? ? ? </dependency>

? ? ? ? <!--文件上傳相關(guān)jar包-->

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>org.apache.commons</groupId>

? ? ? ? ? ? <artifactId>commons-lang3</artifactId>

? ? ? ? ? ? <version>3.8.1</version>

? ? ? ? </dependency>

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>commons-io</groupId>

? ? ? ? ? ? <artifactId>commons-io</artifactId>

? ? ? ? ? ? <version>2.6</version>

? ? ? ? </dependency>

? ? ? ? <dependency>

? ? ? ? ? ? <groupId>commons-fileupload</groupId>

? ? ? ? ? ? <artifactId>commons-fileupload</artifactId>

? ? ? ? ? ? <version>1.3.3</version>

? ? ? ? </dependency>

? ? </dependencies>

? ? <build>

? ? ? ? <plugins>

? ? ? ? ? ? <plugin>

? ? ? ? ? ? ? ? <groupId>org.springframework.boot</groupId>

? ? ? ? ? ? ? ? <artifactId>spring-boot-maven-plugin</artifactId>

? ? ? ? ? ? </plugin>

? ? ? ? </plugins>

? ? </build>

</project>

```

配置文件:(application.xml)

```xml

server:

? port: 8989

? servlet:

? ? context-path: /yingx

? ? jsp:

? ? ? init-parameters:

? ? ? ? development: true

spring:

? mvc:

? ? view:

? ? ? suffix: .jsp

? ? ? prefix: /

? datasource:

? ? driver-class-name: com.mysql.jdbc.Driver

? ? url: jdbc:mysql://localhost:3306/yingx

? ? username: root

? ? password: 123456

mybatis:

? type-aliases-package: com.baizhi.entity

? mapper-locations: classpath:com/baizhi/mapper/*Mapper.xml

```

可以開始寫代碼了。

提示:先寫個流程測試一下

舉例:

entity/Admin.java

```java

package com.baizhi.entity;

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

@Data

@AllArgsConstructor? //有參構(gòu)造

@NoArgsConstructor //無參構(gòu)造

public class Admin {

? ? private String id;

? ? private String username;

? ? private String password;

}

```

resources/xxx/mapper/AdminMapper.xml

```java

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="com.baizhi.dao.AdminDao">

? ? <select id="queryByUsername" parameterType="java.lang.String" resultType="Admin">

? ? ? ? select id,username,password from yx_admin where username=#{username}

? ? </select>

</mapper>? ?

```

dao/AdminDao.java

```java

package com.baizhi.dao;

import com.baizhi.entity.Admin;

public interface AdminDao {

? ? /*

? ? 后臺管理員登錄方法

? ? */

? ? public Admin queryByUsername(String username);

}

```

xxxApplication.java

```java

package com.baizhi;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.baizhi.dao")

@SpringBootApplication

public class YingxZhangykApplication {

? ? public static void main(String[] args) {

? ? ? ? SpringApplication.run(YingxZhangykApplication.class, args);

? ? }

}

```

test/xxx.java

```java

package com.baizhi.yingx_zhangyk;

import com.baizhi.dao.AdminDao;

import com.baizhi.entity.Admin;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@RunWith(SpringRunner.class)

@SpringBootTest

public class YingxZhangykApplicationTests {

? ? @Resource

? ? AdminDao adminDao;

? ? @Test

? ? public void contextLoads() {

? ? ? ? Admin admin= adminDao.queryByUsername("admin");

? ? ? ? System.out.println(admin);

? ? }

}

```

運行測試代碼:

可以開始寫項目了。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容