下面是福哥给大家分享的一个简单的spring boot项目实例,初学者可以参考,如果有用请点赞!
<?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.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>springBootTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>springBootTest</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud-alibaba.version>2.2.0.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId><!-- jsp包 -->
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties(注意我的工具都是1.8的):
spring.datasource.driver-class-name:com.mysql.cj.jdbc.Driver
spring.datasource.url:jdbc:mysql://localhost:3306/数据库名?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.username:root
spring.datasource.password:密码
spring.mvc.view.prefix = /webapp/
spring.mvc.view.suffix = .jsp
2、开始上代码,bean(实体)层。
Order类:
package com.test.bean;
import java.io.Serializable;
import org.springframework.stereotype.Component;
@Component //把普通pojo实例化到spring容器中,相当于配置文件中的
public class Order implements Serializable{
private Integer order_id;
private String customer_id;
private String staff_id;
private String cargo;
private String address;
private String money;
private String order_date;
private String delivery_date;
private String status;
public Integer getOrder_id() {
return order_id;
}
public void setOrder_id(Integer order_id) {
this.order_id = order_id;
}
public String getCustomer_id() {
return customer_id;
}
public void setCustomer_id(String customer_id) {
this.customer_id = customer_id;
}
public String getStaff_id() {
return staff_id;
}
public void setStaff_id(String staff_id) {
this.staff_id = staff_id;
}
public String getCargo() {
return cargo;
}
public void setCargo(String cargo) {
this.cargo = cargo;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getMoney() {
return money;
}
public void setMoney(String money) {
this.money = money;
}
public String getOrder_date() {
return order_date;
}
public void setOrder_date(String order_date) {
this.order_date = order_date;
}
public String getDelivery_date() {
return delivery_date;
}
public void setDelivery_date(String delivery_date) {
this.delivery_date = delivery_date;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public String toString() {
return "Order [order_id=" + order_id + ", customer_id=" + customer_id + ", staff_id=" + staff_id + ", cargo="
+ cargo + ", address=" + address + ", money=" + money + ", order_date=" + order_date
+ ", delivery_date=" + delivery_date + ", status=" + status + "]";
}
}
PageBean类(分页查询辅助类):
package com.test.bean;
import java.util.List;
import org.springframework.stereotype.Component;
@Component
public class PageBean<T> {
private int currPage;//当前页数
private int pageSize;//每页显示的记录数
private int totalCount;//总记录数
private int totalPage;//总页数
private List<T> lists;//每页显示的数据
public int getCurrPage() {
return currPage;
}
public void setCurrPage(int currPage) {
this.currPage = currPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<T> getLists() {
return lists;
}
public void setLists(List<T> lists) {
this.lists = lists;
}
@Override
public String toString() {
return "PageBean [currPage=" + currPage + ", pageSize=" + pageSize + ", totalCount=" + totalCount
+ ", totalPage=" + totalPage + ", lists=" + lists + "]";
}
}
3、controller层。
OrderController类:
package com.test.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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 org.springframework.web.bind.annotation.RequestParam;
import com.test.bean.Order;
import com.test.bean.PageBean;
import com.test.dao.OrderDao;
import com.test.service.OrderService;
@Controller
public class OrderController<Department> {
@Autowired
OrderDao orderdao;//把OrderDao注入,就可以直接调用了(不用new对象。)
@Autowired
OrderService orderservice;
@RequestMapping("order")//欢迎页面
public String order() {
return "login";
}
@RequestMapping("mainPage")//主页查询
public String main(Model model,@RequestParam(value="currentPage",defaultValue="1",required=false)int currentPage){
/* model.addAttribute("order",orderdao.getAll()); */
PageBean<Order> pageBean = orderservice.orderPage(currentPage);
model.addAttribute("pageBean",pageBean);
return "main";
}
/*
* @RequestMapping("OrderPage")//分页查询 public String OrderPage(Model
* model, @RequestParam(value="currentPage",defaultValue="1",required=false)int
* currentPage){ System.out.println("----orderPage-----"+currentPage);
*
* PageBean pageBean = orderdao.OrderPage(currentPage);
*
* System.out.println("----orderPage-----"+pageBean);
* model.addAttribute("order",pageBean);
*
* return "main";
*
* }
*/
@RequestMapping("delete")//删除
public String delete(int order_id) {
orderdao.deleteByPrimaryKey(order_id);
return "redirect:main";//重定向到主页面
}
@RequestMapping("userAdd") //添加
public String userAdd() {
return "userAdd";
}
@RequestMapping("userll")
public String userll() {/* jsp跳转 */
return "ll";
}
@RequestMapping("userEdit")//修改页
public String userEdit(Model model,Integer order_id) {
model.addAttribute("edit", orderdao.selecttest(order_id));
System.out.print(66);
return "userEdit";
}
@RequestMapping("userupdate")//修改执行
public String userupdate(String orderId,String cargo,String staffId,String procurementGoods,String address,String amount,String customerId,String orderDate,String expectedArrivalDate,String status,
HttpServletRequest request,HttpServletResponse response,HttpSession session,Model model) {
System.out.println(orderId);
Order order=new Order();
order.setAddress(address);//拿到前端传来的数据
order.setCustomer_id(customerId);
order.setCargo(cargo);
order.setDelivery_date(expectedArrivalDate);
order.setOrder_date(orderDate);
order.setStaff_id(staffId);
order.setStatus(status);
order.setMoney(amount);
order.setOrder_id(new Integer(orderId));
System.out.println(order.toString());
int n = orderdao.updateByPrimaryKeySelective(order);//调用更新方法对数据库进行更行(执行更新的sql语句)
System.out.println(n);
List<Order> orderList = orderdao.selectByAll();//数据库更新之后把数据查询出来
model.addAttribute("orderlist", orderList);
return "redirect:main";//重定向到main页面
}
@RequestMapping("fff")
public String fff() {
return "fff";
}
//模糊查询,搜索框
@RequestMapping(value = "selectorderByLike")
public String selectorderByLike(HttpServletRequest request, HttpServletResponse response,
HttpSession session, Model model, Order order,String content){
List<Order> sorderList = orderdao.queryOrderByLike(content);
System.out.println("查询到的数据是:"+sorderList);
model.addAttribute("order",sorderList);//这里注意order要和html中的查询遍历那里的名字一样,否则查询框里没有数据。
return "main";
}
}
4、dao层。
OrderDao接口:
package com.test.dao;
import java.util.HashMap;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.test.bean.Order;
import com.test.bean.PageBean;
@Mapper
public interface OrderDao {
@Select("select *from orders")
List<Order> getAll();//查询主页面
@Delete ("delete from orders where order_id=#{order_id}")
int deleteByPrimaryKey(int order_id);//删除
@Select("select *from orders where order_id=#{order_id}")
Order selecttest(Integer order_id);//进入修改页面,并且在input框里面拿到对应主页面的数据。
@Update("update orders set customer_id=#{customer_id},staff_id=#{staff_id},cargo=#{cargo},address=#{address},money=#{money},order_date=#{order_date},delivery_date=#{delivery_date},status=#{status} where order_id=#{order_id}")
int updateByPrimaryKeySelective(Order order);//修改执行
@Select("select *from orders")
List<Order> selectByAll();//修改后查询
List<Order> selectDimGoods(String searchFor);
List<Order> selectDimById(int searchFor);
List<Order> selectDimOrder(Order order);
List<Order> orderSelectAll();
@Select("select count(*) from orders")//分页
int selectCountOrder();
@Select("select *from orders limit #{start},#{size}")
List<Order> orderPage(HashMap<String, Integer> map);//分页sql对应的方法
@Select("select *from orders where cargo LIKE #{query_content}")
List<Order> queryOrderByLike(String content);//搜索框,根据名字查询。
}
5、service层,这里只写了分页查询功能(其他功能只在上面三层中就完成了)。
OrderService接口:
package com.test.service;
import java.util.List;
import com.test.bean.Order;
import com.test.bean.PageBean;
public interface OrderService {
List<Order> orderSelectAll();
PageBean orderPage(int currentPage);//分页查询
List<Order> selectDimOrder(Order order);
List<Order> selectDimGoods(String searchFor);
List<Order> selectDimById(int searchFor);
}
OrderServiceImp类(实现OrderService接口):
package com.test.service.impl;
import com.test.bean.PageBean;
import com.test.dao.OrderDao;
import com.test.service.OrderService;
import com.test.bean.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
@Service("orderService")
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderDao orderdao;
@Override
public List<Order> orderSelectAll(){
return orderdao.orderSelectAll();
}
@Override
public PageBean orderPage(int currentPage) {
HashMap<String,Integer> map = new HashMap<>();
PageBean<Order> pageBean = new PageBean();
//封装当前页数
pageBean.setCurrPage(currentPage);
//封装每页显示的记录数
int pageSize = 5;
pageBean.setPageSize(pageSize);
//封装总记录数
int totalCount = orderdao.selectCountOrder();
pageBean.setTotalCount(totalCount);
//封装总页数
double tc = totalCount;
Double num = Math.ceil(totalCount/pageSize);
pageBean.setTotalPage(num.intValue());
map.put("start",(currentPage-1)*pageSize);
map.put("size",pageSize);
List<Order> lists = orderdao.orderPage(map);
pageBean.setLists(lists);
return pageBean;
}
@Override
public List<Order> selectDimOrder(Order order) {
return orderdao.selectDimOrder(order);
}
@Override
public List<Order> selectDimGoods(String searchFor) {
return orderdao.selectDimGoods(searchFor);
}
@Override
public List<Order> selectDimById(int searchFor) {
return orderdao.selectDimById(searchFor);
}
}
6、上HTML代码(前端)。
login.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>欢迎页面</title>
</head>
<body>
<center><h1>欢迎页面</h1></center>
<hr>
<center><a href="mainPage">订单查询</a></center>
</body>
</html>
main.htnl:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" isELIgnored="false">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="js/jquery.js"></script>
<link href="http://java.sun.com/jsp/jstl/core" >
<title>订单查询</title>
</head>
<body style="text-align: center;">
<div style="width: 100%;text-align: center;">
<h1>主页</h1>
</div>
<hr>
<img style="height: 50px;width: 100px;" src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1585911327836&di=24dedc567b985208acaf715693014b80&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20180808%2F9401cf0cfa45460d97c8baf94d16d951.jpeg"/>
<input id="query_content" type="text" placeholder="请输入姓名" th:value="${content}" style="height: 30px;width:300px; ">
<button type="button" onclick="queryByLike();" style="height:40px;width: 50px;">搜索</button>
<div style="height:20px;"></div>
<table style="margin: auto;"border="1">
<tr style="background: #28FF28">
<th style="color:blue">订单号</th>
<th style="color:blue">客户编号</th>
<th style="color:blue">员工编号</th>
<th style="color:blue">货物</th>
<th style="color:blue">送货地址</th>
<th style="color:blue">金额</th>
<th style="color:blue">订货日期</th>
<th style="color:blue">交货日期</th>
<th style="color:blue">状态</th>
<th style="color:blue">操 作1</th>
<th style="color:blue">操 作2</th>
<th style="color:blue">操 作3</th>
</tr>
<tr th:each="order : ${pageBean.lists}" >
<td th:text="${order.order_id}" style="background: #28FF28"></td>
<td th:text="${order.customer_id}" style="background: #FF9224"></td>
<td th:text="${order.staff_id}" style="background: #FFD306"></td>
<td th:text="${order.cargo}" style="background: #5CADAD"></td>
<td th:text="${order.address}" style="background: #FF95CA"></td>
<td th:text="${order.money}" style="background: #E1C4C4"></td>
<td th:text="${order.order_date}" style="background: #4DFFFF"></td>
<td th:text="${order.delivery_date}" style="background: #B3D9D9"></td>
<td th:text="${order.status}" style="background: #82D900"></td>
<!-- <tr th:each="order : ${order}" >
<td th:text="${order.order_id}" style="background: #28FF28"></td>
<td th:text="${order.customer_id}" style="background: #FF9224"></td>
<td th:text="${order.staff_id}" style="background: #FFD306"></td>
<td th:text="${order.cargo}" style="background: #5CADAD"></td>
<td th:text="${order.address}" style="background: #FF95CA"></td>
<td th:text="${order.money}" style="background: #E1C4C4"></td>
<td th:text="${order.order_date}" style="background: #4DFFFF"></td>
<td th:text="${order.delivery_date}" style="background: #B3D9D9"></td>
<td th:text="${order.status}" style="background: #82D900"></td>
-->
<td><a th:href="'delete?order_id='+${order.order_id}" ><button>删除</button></a></td>
<td><a th:href="userAdd"><button>增加</button></a></td>
<td><a th:href="${'userEdit?order_id='+order.order_id}"><button>修改</button></a></td>
</tr>
</table>
<!--
<table>
<tr>
<td>
<a href="fff">访问fff</a>
</td>
</tr>
</table> -->
<center>
<table>
<div>
<tr>
<td ><a th:href="${'mainPage?currentPage='+1}" >首页</a> </td>
<td ><a th:href="'mainPage?currentPage='+${pageBean.currPage-1}">上一页</a> </td>
<td onclick="num(1)"><button><span id="currentIndex1">1</span></button> </td>
<td onclick="num(2)"><button><span id="currentIndex2">2</span></button> </td>
<td onclick="num(4)"><button><span id="currentIndex4">...</span></button> </td>
<td onclick="num(5)"><button><span id="currentIndex5">5</span></button> </td>
<td ><a th:href="'mainPage?currentPage='+${pageBean.currPage+1}">下一页</a> </td>
<td ><a th:href="'mainPage?currentPage='+${pageBean.totalPage}">末页</a> </td>
<td >共 <span th:text="${pageBean.totalPage}"></span> 页 </td>
<td><input type="hidden" value=""/>
<div >
<span> | 跳转到第</span>
<input type="text" name="gotoPageNo" value="1">
<input type="hidden" value="15">
<input type="hidden" value="" name="currentUrl" >
<span>页</span>
<button onclick="_confirm()">确认</button>
<button><a href="javascript:history.back(-1)">返回</a></button>
</td>
</div>
</tr>
</div>
</table></center>
</body>
<script type="text/javascript">
function queryByLike(){//搜索
var content = document.getElementById("query_content").value;
window.location.href="selectorderByLike?content="+content;
}
/* $.ajax({
url: "/请求地址",
type: "POST",
dataType:"json",
data: {Code:"121"},//请求参数
success: function(data) {
//成功提示
},
error:function(){
//失败提示
}
}) */
</script>
</html>
userAdd.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>添加用户</title>
<link rel="stylesheet" th:href="@{/css/bootstrap.css}"></link>
</head>
<body>
<center>
<br/>
<h1>添加用户</h1>
<hr>
<br/><br/>
<div>
<form th:action="@{/add}" method="post">
<div>
<label for="name">姓名:</label>
<input type="text" name="name" id="name" placeholder="name"/>
</div><div style="height:10px"></div>
<div>
<label for="age">年龄:</label>
<input type="text" name="age" id="age" placeholder="age"/>
</div><div style="height:10px"></div>
<div>
<div>
<input type="submit" value="确定"/>
<input type="reset" value="重置" />
<button><a href="javascript:history.back(-1)">返回上一页</a></button>
</div>
</div>
</form>
</div></center>
</body>
</html>
userEdit.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" isELIgnored="false">
<head>
<meta charset="UTF-8"/>
<script type="text/javascript" src="js/jquery.js"></script>
<title>修改用户</title>
<link rel="stylesheet" th:href="@{/css/bootstrap.css}"></link>
</head>
<body>
<center>
<br/>
<h1>修改用户</h1>
<hr>
<br/><br/>
<div class="with:80%">
<form action="userupdate" method="post">
<table>
<tr>
<!-- <td>订单号:</td> -->
<td><input id="name1" type="hidden" name="orderId" th:value="${edit.order_id}" ></td>
</tr>
<tr>
<td>客户编号:</td>
<td><input type="text" name="customerId" th:value="${edit.customer_id }"></td>
</tr>
<tr>
<td>员工编号:</td>
<td><input type="text" name="staffId" th:value="${edit.staff_id }"></td>
</tr>
<tr>
<td>货物:</td>
<td><input type="text" name="cargo" th:value="${edit.cargo }"></td>
</tr>
<tr>
<td>送货地址:</td>
<td><input type="text" name="address" th:value="${edit.address }"></td>
</tr>
<tr>
<td>金额:</td>
<td><input type="text" name="amount" th:value="${edit.money }"></td>
</tr>
<tr>
<td>订货日期:</td>
<td><input type="text" name="orderDate" th:value="${edit.order_date }"></td>
</tr>
<tr>
<td>交货日期:</td>
<td><input type="text" name="expectedArrivalDate" th:value="${edit.delivery_date }"></td>
</tr>
<tr>
<td>状态:</td>
<td><input type="text" name="status" th:value="${edit.status }"></td>
</tr>
<tr>
<td><input type="submit" value="确认修改"></td>
<td><input type="reset" value="重置">
<button><a href="javascript:history.back(-1)">返回上一页</a></button></td>
</tr>
</table>
</form>
<a href="ll.jsp">访问jsp</a>
</div>
</center>
<script type="text/javascript">
/* document.getElementById('name1').value='值'; */
</script>
</body>
</html>