懒加载JDBC Connection will not be managed by Spring

你上来晒太阳的 你上来晒太阳的 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
问题描述:查询用户信息时想级联查出用户订单以及订单详情,在查询用户的时候JDBC是will be managed by Spring,但懒加载用户订单以及订单详情时就will not be managed by Spring了
2018-07-30 12:25:33.498 DEBUG [main]org.mybatis.spring.SqlSessionUtils.getSqlSession():104 -Creating a new SqlSession
2018-07-30 12:25:33.502 DEBUG [main]org.mybatis.spring.SqlSessionUtils.getSqlSession():120 -Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@62010f5c]
2018-07-30 12:25:33.510 DEBUG [main]org.mybatis.spring.transaction.SpringManagedTransaction.openConnection():86 -JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@558bdf1f] will be managed by Spring
2018-07-30 12:25:33.512 DEBUG [main]com.dsy.dao.UserMapper.selectByPrimaryKey.debug():139 -==> Preparing: select user_id, user_name, user_password from user where user_id = ?
2018-07-30 12:25:33.528 DEBUG [main]com.dsy.dao.UserMapper.selectByPrimaryKey.debug():139 -==> Parameters: 1(Integer)
2018-07-30 12:25:33.622 DEBUG [main]com.dsy.dao.UserMapper.selectByPrimaryKey.debug():139 -<== Total: 1
2018-07-30 12:25:33.637 DEBUG [main]org.mybatis.spring.SqlSessionUtils.closeSqlSession():163 -Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@62010f5c]
2018-07-30 12:25:33.637 DEBUG [main]org.mybatis.spring.SqlSessionUtils.beforeCommit():261 -Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@62010f5c]
2018-07-30 12:25:33.637 DEBUG [main]org.mybatis.spring.SqlSessionUtils.beforeCompletion():287 -Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@62010f5c]
2018-07-30 12:25:33.637 DEBUG [main]org.mybatis.spring.SqlSessionUtils.beforeCompletion():292 -Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@62010f5c]
2018-07-30 12:25:33.637 DEBUG [main]org.mybatis.spring.transaction.SpringManagedTransaction.openConnection():86 -JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@e8df99a] will not be managed by Spring
2018-07-30 12:25:33.637 DEBUG [main]com.dsy.dao.UserOrderMapper.selectByPrimaryKey.debug():139 -==> Preparing: select order_id, user_id, address_id, total_price, order_time, order_state from user_order where order_id = ?
2018-07-30 12:25:33.637 DEBUG [main]com.dsy.dao.UserOrderMapper.selectByPrimaryKey.debug():139 -==> Parameters: 1(Integer)
2018-07-30 12:25:33.653 DEBUG [main]com.dsy.dao.UserOrderMapper.selectByPrimaryKey.debug():139 -<== Total: 1
2018-07-30 12:25:33.653 DEBUG [main]org.mybatis.spring.transaction.SpringManagedTransaction.openConnection():86 -JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@45fd9a4d] will not be managed by Spring
2018-07-30 12:25:33.653 DEBUG [main]com.dsy.dao.OrderItemsMapper.selectByPrimaryKey.debug():139 -==> Preparing: select item_id, good_id, order_id, buy_num from order_items where item_id = ?
2018-07-30 12:25:33.653 DEBUG [main]com.dsy.dao.OrderItemsMapper.selectByPrimaryKey.debug():139 -==> Parameters: 1(Integer)
2018-07-30 12:25:33.669 DEBUG [main]com.dsy.dao.GoodMapper.selectByPrimaryKey.debug():139 -====> Preparing: select good_id, category_id, good_name, good_price, good_img, good_state from good where good_id = ?
2018-07-30 12:25:33.669 DEBUG [main]com.dsy.dao.GoodMapper.selectByPrimaryKey.debug():139 -====> Parameters: 1(Integer)
2018-07-30 12:25:33.669 DEBUG [main]com.dsy.dao.GoodMapper.selectByPrimaryKey.debug():139 -<==== Total: 1
2018-07-30 12:25:33.669 DEBUG [main]com.dsy.dao.OrderItemsMapper.selectByPrimaryKey.debug():139 -<== Total: 1
ASUS_001


Spring配置
<?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:tx="http://www.springframework.org/schema/tx"
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">

<!-- 扫描service包下所有使用注解的类型 -->
<context:component-scan base-package="com.dsy.service"/>

<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource" />
</bean>

<!-- 配置基于注解的声明式事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>


UserMapper.xml:
<resultMap id="BaseResultMap" type="com.dsy.entity.User" >
<id column="user_id" property="userId" jdbcType="INTEGER" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="user_password" property="userPassword" jdbcType="VARCHAR" />
<collection property="orders" column="user_id" select="com.dsy.dao.UserOrderMapper.selectByPrimaryKey" fetchType="lazy"/>
</resultMap>

<sql id="Base_Column_List" >
user_id, user_name, user_password
</sql>


<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from user
where user_id = #{userId,jdbcType=INTEGER}
</select>

