mybatis实例化了dao接口,但是spring的@Autowired无法注入

灬橙路灬 2015-10-15 12:45:02
这个是applicationContext.xml配置文件
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 开启IOC注解扫描 -->
<context:component-scan base-package="com.jjkj"/>

<!-- 开启SpringMVC注解扫描 -->
<mvc:annotation-driven/>

<!-- 加载config.properties -->
<util:properties id="jdbc" location="classpath:config.properties"></util:properties>

<!-- 声明连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="#{jdbc.driverName}"></property>
<property name="url" value="#{jdbc.url}"></property>
<property name="username" value="#{jdbc.username}"></property>
<property name="password" value="#{jdbc.password}"></property>
</bean>

<!-- 配置Session工厂 -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入DataSource -->
<property name="dataSource" ref="dataSource"/>
<!-- 需要加载的mapper.xml,该bean被
创建后,会自动加载这些文件。 -->
<property name="mapperLocations" value="classpath:com/jjkj/sql/*.xml"/>
</bean>

<!-- 配置MapperScannerConfigurer -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入需要扫描的包,它会自动扫描
这个包下的接口,然后实现这些接口,
并且会实例化这些接口的实现类。 -->
<property name="basePackage" value="com.jjkj.dao"/>
<property name="annotationClass" value="com.jjkj.annotation.MyBatisRepository"/>
</bean>
</beans>

这个是mybatis的mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.jjkj.dao.UDao">
<!-- 根据用户名查业务员信息 -->
<select id="findByName" parameterType="string" resultType="com.jjkj.entity.User">
select * from _user where name=#{name}
</select>
</mapper>

这个是serviceImpl
@Service
public class UserServiceImpl implements Serializable, UserService{
@Autowired
private UDao dao;

public Result find(String name){
System.out.println(name);
Result result = new Result();
User user = dao.findByName(name);
result.setData(user);
result.setStatus(1);
result.setMsg("有错误");
return result;
};
public static void main(String[] args) {
UserServiceImpl im = new UserServiceImpl();
Result result = im.find("张三");
System.out.println(result.getMsg());
}
}
只要一执行main方法就报User user = dao.findByName(name);空指针.
求各位大神帮忙解决下
...全文
26334 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
abckingaa 2019-03-21
  • 打赏
  • 举报
回复
分享一个ORM框架--Bee. 实体操作与DB的select,update,insert,delete紧密对应.

Bee框架,一个十分钟即可学会的ORM框架. 有着直接操作实体对象的简便性,也可以方便地自己写sql执行.
https://blog.csdn.net/abckingaa/article/details/81176524
新手源 2019-03-08
  • 打赏
  • 举报
回复
<!-- 配置MapperScannerConfigurer -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入需要扫描的包,它会自动扫描
这个包下的接口,然后实现这些接口,
并且会实例化这些接口的实现类。 -->
<property name="basePackage" value="com.jjkj.dao"/>
<property name="annotationClass" value="com.jjkj.annotation.MyBatisRepository"/>

你在这段配置中加入了自定义的注解@MyBatisRepository,所以必须在你的dao接口上也调加该注解,才能注入,你可以试下这么做对不对。其实有一件事不知道我理解的对不对,为什么要加那个自定义注解呢,不加也可以实例化的。
tongkaiming 2018-08-28
  • 打赏
  • 举报
回复
1,修改
@Resource
private UDao udao;
2,给UDao接口加上注解@Repository
3,直接启动main肯定会报错,因为你的spring没有加载进来
ridish 2018-08-27
  • 打赏
  • 举报
回复
1.应该是你的注入不成功
2.dao 成写的问题(看你写的 ,应该没有错)
chenhua141 2018-08-26
  • 打赏
  • 举报
回复
 <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mapping/*Mapper.xml"></property>
</bean>

<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.javaman.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>

楼主你参考下,我这个MapperScannerConfigurer配的是这个属性,而且你跑的时候,用单元测试啊,你直接这么跑是用不了spring环境的!
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:spring/applicationContext.xml"})
public class ProductTest {

@Autowired
private ProductMapper productMapper;

@Test
public void insertAndSelect() {
Product product1 = new Product();
product1.setName("chenhua");
product1.setPrice(19998.9f);
productMapper.addProduct(product1);
System.out.println("product1的ID:"+product1.getId());

Product product2 = productMapper.selectById(product1.getId());
System.out.println("product2的名字:"+product2.getName());
}
}

虽然在编译的时候,mapper会报红,但是能跑起来,能操作数据库的结果!!!
海的颜色~ 2017-12-28
  • 打赏
  • 举报
回复
楼主后面怎么解决了?解决了么?我也是dao是个接口也报这个错需要注入可是怎么注入?
evanweng 2017-08-11
  • 打赏
  • 举报
回复
引用 16 楼 evanweng 的回复:
不管是在main方法还是controller中用UserServiceImpl im = new UserServiceImpl();这种形式,spring应该都不会去帮你注入你的dao的。你应该在controller加入

@Autowired
private UserService userService;
Service用接口去注入
evanweng 2017-08-11
  • 打赏
  • 举报
回复
不管是在main方法还是controller中用UserServiceImpl im = new UserServiceImpl();这种形式,spring应该都不会去帮你注入你的dao的。你应该在controller加入

@Autowired
private UserService userService;
ly_LAN 2017-08-11
  • 打赏
  • 举报
回复
引用 4 楼 wxwmkyy 的回复:
[quote=引用 3 楼 guang505088726 的回复:] 楼主,我阐述下我的观点 我也是个小白 。 我觉得你在main方法里这么做的话,必须手动注入下这个dao类吧。 Spring在main方法里应该是不会去初始化的,也不会去自动注入dao private UDao uDao = SpringUtils.getBean(UDao.class, "uDao"); /** * 获得bean * * @param name * @return */ @SuppressWarnings("unchecked") public static <T> T getBean(Class<T> clazz, String name) { return (T) WebApplicationContextUtils.getWebApplicationContext( ContextLoader.getCurrentWebApplicationContext() .getServletContext()).getBean(name); } 试下 ,我觉得大致思路是对的 代码就不知道了 试下吧
我把controller写了之后,用完整环节来测试,也是报空指针,就是报空指针,我分析了下大概是注入的问题,但是现在找不到这个问题具体出在什么地方[/quote]注入失败 不会报空指针的。启动的时候就会报错,直接就报注入失败了。 把你抱得错 贴出来看看
qq_28147505 2017-08-10
  • 打赏
  • 举报
