springboot入门教程
使用Springboot在线创建项目:
springboot简介;
springboot
https://baike.baidu.com/item/Spring%20Boot/20249767?fr=aladdin
Springboot
Springboot
https://spring.io/projects/spring-boot/
Building a RESTful Web Service
Building a RESTful Web Service
https://spring.io/guides/gs/rest-service/
https://start.spring.io/
https://start.spring.io/
https://start.spring.io/
在线工具生成的一个Springboot项目:
然后用idea打开我们的项目名为demo的springboot项目:
2 error
‘org.springframework.boot:spring-boot-starter-parent’ not found
https://blog.csdn.net/qq_45171544/article/details/125975987
【错误解决】Springboot中Plugin ‘org.springframework.boot:spring-boot-maven-plugin:‘ not found
https://blog.csdn.net/weixin_42072280/article/details/110388852
运行DemoApplication类文件:
新建程序文件并运行:
使用idea创建项目:
创建项目:
点击next:
点击next:
点击next:
点击finish。
创建完成:
pom.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.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.aaa.demo</groupId>
<artifactId>springboot_ktlx_0930</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_ktlx_0930</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--springboot默认推荐的服务器端页面视图模板,作用类似于jsp-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--spring springMVC web相关组件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis相关组件-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--实体类插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--springboot的测试模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
运行项目:
配置项目:
#配置服务器端口号
server.port=8088
#配置服务器请求路径
server.servlet.context-path=/aaa
#配数据库连接(数据源)
#驱动类
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#用户名
spring.datasource.username=root
#密码
spring.datasource.password=123456
#连接字符串
spring.datasource.url=jdbc:mysql://localhost:3306/0716_sboot_db?useSSL=false
#配置mybatis相关内容
#配置mapper文件的位置
mybatis.mapper-locations=classpath:com/mapper/*.xml
#配置实体类
mybatis.type-aliases-package=com.entity
#下换线转小驼峰
mybatis.configuration.map-underscore-to-camel-case=true
启动项目:
运行SpringbootKtlx0930Application类文件:
创建页面html文件:
相关配置:
创建html文件:
重新启动项目:
访问:“ http://localhost:8088/aaa/ ” 将自动匹配“ index.html ”页面;
自定义banner组件,用于表明当前项目特征
// _ooOoo_ //
// o8888888o //
// 88" . "88 //
// (| ^_^ |) //
// O\ = /O //
// ____/`---'\____ //
// .' \\| |// `. //
// / \\||| : |||// \ //
// / _||||| -:- |||||- \ //
// | | \\\ - /// | | //
// | \_| ''\---/'' | | //
// \ .-\__ `-` ___/-. / //
// ___`. .' /--.--\ `. . ___ //
// ."" '< `.___\_<|>_/___.' >'"". //
// | | : `- \`.;`\ _ /`;.`/ - ` : | | //
// \ \ `-. \_ __\ /__ _/ .-` / / //
// ========`-.____`-.___\_____/___.-`____.-'======== //
// `=---=' //
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
// 佛祖保佑 永无BUG 永不修改 //
启动项目:
解决:
clean清理一下;
重新启动项目:
添加图片和css样式等:
创建package文件:
添加图片:
添加样式:
添加js:
引用css/js/img:
重新创建了Maven项目并运行:
注意:这里因为配置的原因,重新创建了Maven项目
项目框架:
项目代码:
TestController :
package com.aaa.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping("/show")
public String show(){
//跳转到templates下的test.html
return "test";
}
}
App :
package com.aaa.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
test.html :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" type="text/css" href="/css/my.css" />
</head>
<body>
<h1 id="title">这是测试页面</h1>
<img src="/img/sg1.jpg" />
<script type="text/javascript" src="/js/demo.js"></script>
<script type="text/javascript">
document.getElementById("title").click=function (){
show();
}
</script>
</body>
</html>
#配置服务器端口号
#server.port=8088
#配置服务器请求路径
#server.servlet.context-path=/aaa
#配数据库连接(数据源)
#驱动类
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#用户名
spring.datasource.username=root
#密码
spring.datasource.password=123456
#连接字符串
spring.datasource.url=jdbc:mysql://localhost:3306/0716_sboot_db?useSSL=false
#配置mybatis相关内容
#配置mapper文件的位置
mybatis.mapper-locations=classpath:com/mapper/*.xml
#配置实体类
mybatis.type-aliases-package=com.entity
#下换线转小驼峰
mybatis.configuration.map-underscore-to-camel-case=true
// _ooOoo_ //
// o8888888o //
// 88" . "88 //
// (| ^_^ |) //
// O\ = /O //
// ____/`---'\____ //
// .' \\| |// `. //
// / \\||| : |||// \ //
// / _||||| -:- |||||- \ //
// | | \\\ - /// | | //
// | \_| ''\---/'' | | //
// \ .-\__ `-` ___/-. / //
// ___`. .' /--.--\ `. . ___ //
// ."" '< `.___\_<|>_/___.' >'"". //
// | | : `- \`.;`\ _ /`;.`/ - ` : | | //
// \ \ `-. \_ __\ /__ _/ .-` / / //
// ========`-.____`-.___\_____/___.-`____.-'======== //
// `=---=' //
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
// 佛祖保佑 永无BUG 永不修改 //
application.properties :
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>springboot_ktlx2_0930</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--springboot默认推荐的服务器端页面视图模板,作用类似于jsp-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--spring springMVC web相关组件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis相关组件-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--实体类插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--springboot的测试模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
运行项目:
连接数据库:
项目整体布局;
项目代码:
Emp :
package com.aaa.demo.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
@Data
public class Emp {
private Integer empId;
private String empName;
private String sex;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd")
private Date birthday;
}
EmpMapper :
package com.aaa.demo.mapper;
import com.aaa.demo.entity.Emp;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface EmpMapper {
List<Emp> listAll();
}
EmpMapper.xml :
<?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.aaa.demo.mapper.EmpMapper">
<select id="listAll" resultType="emp">
select * from tb_emp
</select>
</mapper>
IEmpService :
package com.aaa.demo.service;
import com.aaa.demo.entity.Emp;
import java.util.List;
public interface IEmpService {
List<Emp> listAll();
}
EmpServiceImpl :
package com.aaa.demo.service.impl;
import com.aaa.demo.entity.Emp;
import com.aaa.demo.mapper.EmpMapper;
import com.aaa.demo.service.IEmpService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class EmpServiceImpl implements IEmpService{
@Resource
private EmpMapper empMapper;
@Override
public List<Emp> listAll() {
return empMapper.listAll();
}
}
EmpController :
package com.aaa.demo.controller;
import com.aaa.demo.entity.Emp;
import com.aaa.demo.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/emp")
public class EmpController {
@Autowired
private IEmpService empService;
@GetMapping("/list")
public List<Emp> list(){
return empService.listAll();
}
}
application.properties :
#配置服务器端口号
#server.port=8088
#配置服务器请求路径
#server.servlet.context-path=/aaa
#配数据库连接(数据源)
#驱动类
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#用户名
spring.datasource.username=root
#密码
spring.datasource.password=123456
#连接字符串
spring.datasource.url=jdbc:mysql://localhost:3306/0927_ssm_vue_db?useSSL=false
#配置mybatis相关内容
#配置mapper文件的位置
mybatis.mapper-locations=classpath:com/mapper/*.xml
#配置实体类
mybatis.type-aliases-package=com.aaa.demo.entity
#下换线转小驼峰
mybatis.configuration.map-underscore-to-camel-case=true
或者:
application.yml :
server:
port: 8080
servlet:
context-path: /
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/0927_ssm_vue_db?useSSL=false&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: 123456
mybatis:
mapper-locations: classpath:com/mapper/*.xml
type-aliases-package: com.aaa.demo.entity
configuration:
map-underscore-to-camel-case: true
注意:
启动项目:
注意:clean后直接重启项目即可。(如果clean之后,compile将会出错)
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/118010.html