[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
问题:java测试类向订单表插入数据,程序没有错误运行也正常,但是查看数据库时发现数据库未插入数据是怎么办?
第一步,先检查确认代码无错误:
public class OrderDaoTest { private OrderDao orderDao = new OrderDaoImpl(); //
保存订单 @Test public void saveOrder() { orderDao.saveOrder(new Order(
"1234123412344", new Date(), new BigDecimal(10000), 0, 1)); orderDao.saveOrder(
new Order("1234123412345", new Date(), new BigDecimal(10000), 0, 1)); System.out
.println("over"); } }
第二步,查看数据库连接配置文件地址是否正确:
username=root password=root url=jdbc:mysql://localhost:3306/book
driverClassName=com.mysql.jdbc.Driver initialSize=5 maxActive=10
第三步,查看连接数据库的工具类,看是否设置了手动提交事务:
/** * 获取数据库连接池中的连接 * @return 如果返回null,说明获取连接失败<br/>有值就是获取连接成功 */ public static
ConnectiongetConnection(){ Connection conn = conns.get(); if (conn == null) {
try { conn = dataSource.getConnection();//从数据库连接池中获取连接 conns.set(conn); //
保存到ThreadLocal对象中,供后面的jdbc操作使用 conn.setAutoCommit(false); // 设置为手动管理事务 } catch (
SQLException e) { e.printStackTrace(); } } return conn; }
数据库连接设置为手动管理事务导致测试类数据插入数据库失败解决方案:
在测试类中手动提交事务就可以解决。
```java public class OrderDaoTest { private OrderDao orderDao = new
OrderDaoImpl(); // 保存订单 @Test public void saveOrder() { try { orderDao.saveOrder
(new Order("1234123412344", new Date(), new BigDecimal(10000), 0, 1)); orderDao.
saveOrder(new Order("1234123412345", new Date(), new BigDecimal(10000), 0, 1));
Connection conn= JdbcUtils.getConnection(); conn.commit(); } catch (Exception e)
{ } } }
`
总结:此次错误主要是由于数据库连接工具类中设置了手动管理事务,在测试类里手动提交就可以了。