你先把 private SqlSession SqlSession;  首字母小写

解决方案 »

  1.   


    我改了小写也不行啊 xml也要改<bean id="userDaoimpl" class="com.springmvc.dao.UserDaoImpl" >    
        <property name="SqlSession" ref="SqlSession"/>
    </bean>
      

  2.   


    我改了小写也不行啊 xml也要改<bean id="userDaoimpl" class="com.springmvc.dao.UserDaoImpl" >    
        <property name="SqlSession" ref="SqlSession"/>
    </bean>
    我都改了.. <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/mybaitis"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
    </bean>
    <!-- 
    2. mybatis的SqlSession的工厂: SqlSessionFactoryBean 
    dataSource / typeAliasesPackage
    -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="datasource"/>
    <property name="typeAliasesPackage" value="com.springmvc.mybaitsmodel"/> </bean> <bean id="SqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory" />
    <!-- <constructor-arg index="1" value="BATCH" />如果想要进行批量操作可加入这个属性 -->
    </bean>
    <bean id="userDaoimpl" class="com.springmvc.dao.UserDaoImpl" >
    <property name="sqlSession" ref="SqlSession"/>
    </bean>   
    private SqlSession sqlSession;
    public SqlSession getSqlSession() {
    return sqlSession;
    }
    public void setSqlSession(SqlSession sqlSession) {
    System.out.println("set:"+sqlSession);
    this.sqlSession = sqlSession;
    } @Override
    public User findUserById(int id) {

    System.out.println("usering..."+sqlSession);

    User user = sqlSession.selectOne(User.class.getName()+".selectUserByid", id);
    System.out.println(user.getName());

    return user;
    }这样就好用:ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/config/beans.xml");
    UserDaoImpl service = (UserDaoImpl)ctx.getBean("userDaoimpl");
    为啥啊
      

  3.   

    现在正常调用时User user = sqlSession.selectOne(User.class.getName()+".selectUserByid", id);这里的sqlSession为null?
      

  4.   


    通过
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/config/beans.xml");
    UserDaoImpl service = (UserDaoImpl)ctx.getBean("userDaoimpl");
    这种方式调用时不为空,加载tomcat的时候就为空
      

  5.   


    <bean id="SqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    这个地方小写没改
      

  6.   

    注入session的配置没问题,我觉得可能是service调用或注入有问题
      

  7.   

    Spring注入默认采用的是ByName,所有对实体的要求是尽量的参照javaBean规范。
      

  8.   

    tomcat加载spring应该在web.xml文件里配置一下吧。把这个文件/config/beans.xml配置到web.xml里试试呢