先给大家看一下整个项目的结构
user
sale
product
pom.xml
<?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>
主程序
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 List<Product> getList(); @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 List<Sale> getsale(@Param("num") Integer num); //绑定下拉框 @Select("select * from product") public List<Product> getList(); //添加 public int addsale(Sale sale); }
saledao.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.mckz.dao.SaleDao"> <resultMap id="getAllSales" type="com.mckz.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.mckz.entity.Product"> <id column="pid" property="pid"></id> <result column="productName" property="productName"></result> </association> <association property="user" javaType="com.mckz.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> <insert id="addsale"> INSERT INTO sale(price,quantity,totalPrice,saleDate,userId,productId) VALUE(#{price},#{quantity},#{totalPrice},#{saleDate},#{userId},#{productId}) </insert> </mapper>
service:productservice
package com.mckz.service; import com.mckz.entity.Product; import java.util.List; public interface ProductService { //查库存 public List<Product> getList(); 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 PageInfo<Sale> getsale(@Param("num") Integer num, @Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize); //绑定下拉框 public List<Product> 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 List<Product> getList() { List<Product> 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 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; } }
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