jdk_ying 2016-08-25 04:45 采纳率: 0%
浏览 12499

spring+mybatis 配置mapper扫描无法注入

 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:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--获得属性文件里面的键值  -->                  
    <!-- <context:property-placeholder location="classpath*:*.properties" /> -->
    <!--扫描需要容器管理的类-->                   
    <context:component-scan base-package="com.nb"/> 
    <!-- 数据源 -->
    <bean name="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="url">
            <value>jdbc:mysql://127.0.0.1:3306/mynb?useUnicode=true&amp;characterEncoding=UTF-8</value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>123456</value>
        </property>
    </bean>
    <!-- ================================ MyBatis SqlSession配置 ========================================= -->
    <!-- 使用SqlSessionFactoryBean工厂产生SqlSession对象,方便后期注入Dao -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!--  <property name="configLocation" value="classpath:configuration.xml"></property>-->
        <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
        <property name="mapperLocations" value="classpath*:com/nb/dao/*.xml" />
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.nb.dao" />
        <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> -->        
    </bean>
    <!-- ================================= 事务控制相关 ============================================= -->
    <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="delete*" propagation="REQUIRED" read-only="false" 
                       rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
            <tx:method name="insert*" propagation="REQUIRED" read-only="false" 
                       rollback-for="java.lang.RuntimeException" />
            <tx:method name="update*" propagation="REQUIRED" read-only="false" 
                       rollback-for="java.lang.Exception" />

            <tx:method name="find*" propagation="SUPPORTS"/>
            <tx:method name="get*" propagation="SUPPORTS"/>
            <tx:method name="select*" propagation="SUPPORTS"/>
        </tx:attributes>
    </tx:advice>    
    <aop:config>
        <aop:pointcut id="pc" expression="execution(* *.*.*(..))" />
        <aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
    </aop:config>
</beans>



请问是什么原因呢,帮忙看看,谢谢
  • 写回答

5条回答

  • jdk_ying 2016-08-25 04:44
    关注

    测试类:
    package com.nb.service;

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

    import com.nb.domain.ent.User;

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration({ "/spring/applicationContext.xml" })
    public class UserManagerServiceTest {
    @Autowired
    IUserManagerService userManagerService;

    @Test
    public void testAddUser() {
        User user = new User();
        user.setUserName("jack");
        user.setPassword("123456");
        userManagerService.addUser(user);
    }
    

    }

     实现类:
     package com.nb.service;
    
    import java.util.UUID;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    import com.nb.dao.UserMapper;
    import com.nb.domain.ent.User;
    
    @Service
    @Transactional
    public class UserManagerService implements IUserManagerService {
    
        @Autowired
        public UserMapper userMapper;
    
        @Override
        public int addUser(User user) {
            user.setId(UUID.randomUUID().toString());
            user.setUserName(user.getUserName());
            user.setPassword(user.getPassword());
            userMapper.insert(user);
            return userMapper.insert(user);
        }
    
    
    }
    
    

    dao:
    package com.nb.dao;

    import com.nb.domain.ent.User;

    public interface UserMapper {
    int insert(User record);

    int insertSelective(User record);
    

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。