67,550
社区成员




@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-mybatis.xml")
public class TestSellingOrderDao {
@Autowired
private ISellingOrderService sellingOrderService;
@Autowired
private IDealedBookDao dealedBookDao;
@Test
public void sellDaoShouldNotBeNull() {
assertNotNull(sellingOrderDao);
DealedBook dealedBook = new DealedBook();
dealedBook.setBookId(1);
dealedBook.setDealedNum("DealedNum");
dealedBook.setSellingPrice(6);
dealedBook.setRentalPrice(12);
dealedBook.setDistrictId(1);
dealedBook.setUserId(1);
dealedBook.setDatetime(new Date());
for (int i = 1; i <= 1000; i++)
dealedBookDao.insert(dealedBook);
SellingOrder order = new SellingOrder();
order.setUserId(2);
order.setDeliveryMethodId(1); //
order.setBookId(dealedBook.getBookId()); //
order.setAmount(5);
order.setDatetime(new Date());
order.setPayed(1);
// 取书号:学校编号+日期+待售书的ID
order.setTakingBookNum( "" +
"SYSU" +
new SimpleDateFormat("yyyyMMdd").format(new Date()) +
String.format("%05d", dealedBook.getId())
);
new Thread() {
@Override
public void run() {
for (int i = 1; i <= 100; i++)
sellingOrderService.insertOrder(order); // 执行事务
}
}.start();
for (int i = 1; i <= 100; i++)
sellingOrderService.insertOrder(order); // 执行事务
}
}
package com.databasegroup.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.databasegroup.dao.IDealedBookDao;
import com.databasegroup.dao.ISellingOrderDao;
import com.databasegroup.exception.NoEnoughBooksException;
import com.databasegroup.model.DealedBook;
import com.databasegroup.model.SellingOrder;
import com.databasegroup.service.ISellingOrderService;
@Service("sellingOrderService")
public class SellingOrderServiceImpl implements ISellingOrderService {
@Resource
private ISellingOrderDao sellingOrderDao;
@Resource
private IDealedBookDao dealedBookDao;
@Override
public void insertOrder(SellingOrder sellingOrder) {
StringBuilder dealedBookId = new StringBuilder();
int bookId = sellingOrder.getBookId();
int amount = sellingOrder.getAmount();
List<DealedBook> dealedBooks =
dealedBookDao.getNoDealedBookByBookIdAndAmount(bookId, amount);
if (dealedBooks.size() != amount)
{
throw new NoEnoughBooksException("没有足够的书本"); // 错误抛出后,事务会回滚
}
for (DealedBook dealedBook : dealedBooks) {
dealedBook.setSelled(1);
dealedBook.setSelledDatetime(new Date());
dealedBookId.append("" + dealedBook.getId() + '|');
dealedBookDao.update(dealedBook); // 修改另外表的数据
}
if (dealedBookId.length() > 0)
dealedBookId = dealedBookId.deleteCharAt(dealedBookId.length()-1);
// 设置dealed_book_id字段的值
sellingOrder.setDealedBookIds(dealedBookId.toString());
sellingOrderDao.insert(sellingOrder); /
}
}
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<!-- com.databasegroup下的组件(bean),并排除controller -->
<context:component-scan base-package="com.databasegroup">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<mvc:annotation-driven />
<!-- mybatis 配置1:读参数 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<!-- mybatis 配置2:数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${jdbc.initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${jdbc.maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${jdbc.minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${jdbc.maxWait}"></property>
</bean>
<!-- spring和MyBatis整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描映射文件 -->
<property name="mapperLocations" value="classpath:/mapper/*.xml"></property>
<!-- mybatis配置 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 使用别名 -->
<property name="typeAliasesPackage" value="com.databasegroup.model"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.databasegroup.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- spring declarative transaction management -->
<aop:config>
<aop:pointcut id="allServiceMethods"
expression="execution(* com.databasegroup.service.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="allServiceMethods" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="reduce*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
</beans>
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 扫描com.databasegroup.fortest.controller下的controller -->
<context:component-scan base-package="com.databasegroup.controller">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 注解驱动 -->
<mvc:annotation-driven />
<!-- 处理静态资源的请求 -->
<mvc:resources location="/WEB-INF/views/css/" mapping="/css/**" />
<mvc:resources location="/WEB-INF/views/js/" mapping="/js/**" />
<mvc:resources location="/images/" mapping="/images/**" />
<mvc:resources location="/WEB-INF/views/admin/" mapping="/admin/**" />
<!-- 不懂标记 -->
<bean
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<!-- 不懂标记 -->
<bean
class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 上传文件 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<!-- 最大内存大小 -->
<property name="maxInMemorySize" value="10240" />
<!-- 最大文件大小,-1为不限制大小 -->
<property name="maxUploadSize" value="-1" />
</bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/center"/>
<mvc:mapping path="/books"/>
<mvc:mapping path="/alipay/**"/>
<mvc:mapping path="/buy_rent"/>
<mvc:mapping path="/pay/**"/>
<mvc:mapping path="/rent/**"/>
<mvc:mapping path="/success/**"/>
<bean class="com.databasegroup.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/backend/**"/>
<bean class="com.databasegroup.interceptor.AdminLoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
@Override
public synchronized void insertOrder(SellingOrder sellingOrder) {
StringBuilder dealedBookId = new StringBuilder();
int bookId = sellingOrder.getBookId();
int amount = sellingOrder.getAmount();
List<DealedBook> dealedBooks =
dealedBookDao.getNoDealedBookByBookIdAndAmount(bookId, amount);
if (dealedBooks.size() != amount)
{
throw new NoEnoughBooksException("没有足够的书本"); // 错误抛出后,事务会回滚
}
for (DealedBook dealedBook : dealedBooks) {
dealedBook.setSelled(1);
dealedBook.setSelledDatetime(new Date());
dealedBookId.append("" + dealedBook.getId() + '|');
dealedBookDao.update(dealedBook); // 修改另外表的数据
}
if (dealedBookId.length() > 0)
dealedBookId = dealedBookId.deleteCharAt(dealedBookId.length()-1);
// 设置dealed_book_id字段的值
sellingOrder.setDealedBookIds(dealedBookId.toString());
sellingOrderDao.insert(sellingOrder); /
}