请老师看一下,之前老师说的重启和刷新maven结构都不行
代码:
实体类:
order:
package com.qin.os.entity;
import java.util.Date;
public class Order {
private String id;
private String productId;
private Integer number;
private Double price;
private Date createTime;
private Date sendTime;
private Date confirmTime;
private String consignee;
private String consigneePhone;
private String consigneeAddress;
private String status;
public Order() {
}
public Order(String id, String productId, Integer number, Double price, String consignee, String consigneePhone, String consigneeAddress) {
this.id = id;
this.productId = productId;
this.number = number;
this.price = price;
this.consignee = consignee;
this.consigneePhone = consigneePhone;
this.consigneeAddress = consigneeAddress;
}
@Override
public String toString() {
return "Order{" +
"id='" + id + '\'' +
", productId='" + productId + '\'' +
", number=" + number +
", price=" + price +
", createTime=" + createTime +
", sendTime=" + sendTime +
", confirmTime=" + confirmTime +
", consignee='" + consignee + '\'' +
", consigneePhone='" + consigneePhone + '\'' +
", consigneeAddress='" + consigneeAddress + '\'' +
", status='" + status + '\'' +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getSendTime() {
return sendTime;
}
public void setSendTime(Date sendTime) {
this.sendTime = sendTime;
}
public Date getConfirmTime() {
return confirmTime;
}
public void setConfirmTime(Date confirmTime) {
this.confirmTime = confirmTime;
}
public String getConsignee() {
return consignee;
}
public void setConsignee(String consignee) {
this.consignee = consignee;
}
public String getConsigneePhone() {
return consigneePhone;
}
public void setConsigneePhone(String consigneePhone) {
this.consigneePhone = consigneePhone;
}
public String getConsigneeAddress() {
return consigneeAddress;
}
public void setConsigneeAddress(String consigneeAddress) {
this.consigneeAddress = consigneeAddress;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
product:
package com.qin.os.entity;
public class Product {
private String id;
private String title;
private Double price;
private Integer stock;
private String status;
public Product() {
}
public Product(String id, String title, Double price, Integer stock, String status) {
this.id = id;
this.title = title;
this.price = price;
this.stock = stock;
this.status = status;
}
@Override
public String toString() {
return "Product{" +
"id='" + id + '\'' +
", title='" + title + '\'' +
", price=" + price +
", stock=" + stock +
", status='" + status + '\'' +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Integer getStock() {
return stock;
}
public void setStock(Integer stock) {
this.stock = stock;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
dao包:
orderDao:
package com.qin.os.dao;
import com.qin.os.entity.Order;
import java.util.List;
public interface OrderDao {
void insert(Order order);
void update(Order order);
void delete(String id);
Order findOne(String id);
List<Order> findAll();
}
orderDaoImpl:
package com.qin.os.dao.impl;
import com.qin.os.dao.OrderDao;
import com.qin.os.entity.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Repository
public class OrderDaoImpl implements OrderDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void insert(Order order) {
String sql="insert into orders values(?,?,?,?,?,?,?,?,?,?,?)";
jdbcTemplate.update(sql,order.getId(),order.getProductId(),order.getNumber(),order.getPrice(),order.getCreateTime(),order.getSendTime(),order.getConfirmTime(),order.getConsignee(),order.getConsigneePhone(),order.getConsigneeAddress(),order.getStatus());
}
public void update(Order order) {
String sql="update orders set product_id=?,number=?,price=?,create_time=?,send_time=?,confirm_time=?,consignee=?,consignee_phone=?,consignee_address=?,status=? where id=?";
jdbcTemplate.update(sql,order.getProductId(),order.getNumber(),order.getPrice(),order.getCreateTime(),order.getSendTime(),order.getConfirmTime(),order.getConsignee(),order.getConsigneePhone(),order.getConsigneeAddress(),order.getStatus(),order.getId());
}
public void delete(String id) {
String sql="delete from orders where id=?";
jdbcTemplate.update(sql,id);
}
public Order findOne(String id) {
String sql="select *from orders where id=?";
Order order=jdbcTemplate.queryForObject(sql,new OrderRowMapper(),id);
return order;
}
public List<Order> findAll() {
String sql="select *from orders";
List<Order> list=jdbcTemplate.query(sql,new OrderRowMapper());
return list;
}
private class OrderRowMapper implements RowMapper<Order> {
public Order mapRow(ResultSet resultSet, int i) throws SQLException {
Order order=new Order();
order.setId(resultSet.getString("id"));
order.setProductId(resultSet.getString("product_id"));
order.setNumber(resultSet.getInt("number"));
order.setPrice(resultSet.getDouble("price"));
order.setCreateTime(resultSet.getTime("create_time"));
order.setSendTime(resultSet.getTime("send_time"));
order.setConfirmTime(resultSet.getTime("confirm_time"));
order.setConsignee(resultSet.getString("consignee"));
order.setConsigneePhone(resultSet.getString("consignee_phone"));
order.setConsigneeAddress(resultSet.getString("consignee_address"));
order.setStatus(resultSet.getString("status"));
return order;
}
}
}
productDao:
package com.qin.os.dao;
import com.qin.os.entity.Order;
import com.qin.os.entity.Product;
import java.util.List;
public interface ProductDao {
void insert(Product product);
void update(Product product);
void delete(String id);
Product findOne(String id);
List<Product> findAll();
}
productDaoImpl:
package com.qin.os.dao.impl;
import com.qin.os.dao.ProductDao;
import com.qin.os.entity.Order;
import com.qin.os.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Repository
public class ProductDaoImpl implements ProductDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void insert(Product product) {
String sql="insert into products values(?,?,?,?,?)";
jdbcTemplate.update(sql,product.getId(),product.getTitle(),product.getPrice(),product.getStock(),product.getStatus());
}
public void update(Product product) {
String sql="update products set title=?,price=?,stock=?,status=? where id=?";
jdbcTemplate.update(sql,product.getTitle(),product.getPrice(),product.getStock(),product.getStatus(),product.getId());
}
public void delete(String id) {
String sql="delete from products where id=?";
jdbcTemplate.update(sql,id);
}
public Product findOne(String id) {
String sql="select *from products where id=?";
Product product=jdbcTemplate.queryForObject(sql,new ProductsRowMapper());
return product;
}
public List<Product> findAll() {
String sql="select *from products";
List<Product> list=jdbcTemplate.query(sql,new ProductsRowMapper());
return list;
}
private class ProductsRowMapper implements RowMapper<Product> {
public Product mapRow(ResultSet resultSet, int i) throws SQLException {
Product product=new Product();
product.setId(resultSet.getString("id"));
product.setTitle(resultSet.getString("title"));
product.setPrice(resultSet.getDouble("price"));
product.setStock(resultSet.getInt("stock"));
product.setStatus(resultSet.getString("status"));
return product;
}
}
}
service包:
package com.qin.os.service;
import com.qin.os.entity.Order;
import org.springframework.stereotype.Service;
public interface OrderService {
void add(Order order);
}
orderServiceImpl:
package com.qin.os.service.impl1;
import com.qin.os.dao.OrderDao;
import com.qin.os.dao.ProductDao;
import com.qin.os.entity.Order;
import com.qin.os.entity.Product;
import com.qin.os.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import java.util.Date;
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderDao orderDao;
@Autowired
private ProductDao productDao;
@Autowired
private PlatformTransactionManager transactionManager;
@Autowired
private TransactionDefinition transactionDefinition;
public void add(Order order) {
order.setStatus("待发货");
order.setCreateTime(new Date());
TransactionStatus transactionStatus=transactionManager.getTransaction(transactionDefinition);
try {
orderDao.insert(order);
Product product = productDao.findOne(order.getProductId());
product.setStock(product.getStock() - order.getNumber());
productDao.update(product);
System.out.println("aaa");
transactionManager.commit(transactionStatus);
}catch (Exception e)
{
transactionManager.rollback(transactionStatus);
}
}
}
spring-dao.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: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">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/os?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8"/>
<property name="username" value="root"/>
<property name="password" value="qfc111620"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<context:component-scan base-package="com.qin.os.dao"/>
</beans>
spring-service.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: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">
<import resource="spring-dao.xml"/>
<context:component-scan base-package="com.qin.os.service.impl1"/>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="transactionDefinition" class="org.springframework.transaction.support.DefaultTransactionDefinition">
<property name="propagationBehaviorName" value="PROPAGATION_REQUIRED"/>
</bean>
</beans>
test:
package com.qin.os.service;
import com.qin.os.entity.Order;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-service.xml")
public class Test {
@Autowired
private OrderService orderService;
@org.junit.Test
public void demo1()
{
Order order=new Order("100003","100002",1,1799.9,"张三","1111111","jsjaajajja");
orderService.add(order);
}
}
错误信息:
E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\bin\java.exe -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:59371,suspend=y,server=n -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:C:\Users\Administrator\AppData\Local\JetBrains\IntelliJIdea2020.1\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8 -classpath "E:\IDEA\IntelliJ IDEA 2020.1.2\lib\idea_rt.jar;E:\IDEA\IntelliJ IDEA 2020.1.2\plugins\junit\lib\junit5-rt.jar;E:\IDEA\IntelliJ IDEA 2020.1.2\plugins\junit\lib\junit-rt.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\charsets.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\deploy.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\ext\access-bridge-64.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\ext\cldrdata.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\ext\dnsns.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\ext\jaccess.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\ext\jfxrt.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\ext\localedata.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\ext\nashorn.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\ext\sunec.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\ext\sunjce_provider.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\ext\sunmscapi.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\ext\sunpkcs11.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\ext\zipfs.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\javaws.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\jce.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\jfr.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\jfxswt.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\jsse.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\management-agent.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\plugin.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\resources.jar;E:\eclipse-jee-2019-12-R-win32-x86_64-1\jre.1.8\jre\lib\rt.jar;E:\IDEA\IDEAwork\os\target\test-classes;E:\IDEA\IDEAwork\os\target\classes;E:\maven-repo\mysql\mysql-connector-java\8.0.12\mysql-connector-java-8.0.12.jar;E:\maven-repo\com\google\protobuf\protobuf-java\2.6.0\protobuf-java-2.6.0.jar;E:\maven-repo\junit\junit\4.12\junit-4.12.jar;E:\maven-repo\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;E:\maven-repo\org\springframework\spring-core\5.2.7.RELEASE\spring-core-5.2.7.RELEASE.jar;E:\maven-repo\org\springframework\spring-jcl\5.2.7.RELEASE\spring-jcl-5.2.7.RELEASE.jar;E:\maven-repo\org\springframework\spring-beans\5.2.7.RELEASE\spring-beans-5.2.7.RELEASE.jar;E:\maven-repo\org\springframework\spring-context\5.2.7.RELEASE\spring-context-5.2.7.RELEASE.jar;E:\maven-repo\org\springframework\spring-expression\5.2.7.RELEASE\spring-expression-5.2.7.RELEASE.jar;E:\maven-repo\org\springframework\spring-aop\5.2.7.RELEASE\spring-aop-5.2.7.RELEASE.jar;E:\maven-repo\org\springframework\spring-jdbc\5.2.7.RELEASE\spring-jdbc-5.2.7.RELEASE.jar;E:\maven-repo\org\springframework\spring-tx\5.2.7.RELEASE\spring-tx-5.2.7.RELEASE.jar;E:\maven-repo\org\springframework\spring-test\5.2.7.RELEASE\spring-test-5.2.7.RELEASE.jar;E:\maven-repo\org\aspectj\aspectjweaver\1.9.5\aspectjweaver-1.9.5.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.qin.os.service.Test,demo1
Connected to the target VM, address: '127.0.0.1:59371', transport: 'socket'
七月 22, 2020 4:13:30 下午 org.springframework.test.context.support.AbstractTestContextBootstrapper getDefaultTestExecutionListenerClassNames
信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
七月 22, 2020 4:13:30 下午 org.springframework.test.context.support.AbstractTestContextBootstrapper getTestExecutionListeners
信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@6d2a209c, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@75329a49, org.springframework.test.context.support.DirtiesContextTestExecutionListener@161479c6, org.springframework.test.context.transaction.TransactionalTestExecutionListener@4313f5bc, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@7f010382, org.springframework.test.context.event.EventPublishingTestExecutionListener@1e802ef9]
Disconnected from the target VM, address: '127.0.0.1:59371', transport: 'socket'
Process finished with exit code 0
项目结构:
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 323 份
- 解答问题 8263 个
本阶段将带你学习主流框架SSM,以及SpringBoot ,打通成为Java工程师的最后一公里!
了解课程


恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星