啥也不说,上代码和部分解释(本文使用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项目搭建完成。如果启动后出现错误可留言=.=.=.=.=.=.=.=..=.=.=.=.=.=.=.=.=.=