Spring注入问题,求大神! 你先把 private SqlSession SqlSession; 首字母小写 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我改了小写也不行啊 xml也要改<bean id="userDaoimpl" class="com.springmvc.dao.UserDaoImpl" > <property name="SqlSession" ref="SqlSession"/></bean> 我改了小写也不行啊 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");为啥啊 现在正常调用时User user = sqlSession.selectOne(User.class.getName()+".selectUserByid", id);这里的sqlSession为null? 通过ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/config/beans.xml"); UserDaoImpl service = (UserDaoImpl)ctx.getBean("userDaoimpl");这种方式调用时不为空,加载tomcat的时候就为空 <bean id="SqlSession" class="org.mybatis.spring.SqlSessionTemplate">这个地方小写没改 注入session的配置没问题,我觉得可能是service调用或注入有问题 Spring注入默认采用的是ByName,所有对实体的要求是尽量的参照javaBean规范。 tomcat加载spring应该在web.xml文件里配置一下吧。把这个文件/config/beans.xml配置到web.xml里试试呢 Java泛型(来就有分) 一个xml的输出问题 MyEclipse6.5 Struts2 错误 写logic:iterate标签,如何美化界面,有的发个漂亮的代码 Tomcat连接池问题 hibernate插入,修改出错 当上Open Source版主,和兄弟们见个面,希望大家多多支持,散分~~~ web界面的工作流处理用什么框架比较好? 谁能帮我一下,我一定给分的 高手进来看看--------JB7.0的字体问题 Spring MVC 做下载功能遇到问题 时间格式验证 奇怪问题
我改了小写也不行啊 xml也要改<bean id="userDaoimpl" class="com.springmvc.dao.UserDaoImpl" >
<property name="SqlSession" ref="SqlSession"/>
</bean>
我改了小写也不行啊 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");
为啥啊
通过
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/config/beans.xml");
UserDaoImpl service = (UserDaoImpl)ctx.getBean("userDaoimpl");
这种方式调用时不为空,加载tomcat的时候就为空
<bean id="SqlSession" class="org.mybatis.spring.SqlSessionTemplate">
这个地方小写没改