先给大家看一下整个项目的结构
user
sale
product
pom.xml
"1.0" encoding="UTF-8"?>"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"> 4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.1.RELEASE com.zn springboot_invoicing 0.0.1-SNAPSHOT springboot_invoicing Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-logging org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools true runtime mysql mysql-connector-java runtime mysql mysql-connector-java 5.1.32 org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.1 com.alibaba fastjson 1.2.12 com.alibaba druid 1.0.18 org.mybatis mybatis 3.4.1 org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-jdbc com.github.pagehelper pagehelper-spring-boot-starter 1.2.3 org.springframework.boot spring-boot-maven-plugin src/main/java **/*.properties **/*.xml
主程序
package com.mckz; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @MapperScan("com.mckz.*") @SpringBootApplication public class SpringbootInvoicingApplication { public static void main(String[] args) { SpringApplication.run(SpringbootInvoicingApplication.class, args); } }
entity:product
package com.mckz.entity; public class Product { private Integer pid; private String productName; private int quantity; public Integer getPid() { return pid; } public void setPid(Integer pid) { this.pid = pid; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } }
entity:user
package com.mckz.entity; import org.springframework.stereotype.Repository; @Repository public class User { private Integer uid; private String userName; private String password; private String realName; public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getRealName() { return realName; } public void setRealName(String realName) { this.realName = realName; } }
entity:sale
package com.mckz.entity; import java.util.Date; public class Sale { private Integer sid; private Double price; private Integer quantity; private Double totalPrice; private Date saleDate; private Integer userId; private Integer productId; private Product product; private User user; public Product getProduct() { return product; } public void setProduct(Product product) { this.product = product; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Integer getSid() { return sid; } public void setSid(Integer sid) { this.sid = sid; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public Integer getQuantity() { return quantity; } public void setQuantity(Integer quantity) { this.quantity = quantity; } public Double getTotalPrice() { return totalPrice; } public void setTotalPrice(Double totalPrice) { this.totalPrice = totalPrice; } public Date getSaleDate() { return saleDate; } public void setSaleDate(Date saleDate) { this.saleDate = saleDate; } public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public Integer getProductId() { return productId; } public void setProductId(Integer productId) { this.productId = productId; } }
Userdao
package com.mckz.dao; import com.mckz.entity.User; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; @Repository public interface UserDao { //登录的方法 @Select("select * from users where userName=#{userName} and password=#{password}") public User login(User user); }
Productdao
package com.mckz.dao; import com.mckz.entity.Product; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface ProductDao { //查库存 @Select("select * from product") public ListgetList(); @Select("select * from product where pid=#{pid}") public Product getname(@Param("pid") Integer pid); }
saledao
package com.mckz.dao; import com.mckz.entity.Product; import com.mckz.entity.Sale; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface SaleDao { //查询 public Listgetsale(@Param("num") Integer num); //绑定下拉框 @Select("select * from product") public List getList(); //添加 public int addsale(Sale sale); }
saledao.xml
"1.0" encoding="UTF-8" ?> DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">namespace="com.mckz.dao.SaleDao"> "getAllSales" type="com.mckz.entity.Sale"> <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>"sid" column="sid"> "quantity" column="quantity"> "price" column="price"> "totalPrice" property="totalPrice"> "saleDate" column="saleDate"> "userId" property="userId"> "productId" column="productId"> "product" javaType="com.mckz.entity.Product"> "pid" property="pid"> "productName" property="productName"> "user" javaType="com.mckz.entity.User"> "uid" column="uid"> "userName" property="userName"> "addsale"> INSERT INTO sale(price,quantity,totalPrice,saleDate,userId,productId) VALUE(#{price},#{quantity},#{totalPrice},#{saleDate},#{userId},#{productId})
service:productservice
package com.mckz.service; import com.mckz.entity.Product; import java.util.List; public interface ProductService { //查库存 public ListgetList(); public Product getname(Integer pid); }
service:saleservice
package com.mckz.service; import com.github.pagehelper.PageInfo; import com.mckz.entity.Product; import com.mckz.entity.Sale; import org.apache.ibatis.annotations.Param; import java.util.List; public interface SaleService { //查询 public PageInfogetsale(@Param("num") Integer num, @Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize); //绑定下拉框 public List getList(); //添加 public int addsale(Sale sale); }
service:userservice
package com.mckz.service; import com.mckz.entity.User; public interface UserService { //登录的方法 public User login(User user); }
productserviceimpl
package com.mckz.service.impl; import com.mckz.dao.ProductDao; import com.mckz.entity.Product; import com.mckz.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 ListgetList() { List list = productDao.getList(); return list; } @Override public Product getname(Integer pid) { Product getname = productDao.getname(pid); return getname; } }
saleserviceimpl
package com.mckz.service.impl; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.mckz.dao.ProductDao; import com.mckz.dao.SaleDao; import com.mckz.entity.Product; import com.mckz.entity.Sale; import com.mckz.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 PageInfogetsale(Integer num, Integer pageNum, Integer pageSize) { Page page = PageHelper.startPage(pageNum, pageSize); List getsale = saleDao.getsale(num); return page.toPageInfo(); } @Override public List getList() { List list = productDao.getList(); return list; } @Override public int addsale(Sale sale) { int addsale = saleDao.addsale(sale); return addsale; } }
userserviceimpl
package com.mckz.service.impl; import com.mckz.dao.UserDao; import com.mckz.entity.*; import com.mckz.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); } }
application.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql:///invoicingsystem spring.datasource.username=root spring.datasource.password=123 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