java spring的问题找不到datasource 20C

配置文件 <?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

<context:property-placeholder location="file:${user.home}/webapp-conf/oms_empty_config.properties"/>

<context:component-scan base-package="com.dabay.service.web.manager">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

/context:component-scan
task:annotation-driven/

<bean id="springContextUtil" class="com.dabay.service.web.manager.util.SpringContextUtil"/>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mapperLocations" value="classpath:/mapper/*Mapper.xml"/>
    <property name="plugins">
        <array>
            <bean class="com.github.pagehelper.PageHelper">
                <property name="properties">
                    <value>
                        dialect = mysql
                        reasonable = true
                        pageSizeZero = true
                    </value>
                </property>
            </bean>
            <bean class="com.dabay.service.web.manager.interceptor.MybatisInterceptor"/>
        </array>
    </property>
</bean>

<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
    <constructor-arg index="0" ref="sqlSessionFactory"/>
    <constructor-arg index="1" value="BATCH"/>
</bean>

<bean id="propertyConfigurer"  class="com.dabay.service.web.manager.util.PropertyUtils">
    <property name="locations">
        <list>
            <value>file:${user.home}/webapp-conf/oms_empty_config.properties</value>
        </list>
    </property>
</bean>

<!-- 支持上传文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.dabay.service.web.manager.persistence.dao.**"/>
    <property name="properties">
        <value>
            mappers=tk.mybatis.mapper.common.Mapper
        </value>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${sup.driver}"/>
    <property name="jdbcUrl" value="${sup.jdbcUrl}"/>
    <property name="User" value="${sup.user}"/>
    <property name="password" value="${sup.password}"/>
    <property name="maxPoolSize" value="${sup.maxPoolSize}"/>
    <property name="minPoolSize" value="${sup.minPoolSize}"/>
    <property name="preferredTestQuery" value="SELECT 1 FROM dual"/>
</bean>

处理登录的一个类文件
package com.dabay.service.web.manager.Dao;

import com.dabay.service.web.manager.entity.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.sql.DataSource;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**

  • Created by Administrator on 2016/11/28.
    */
    @Repository
    public class JdbcAdminDao
    implements AdminDao,Serializable{

    @Autowired
    private DataSource dataSource;

    public Admin findByCode(
    String username) {
    if (username == null
    || username.equals("")) {
    return null;
    }
    Connection con = null;
    try {
    //创建连接
    con = dataSource.getConnection();
    //执行SQL
    String sql = "select * " +
    "from manage_employee" +
    "where login_name=?";
    PreparedStatement ps =
    con.prepareStatement(sql);
    ps.setString(1,username);
    ResultSet rs = ps.executeQuery();
    //处理结果集
    if (rs.next()) {
    Admin a = new Admin();
    //a.setId(rs.getInt("id"));
    //a.getEmployee_code(rs.getString("employee_code"));
    a.setLogin_name(rs.getString("login_name"));
    a.setPassword(rs.getString("password"));
    return a;
    }
    } catch (Exception e) {
    e.printStackTrace();
    throw new RuntimeException(
    "查询管理员失败!", e);
    } finally {
    try {
    con.close();
    } catch (SQLException e) {
    e.printStackTrace();
    throw new RuntimeException(
    "关闭连接失败!", e);
    }
    }
    return null;
    }
    }

debug的原因是因为 dataSource = null 哪位大神解答一下

3个回答

JdbcAdminDao 里通过sqlsessionFactory 操作

可能是 却jar 还有就是配置不对,建议写个测试,加载一下配置文件,看能不能得到datasource对象

肯定是空值呀,你的dataSource 都注入到 bean id="sqlSessionFactory"的bean里了,也就是说session不在dataSource里了。在JdbcAdminDao类里注入@ respority dataSource,不管用

wq8660389
wq8660389 请问我应该怎么改?新人刚接触spring
接近 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