SpringBoot与MyBatis整合实现小型进销存项目
1、导入依赖
<?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.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.zn</groupId> <artifactId>springboot_invoicing</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot_invoicing</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <!--xml配置,此是为了将来整合Hibernate或者mybatis 默认没有需要配置--> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
2、配置application.properties
3、页面以及静态配置
4、dao层
5、dao.xml
<?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>
<?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>
<?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层
7、serviceImpl层
8、controller层
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; } }
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; } }
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("/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"; } }