Java springboot最简可运行项目
一、最基础的配置只需两个文件
1.pom.xml有两种配置方式
a.使用spring-boot-starter-web来引入相关包,启动项目
b.需要关联包:spring-boot-dependencies
一种是parent方式,这在引用的文章里已有
另一种是dependencyManagement方式
<?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>net.highersoft</groupId> <artifactId>springboot</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <spring.cloud-version>Hoxton.SR9</spring.cloud-version> <spring.boot-version>2.3.6.RELEASE</spring.boot-version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <mybatis-spring-version>2.1.4</mybatis-spring-version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring.boot-version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- spring boot实现Java Web服务 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.springboot启动类
net.highersoft.springboot.Application.java
package net.highersoft.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan @EnableAutoConfiguration public class Application { public static void main(String[] args) { ConfigurableApplicationContext context=SpringApplication.run(Application.class); System.out.println("in"); } }二、配置网络相关参数
添加application.yml,这里只配置端口号.
小知识:
对于springboot项目 application.properties > application.yml ,前者的配置的优先级高于后者,前者更管用,后者更先加载。
springboot不识别而springcloud识别的文件bootstrap.yml ,这个
server: port: 8082 spring: application: name: springboot
三、添加Controller类
返回Json数据
package net.highersoft.springboot; import net.highersoft.springboot.entity.InfoEntry; import net.highersoft.springboot.mapper.ItemMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.List; import java.util.Map; @Controller public class TestController { @RequestMapping("/") @ResponseBody public Map<String,String> home(){ Map<String,String> rst=new HashMap<>(); rst.put("key","val"); return rst; } }四、添加mybatis
添加pom.xml依赖
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis-spring-version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency>添加application.yml数据源配置
server: port: 8082 spring: application: name: springboot datasource: driver-class-name: com.mysql.jdbc.Driver password: 'xx' url: jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF8&rewriteBatchedStatements=true username: root mybatis: mapperLocations: classpath:mapper/*.xml再借助上次mybatis那期里面的实体类与mapper接口与mapper映射xml文件 。注意都放到本文件夹下面(net.highersoft.springboot),其mapper需新建一个mapper文件夹,并且添加标注:
import org.apache.ibatis.annotations.Mapper; @Mapper
最后Controller里添加一个接口,来显示数据库里的数据:
@Autowired private ItemMapper itemMapper; @RequestMapping("/list") @ResponseBody public List<InfoEntry> list() { return itemMapper.selectLastInfo(); }
本文内容基于http://www.highersoft.net/html/notice/notice_572.html,再添加spring cloude部分。
相关阅读
评论:
↓ 广告开始-头部带绿为生活 ↓
↑ 广告结束-尾部支持多点击 ↑