啥也不說,上代碼和部分解釋(本文使用idea開發工具)
創建spring boot項目非常簡單
1.
2.
3.
4.
直接next,改好你的存儲空間後,finish即可
因爲spring boot自己引入了maven依賴 ,所以需要修改maven的地址,看下圖
因爲springboot本身不生成webapp文件,所以需要配置,如下圖
接下來配置application.yml (本篇使用的yml方式配置,可上網搜properties的配置)
spring:
mvc:
view:
prefix: /WEB-INF/views/ #標註掃描jsp的目錄
suffix: .jsp
datasource:
url: jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver #驅動
jpa:
hibernate:
ddl-auto: update #新增數據庫鏈接必備
server:
port: 8080
接下來是依賴(包含mybatis,mysql以及spring boot不支持jsp的依賴)
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<!--因springboot不支持jsp,導入依賴解決 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
創建bean,比如我創建的是student類
package com.rmg.mysql.example.mysqldemo.pojo;
public class Student {
private Integer id;
private String name;
private String password;
private String description;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", description='" + description + '\'' +
'}';
}
}
接下來創建一個StudentMapper用來操作數據庫進行查詢
package com.rmg.mysql.example.mysqldemo.mapper;
import com.rmg.mysql.example.mysqldemo.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface StudentMapper {
//無腦全數據查詢
@Select("SELECT * FROM BaseStudent")
List<Student> findAll();
}
接下來創建一個業務類的接口實現StudentServiceImpl
package com.rmg.mysql.example.mysqldemo.serviceImpl;
import com.rmg.mysql.example.mysqldemo.mapper.StudentMapper;
import com.rmg.mysql.example.mysqldemo.pojo.Student;
import com.rmg.mysql.example.mysqldemo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
//無腦查詢student表全部數據
@Override
public List<Student> findAll() {
return studentMapper.findAll();
}
}
接下來新建一個接口StudentService
package com.rmg.mysql.example.mysqldemo.service;
import com.rmg.mysql.example.mysqldemo.pojo.Student;
import java.util.List;
public interface StudentService {
List<Student> findAll();
}
接下來新建一個控制類StudentController
package com.rmg.mysql.example.mysqldemo.controller;
import com.rmg.mysql.example.mysqldemo.pojo.Student;
import com.rmg.mysql.example.mysqldemo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/student")
public String findAll(Model model){
List<Student> studentList = studentService.findAll();
model.addAttribute("students",studentList);
return "studentList";
}
}
最後創建一個jsp界面
<%--
Created by IntelliJ IDEA.
User: 任明逛
Date: 2019/11/7
Time: 20:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>展示列表</title>
</head>
<body>
<table align='center' border='1' cellspacing='0'>
<tr>
<td>id</td>
<td>name</td>
<td>password</td>
<td>description</td>
</tr>
<c:forEach items="${students}" var="s" varStatus="st">
<tr>
<td>${s.id}</td>
<td>${s.name}</td>
<td>${s.password}</td>
<td>${s.description}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
給大家展示一下我的項目分包
啓動後界面展示:
至此一個簡單的spring boot項目搭建完成。如果啓動後出現錯誤可留言=.=.=.=.=.=.=.=..=.=.=.=.=.=.=.=.=.=