UserOrderMapper.xml
<resultMap id="BaseResultMap" type="com.dsy.entity.UserOrder" >
<id column="order_id" property="orderId" jdbcType="INTEGER" />
<result column="user_id" property="userId" jdbcType="INTEGER" />
<result column="address_id" property="addressId" jdbcType="INTEGER" />
<result column="total_price" property="totalPrice" jdbcType="DOUBLE" />
<result column="order_time" property="orderTime" jdbcType="TIMESTAMP" />
<result column="order_state" property="orderState" jdbcType="INTEGER" />
<collection property="items" column="order_id" select="com.dsy.dao.OrderItemsMapper.selectByPrimaryKey"/>
</resultMap>
<sql id="Base_Column_List" >
order_id, user_id, address_id, total_price, order_time, order_state
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from user_order
where order_id = #{orderId,jdbcType=INTEGER}
</select>

BaseServiceImpl:
package com.dsy.service.impl;

import com.dsy.dao.BaseMapper;
import com.dsy.service.BaseService;
import org.springframework.transaction.annotation.Transactional;

/**
* Created by dsy on 2018/7/29
* Package com.dsy.service.impl
*/
//一定要加上否则下面的方法不会被事务控制
@Transactional(value = "transactionManager")
public abstract class BaseServiceImpl<T> implements BaseService<T> {

public abstract BaseMapper<T> getMapper();

@Override
public int deleteByPrimaryKey(Integer id) {
return getMapper().deleteByPrimaryKey(id);
}

@Override
public int insert(T record) {
return getMapper().insert(record);
}

@Override
public int insertSelective(T record) {
return getMapper().insertSelective(record);
}

@Override
public T selectByPrimaryKey(Integer id) {
return getMapper().selectByPrimaryKey(id);
}

@Override
public int updateByPrimaryKeySelective(T record) {
return getMapper().updateByPrimaryKeySelective(record);
}

@Override
public int updateByPrimaryKey(T record) {
return getMapper().updateByPrimaryKey(record);
}
}


UserServiceImpl:
package com.dsy.service.impl;

import com.dsy.dao.BaseMapper;
import com.dsy.dao.UserMapper;
import com.dsy.entity.User;
import com.dsy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* Created by dsy on 2018/7/29
* Package com.dsy.service.impl
*/
@Service
@Transactional(value = "transactionManager")
public class UserServiceImpl extends BaseServiceImpl implements UserService {

@Autowired
private UserMapper userMapper;

@Override
public BaseMapper<User> getMapper() {
return userMapper;
}



}


Test:

import com.dsy.entity.OrderItems;
import com.dsy.entity.User;
import com.dsy.entity.UserOrder;
import com.dsy.service.UserService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
* Created by dsy on 2018/7/28
* Package PACKAGE_NAME
*/
public class UserTest extends BaseTest {

@Autowired
private UserService userService;

@Test
public void testFindUserById(){
User user = (User) userService.selectByPrimaryKey(1);
List<UserOrder> orders = user.getOrders();
for (UserOrder order : orders){
List<OrderItems> items = order.getItems();
for (OrderItems items1:items){
System.out.println(items1.getGood().getGoodName());
}
}
}

}

日志打印结果在最上面
...全文

相关推荐

Springboot+MySQL,出现Could not get JDBC Connection

Caused by: org.apache.ibatis.exceptions.PersistenceException: Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO) The error m

weixin_38732425

Spring Data JDBC与JDBC的区别

JDBC规范     java.sql和javax.sql两个包中的类与接口(天龙八部):     DataSource:数据源     DriverManager:驱动管理     Driver:JDBC驱动     Connection:数据库连接     Statement:语句,执行SQL     PrepareStatement:预编译语句,性能更好     CallableStatement:调用存储过程     ResultSet:结果集,封装了多条记录     JDBC数据库连接池/Connection Pool         DBCP:apache tomcat内置   

weixin_38515270

Spring boot连接oracle数据库JDBC配置步骤

连接的是oracle 11g,配置成功,查询,更新,都测试成功了

mailwy163

Spring5源码之JDBC

这篇文章主要介绍了Spring5源码之JDBC,基于上一篇Spring5之JDBC使用案例来一步步剖析jdbcTemplate操作数据库的源码。需要的朋友可以参考一下。 1、save/update功能实现 在UserServiceImpl中jdbcTemplate的初始化是从setDataSource方法开始的,DataSource实例通过参数注入,DataSource的创建过程是引入第三方的连接池。 DataSource是整个数据库操作的基础,里面封装了整个数据库的连接信息。我们首先以保存实体类为例进行代码跟踪。 public void save(User user) { jdbcTemp

weixin_38604951

数据池连接Name jdbc is not bound in this Context解决方案

配置tomcat7sqlserver数据连接池java 利用数据源对象建立数据库连接时的错误: Name jdbc is not bound in this Context 解决方案

liutingna86
评论 5
收藏 已收藏
分享
返回顶部
你上来晒太阳的
Java EE 67,535 成员 225,843 内容