在以往的ssh,ssm传统项目中,大量的xml等配置文件搞得开发人员晕头转向,导致开发效率严重下降。自从springBoot新一代框架出现之后,开发人员的春天终于来了,下面说下springBoot的优势所在:
简化xml(除了mybatis)
独立的应用程序 (jar/war/jsp)建议选择jar工程,war也可以,不推荐选择jsp;
单独war工程--不需要集成tomcat插件运行 (内嵌一个tomcat)
1,创建独立的spring应用程序
2,嵌入的tomcat,无需部署war文件
3,简化maven配置
4,自动配置spring
5,提高 生产就绪型功能,如指标,健康检查,和外部配置;
6,开箱即用,没有代码生成,也无需xml配置
接下来给大家演示springBoot+spring+mybatis+jsp实现一个分页查询小项目;
第一:我们在pom文件中引入springBoot的依赖:
<!--springBoot依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<!-- jsp依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- jsts标签库依赖 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
</dependencies>
第二:在src/main/resources下创建application.properties文件或application.yml文件,springBoot默认在此目录下找配置文件:
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.mvc.view.prefix=/jsp/
spring.mvc.view.suffix=.jsp
第三:分层开发,具体如下:
1,编写pojo层:
package com.offcn.pojo;
import java.sql.Timestamp;
public class Products {
private Integer pid;
private String name;
private Integer catalog;
private String catalog_name;
private Integer number;
private String picture;
private Timestamp release_time;
private Double price;
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getCatalog() {
return catalog;
}
public void setCatalog(Integer catalog) {
this.catalog = catalog;
}
public String getCatalog_name() {
return catalog_name;
}
public void setCatalog_name(String catalog_name) {
this.catalog_name = catalog_name;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public Timestamp getRelease_time() {
return release_time;
}
public void setRelease_time(Timestamp release_time) {
this.release_time = release_time;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}
2,dao层(mapper)
package com.offcn.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import com.offcn.pojo.Products;
public interface ProductsMapper {
@Select("select count(1) from products")
public int getProductsCount();
@Select("select pid,name,price,picture,release_time from products limit #{skip},#{size}")
public List<Products> getProducts(@Param("skip") Integer skip,@Param("size") Integer size );
}
3,编写service层:
package com.offcn.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.offcn.pojo.Products;
public interface ProductsService {
public int getProductCount();
public List<Products> getProducts(Integer pageNO,Integer size);
}
serviceImpl实现service接口:
package com.offcn.serviceImpl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.offcn.mapper.ProductsMapper;
import com.offcn.pojo.Products;
import com.offcn.service.ProductsService;
@Service("productsService")
public class ProductsServiceImpl implements ProductsService {
@Resource
private ProductsMapper productsMapper;
@Override
public int getProductCount() {
return productsMapper.getProductsCount();
}
@Override
public List<Products> getProducts(Integer pageNO, Integer size) {
int skip =(pageNO-1)*size;
return productsMapper.getProducts(skip, size);
}
}
4,编写controller层:
package com.offcn.controller;
import javax.annotation.Resource;
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.offcn.service.ProductsService;
@Controller
public class ProductsController {
@Resource
private ProductsService productsService;
@RequestMapping("/index")
public String pageProductList(@RequestParam(required=false,defaultValue="1") int pageNO,Model model){
int size=10;
int totalProduct = productsService.getProductCount();
int totalPage=0;
// 得到数据显示的最大页数
if(totalProduct%size==0){
totalPage=totalProduct/size;
}else{
totalPage=totalProduct/size+1;
}
//页数超出处理
if(pageNO<=0){
pageNO=1;
}else if(pageNO>totalPage){
pageNO=totalPage;
}
model.addAttribute("totalPage", totalPage);
model.addAttribute("size", size);
model.addAttribute("pageNO", pageNO);
model.addAttribute("totalProduct", totalProduct);
model.addAttribute("productList", productsService.getProducts(pageNO, size));
return "index";
}
}
5,编写程序启动层app
package com.offcn;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages={"com.offcn.mapper"})
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
//注意@SpringBootApplication注解默认在同级包下搜索包,将包加入到容器,
6,编写index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h2>Hello World!</h2>
model.addAttribute("totalPage", totalPage);
model.addAttribute("size", size);
model.addAttribute("pageNO", pageNO);
model.addAttribute("totalProduct", totalProduct);
model.addAttribute("productList", productsService.getProducts(pageNO, size));
pid,name,price,picture,release_time
<table>
<c:forEach items="${productList}" var="product">
<tr>
<td>${product.pid}</td>
<td>${product.name}</td>
<td>${product.picture}</td>
<td>${product.price}</td>
<td>${product.release_time}</td>
</tr>
</c:forEach>
</table>
<div>
<p>商品总数:${totalProduct}</p>
<a href="index?pageNO=1">首页</a>
<a href="index?pageNO=${pageNO-1}">上一页</a>
<a href="index?pageNO=${pageNO+1}">下一页</a>
<a href="index?pageNO=${totalPage}">尾页</a>
<p>当前是第${pageNO}页 / 总共${totalPage}页</p>
</div>
</body>
</html>
运行效果如下:
总结:
springBoot开发起来真的贼爽,不需编写大量的xml,小弟做的一个简单的小项目,存在有些不足,求大神指正一下