SpringBoot终章(整合小型进销系统)
在前面的章节中我们学习Spring的时候可以看到配置文件比较多,所以我们有了SpringBoot
1. 引入依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency> <!-- 核心依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 可以实现热部署,在IDEA上实现热部署还需一些额外的配置,请查阅资料 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>runtime</scope> </dependency> <!-- JDBC for mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.32</version> </dependency> <!-- mybatis --> <!--mybatis--> <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <!--fastJson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.12</version> </dependency> <!--druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <!--thymeleaf 新的模板引擎,比jsp要出色--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--jdbc--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- 分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> </dependencies>
2.配置application.properties文件
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql:///invoicingsystem spring.datasource.username=root spring.datasource.password=123456 mybais.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.zn.entity #映射级别 mybatis.configuration.auto-mapping-behavior=full #Spring Data JPA配置 spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jackson.serialization.indent-output=true spring.jpa.database=mysql spring.main.allow-bean-definition-overriding=true
3.配置页面
在前面的页面中我们使用的都是jsp页面,但是在这里我们是使用html页面的并且存放到templates目录下
4.配置Dao接口
@Repository public interface ProductDao { //查库存 public List<Product> getList(); public Product getname(@Param("pid") Integer pid); }
@Repository public interface SaleDao { //查询 public List<Sale> getsale(@Param("num") Integer num); //绑定下拉框 public List<Product> getList(); //添加 public int addsale(Sale sale); }
@Repository public interface UserDao { //登录的方法 public User login(User user); }
5.配置Dao.xml文件
product
<?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"> <!--namespace需要指向接口全路径--> <mapper namespace="com.zn.dao.ProductDao"> <select id="getList" resultType="com.zn.entity.Product"> select * from product </select> <select id="getname" resultType="com.zn.entity.Product"> select * from product where pid=#{pid} </select> </mapper>
sale
<?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"> <!--namespace需要指向接口全路径--> <mapper namespace="com.zn.dao.SaleDao"> <resultMap id="getAllSales" type="com.zn.entity.Sale"> <id property="sid" column="sid"></id> <result property="quantity" column="quantity"></result> <result property="price" column="price"></result> <result column="totalPrice" property="totalPrice"></result> <result property="saleDate" column="saleDate"></result> <result column="userId" property="userId"></result> <result property="productId" column="productId"></result> <association property="product" javaType="com.zn.entity.Product"> <id column="pid" property="pid"></id> <result column="productName" property="productName"></result> </association> <association property="user" javaType="com.zn.entity.User"> <id property="uid" column="uid"></id> <result column="userName" property="userName"></result> </association> </resultMap> <select id="getsale" resultMap="getAllSales"> select * from product as p,sale as s,users as u where p.pid=s.productId and u.uid=s.userId <if test="num==1"> order by s.totalPrice DESC </if> <if test="num==2"> order by s.saleDate DESC </if> </select> <select id="getList" resultType="com.zn.entity.Product"> select * from product </select> <insert id="addsale"> INSERT INTO sale(price,quantity,totalPrice,saleDate,userId,productId) VALUE(#{price},#{quantity},#{totalPrice},#{saleDate},#{userId},#{productId}) </insert> </mapper>
user
<?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"> <!--namespace需要指向接口全路径--> <mapper namespace="com.zn.dao.UserDao"> <select id="login" resultType="com.zn.entity.User"> select * from users where userName=#{userName} and password=#{password} </select> </mapper>
6.配置Service层
product
public interface ProductService { //查库存 public List<Product> getList(); public Product getname(Integer pid); }
sale
public interface SaleService { //查询 public PageInfo<Sale> getsale(@Param("num") Integer num, @Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize); //绑定下拉框 public List<Product> getList(); //添加 public int addsale(Sale sale); }
user
public interface UserService { //登录的方法 public User login(User user); }
7.配置serviceimpl层
product
package com.zn.service.impl; import com.zn.dao.ProductDao; import com.zn.entity.Product; import com.zn.service.ProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service("ProductService") public class ProductServiceImpl implements ProductService { @Autowired ProductDao productDao; @Override public List<Product> getList() { List<Product> list = productDao.getList(); return list; } @Override public Product getname(Integer pid) { Product getname = productDao.getname(pid); return getname; } }
sale
package com.zn.service.impl; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.zn.dao.ProductDao; import com.zn.dao.SaleDao; import com.zn.entity.Product; import com.zn.entity.Sale; import com.zn.service.SaleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service("SaleService") public class SaleServiceImpl implements SaleService { @Autowired SaleDao saleDao; @Autowired ProductDao productDao; @Override public PageInfo<Sale> getsale(Integer num, Integer pageNum, Integer pageSize) { Page<Sale> page = PageHelper.startPage(pageNum, pageSize); List<Sale> getsale = saleDao.getsale(num); return page.toPageInfo(); } @Override public List<Product> getList() { List<Product> list = productDao.getList(); return list; } @Override public int addsale(Sale sale) { int addsale = saleDao.addsale(sale); return addsale; } }
user
package com.zn.service.impl; import com.zn.dao.UserDao; import com.zn.entity.User; import com.zn.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service("UserService") public class UserServiceImpl implements UserService { @Autowired UserDao userDao; @Override public User login(User user) { return userDao.login(user); } }
8.配置Controller控制器
product
package com.zn.controller; import com.zn.entity.Product; import com.zn.service.ProductService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import java.util.List; @Controller @RequestMapping("/product") public class ProductController { @Resource(name = "ProductService") ProductService productService; @RequestMapping("/getproduct") @ResponseBody public Object getProductList(){ System.out.println("库存列表绑定"); List<Product> proList = productService.getList(); return proList; } /*根据id查询库存*/ @RequestMapping("/getpr") @ResponseBody public Object getProduct(Integer pid){ System.out.println("库存详情"); Product proLists = productService.getname(pid); return proLists; } }
sale
package com.zn.controller; import com.github.pagehelper.PageInfo; import com.zn.entity.Product; import com.zn.entity.Sale; import com.zn.entity.User; import com.zn.service.SaleService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.util.Date; import java.util.List; @Controller @RequestMapping("/sale") public class SaleController { @Resource(name = "SaleService") SaleService saleService; @RequestMapping("/getsale") @ResponseBody public Object getsale(Integer num, Integer pageNum, Integer pageSize){ System.out.println("进了吗??"); PageInfo<Sale> getsale = saleService.getsale(num, pageNum + 1, 5); return getsale; } @RequestMapping("/getlist") @ResponseBody public Object getlist(){ System.out.println("绑定下拉框"); List<Product> list = saleService.getList(); return list; } @RequestMapping("/addsale") @ResponseBody public ModelAndView addsale(Sale sale, HttpServletRequest request, ModelAndView mv){ if (sale!=null){ Double totalPrice=sale.getPrice() * sale.getQuantity(); sale.setTotalPrice(totalPrice); sale.setSaleDate(new Date()); User login = (User) request.getSession().getAttribute("login"); sale.setUserId(login.getUid()); int addsale = saleService.addsale(sale); if (addsale>0){ System.out.println("添加成功!"); mv.setViewName("saleList"); }else{ System.out.println("添加失败!"); mv.setViewName("prodectAdd"); } } return mv; } }
user
package com.zn.controller; import com.zn.entity.User; import com.zn.service.UserService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Controller @RequestMapping(value = "/user") public class UserController { @Resource(name = "UserService") UserService userService; @RequestMapping("/login") @ResponseBody public ModelAndView login(User user, HttpServletRequest request, HttpServletResponse response, ModelAndView modelAndView){ User login = userService.login(user); if (login!=null){ System.out.println("登陆成功!"); request.getSession().setAttribute("login",login); modelAndView.setViewName("index"); }else { modelAndView.setViewName("login"); } return modelAndView; } @RequestMapping("/remover") private String loginOut(HttpServletRequest request, HttpServletResponse response) { request.getSession().removeAttribute("login"); return "login"; } /*转发登陆页面*/ @RequestMapping("/goLogin") public Object goLogin(){ return "login"; } /*转发查询页面*/ @RequestMapping("leList") public Object saleList(){ return "saleList"; } /*转发销售页面*/ @RequestMapping("/prodectAdd") public Object productAdd(){ return "prodectAdd"; } /*转发查看库存页面*/ @RequestMapping("/prview") public Object prview(){ return "prview"; } }
9.开启SpringbootInvoicingApplication
开启后直接访问控制器写入的路径