回复
又是一个出问题解决了,不发自己解决方案的人。
天山鸟飞绝 2016-12-29
  • 打赏
  • 举报
回复
同意5楼的。
风中的追赶 2016-12-28
  • 打赏
  • 举报
回复
mybatis 的方法调用 不就是直接调用接口么 为什么要还要去实现接口呢。 直接在service层 @Autowired pravite Udao udao; 这个接口就可以了
l371890934 2015-10-16
  • 打赏
  • 举报
回复
看你的配置文件,你在自动映射时添加个人注解(annotionClass), 是不是你的dao中没有添加个人注解,所有扫描不到无法自动注入啊?
wuli_1766 2015-10-15
  • 打赏
  • 举报
回复
同意5楼的!
YangSy_001 2015-10-15
  • 打赏
  • 举报
回复
楼主,首先你启动服务器有没有报错 如果没有当我没说 因为我看你 你这里
<!-- 开启IOC注解扫描 -->
<context:component-scan base-package="com.jjkj"/>
少了
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
<context:include-filter type="annotation" expression="org.springframework.stereotype.Component" />
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
这几个注解
灬橙路灬 2015-10-15
  • 打赏
  • 举报
回复
引用 6 楼 lsongiu86 的回复:
UserServiceImpl im = new UserServiceImpl();自己new出来的,当然没注入了,获取spring容器里边,context.getbean()
但是我写了完整的controller通过浏览器传值进来访问,步骤main方法返回给我的也是null
lsongiu86 2015-10-15
  • 打赏
  • 举报
回复
UserServiceImpl im = new UserServiceImpl();自己new出来的,当然没注入了,获取spring容器里边,context.getbean()
w543705344 2015-10-15
  • 打赏
  • 举报
回复
没记错的话 private UDao dao; 如果要实现spring注入要set get 还有就是spring注入在spring文件里面要写 <bean id="名字随意service" class="包名.UserServiceImpl "> <property name="dao"> <ref bean="包名.你dao的实现名" /> </property> </bean> mybatis的mapper.xml技术我就不是很懂了 不过即使用 也要在spring里面写注入吧
灬橙路灬 2015-10-15
  • 打赏
  • 举报
回复
引用 3 楼 guang505088726 的回复:
楼主,我阐述下我的观点 我也是个小白 。 我觉得你在main方法里这么做的话,必须手动注入下这个dao类吧。 Spring在main方法里应该是不会去初始化的,也不会去自动注入dao private UDao uDao = SpringUtils.getBean(UDao.class, "uDao"); /** * 获得bean * * @param name * @return */ @SuppressWarnings("unchecked") public static <T> T getBean(Class<T> clazz, String name) { return (T) WebApplicationContextUtils.getWebApplicationContext( ContextLoader.getCurrentWebApplicationContext() .getServletContext()).getBean(name); } 试下 ,我觉得大致思路是对的 代码就不知道了 试下吧
我把controller写了之后,用完整环节来测试,也是报空指针,就是报空指针,我分析了下大概是注入的问题,但是现在找不到这个问题具体出在什么地方
Only蜡笔小新 2015-10-15
  • 打赏
  • 举报
回复
楼主,我阐述下我的观点 我也是个小白 。 我觉得你在main方法里这么做的话,必须手动注入下这个dao类吧。 Spring在main方法里应该是不会去初始化的,也不会去自动注入dao private UDao uDao = SpringUtils.getBean(UDao.class, "uDao"); /** * 获得bean * * @param name * @return */ @SuppressWarnings("unchecked") public static <T> T getBean(Class<T> clazz, String name) { return (T) WebApplicationContextUtils.getWebApplicationContext( ContextLoader.getCurrentWebApplicationContext() .getServletContext()).getBean(name); } 试下 ,我觉得大致思路是对的 代码就不知道了 试下吧
加载更多回复(2)

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