Spring-serves.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 扫描Serves,Jopo,Dao,configlocation包的注解-->
<context:component-scan base-package="Serves,Jopo,Dao,configlocation" ></context:component-scan>
<!-- 导入druid.properties配置文件-->
<context:property-placeholder location="classpath:druid.properties"></context:property-placeholder>
<!-- 生成配置文件的链接-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" ></property>
<property name="url" value="jdbc:mysql://localhost:3306/java0412"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="maxWait" value="5000"></property>
<property name="maxActive" value="10"></property>
</bean>
<!-- 生成sqlSessionFactory类,该类根据id是dataSource的链接,对应的名字typeAliasesPackage包的
名字是Jopo,mapperLocations对应的xml文件,plugins对应的分页-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="Jopo"></property>
<property name="mapperLocations" value="classpath:mapper/*Mapper.xml"></property>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
helperDialect=mysql
reasonable=true
</value>
</property>
</bean>
</array>
</property>
</bean>
<!-- 生成sqlSession的实体类sqlSessionFactoryBean引入工厂类的实体id,对应实体类包名mapper-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
<property name="basePackage" value="mapper"></property>
</bean>
<!-- 生成事务类-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 根据方法实现事务要不要添加事务-->
<!-- <tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">-->
<!-- <tx:attributes>-->
<!-- <tx:method name="insert*" propagation="REQUIRED"/>-->
<!-- <tx:method name="updata*" propagation="REQUIRED"/>-->
<!-- <tx:method name="delete*" propagation="REQUIRED"/>-->
<!-- <tx:method name="select*" propagation="SUPPORTS"/>-->
<!-- </tx:attributes>-->
<!-- </tx:advice>-->
<!-- <aop:config>-->
<!-- <aop:pointcut id="cut" expression="execution(* Serves.*.*(..))"/>-->
<!-- <aop:advisor advice-ref="txAdvice" pointcut-ref="cut"></aop:advisor>-->
<!-- </aop:config>-->
<!-- 根据注解来实现判断是否用事务-->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"></tx:annotation-driven>
</beans>
serves实现类
package Serves.impl;
import Dao.impl.ProductsIDaompl;
import Jopo.Category;
import Jopo.Products;
import Serves.ProductsService;
import Utils.MyBatisUtil;
import Utils.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import mapper.CategoryMapper;
import mapper.ProductsMapper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
//生成对应的实现类
@Service
public class ProductsSercivesImpl implements ProductsService {
//注入属性对应的实现类
@Autowired
ProductsMapper productsMapper=null;
@Override
//默认是开启事务
@Transactional
public int addProducts(Products products) {
int result=0;
if (productsMapper.selectProductsById(products.getPid())==null){
result=productsMapper.insertProducts(products);
// System.out.println(1/0);
}else {
result=-1;
}
return result;
}
@Override
@Transactional
public int doryProducts(String pid) {
int result=0;
if (productsMapper.selectProductsById(pid)!=null){
result=productsMapper.deleteProductsByPid(pid);
}else {
result=-1;
}
return result;
}
@Override
@Transactional
public int dorymangeProducts(String[] pids) {
int result=0;
for (int i = 0; i < pids.length; i++) {
String pid=pids[i];
result=productsMapper.deleteProductsByPid(pid);
}
return result;
}
@Override
@Transactional
public int reviseProducts(Products products) {
int result=0;
if (productsMapper.selectProductsById(products.getPid())!=null){
result=productsMapper.updateProducts(products);
}else {
result=-1;
}
return result;
}
@Override
@Transactional(propagation = Propagation.SUPPORTS)
public List<Products> findProducts() {
List<Products> list=null;
list=new ArrayList<>();
list=productsMapper.selectAllProducts();
return list;
}
@Override
@Transactional(propagation = Propagation.SUPPORTS)
public Products findbyidProducts(String pid) {
Products products=null;
products=productsMapper.selectProductsById(pid);
return products;
}
@Transactional(propagation = Propagation.SUPPORTS)
public PageInfo<Products> findProductsbypage(int pageindex,int pagesize) {
PageInfo<Products> pageInfo=null;
PageHelper.startPage(pageindex,pagesize);
List<Products> productsList= productsMapper.selectAllProducts();
pageInfo=new PageInfo<>(productsList);
return pageInfo;
}
}
对应的Servlet
package controller;
import Jopo.Category;
import Jopo.Information;
import Jopo.Pagemange;
import Jopo.Products;
import Serves.CateGoryService;
import Serves.ProductsService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServlet;
import java.util.List;
//Servlet实现类
@RestController
//Servlet路径
@RequestMapping(path = "/customer/products")
public class ProductsController extends HttpServlet {
@Autowired
CateGoryService cateGoryService;
@Autowired
ProductsService productsService;
@GetMapping(path = "/toadd")
public Information toAdd() {
List<Category> list=cateGoryService.findCategory();
Information information=new Information();
information.setData(list);
return information;
}
//Servlet方法路径
@PostMapping(path = "/doadd")
public Information doAdd(Products products) {
int rul=productsService.addProducts(products);
Information information=new Information();
information.setData(rul);
if(rul>0){
}else if(rul==-1){
information.setInfstr("该工号已经纯在");
}else if(rul==-2){
information.setInfstr("添加失败");
}
return information;
}
@GetMapping(path = "/findall/{pageindex}")
//@PathVariable通过路径传入参数用{}接收,将接收的值赋值给int pageindex参数
public Information findAll(@PathVariable(name = "pageindex") int pageindex ){
int pagesize=3;
PageInfo<Products> pageInfo=productsService.findProductsbypage(pageindex,pagesize);
Pagemange pagemange=new Pagemange();
pagemange.setPagecount(pageInfo.getPages());
pagemange.setPagesize(pagesize);
pagemange.setPageindex(pageindex);
pagemange.setData(pageInfo.getList());
Information information=new Information();
information.setData(pagemange);
return information;
}
@GetMapping(path = "/toupdate")
//将传入的@RequestParam(value ="pid")值"pid"作为传输赋值给String id
public Information toupdate(@RequestParam(value ="pid") String id) {
Products products=productsService.findbyidProducts(id);
Information information=new Information();
information.setData(products);
return information;
}
@PostMapping(path = "/doupdate")
public Information doupdate(Products products) {
int rul=productsService.reviseProducts(products);
Information information=new Information();
information.setData(rul);
if(rul>0){
}else {
information.setInfstr("修改失败");
}
return information;
}
@GetMapping(path = "/dory")
public Information dory(String pid ) {
int rul=productsService.doryProducts(pid);
Information information=new Information();
information.setData(rul);
if(rul>0){
}else {
information.setInfstr("删除失败");
}
return information;
}
@PostMapping(path = "/dorymany")
public Information dorymany(String[] pids) {
int rul=productsService.dorymangeProducts(pids);
Information information=new Information();
information.setData(rul);
ObjectMapper objectMapper=new ObjectMapper();
if(rul>0){
}else {
information.setInfstr("删除失败");
}
return information;
}
}