1

Mybatis + Spring

When I Autowire a Dao into service and calling service in Junit Test the Service Object is Intailized but The Dao Object are null.

springTest/springcontext-datasource.xml
-----------------------------------------------------------------------

<?xml version="1.0" encoding="gb18030"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:jdbc="http://www.springframework.org/schema/jdbc"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc 
     http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:springTest/config.properties</value>
        </property>
    </bean>     

    <!-- define the mysql dataSource -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${driverClassName}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
    </bean> 

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

     <aop:config>  
         <aop:advisor pointcut="execution(* com.service..*.*(..))" advice-ref="txAdvice"/>  
     </aop:config>
     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
         <tx:attributes>  
             <tx:method name="get*" read-only="true"/>  
             <tx:method name="find*" read-only="true"/>  
             <tx:method name="*"  propagation="REQUIRED"/>  
        </tx:attributes>  
     </tx:advice>       

      <!-- enable component scanning and autowire (beware that this does not enable mapper scanning!) -->    
    <context:component-scan base-package="com.service" />

     <!-- define the SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="com.domain" />
        <property name="mapperLocations" value="classpath*:com/dao/*.xml" />
    </bean>

   <!-- scan for mappers and let them be autowired -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.dao" />
    </bean>

    <bean id="productService" class="com.service.ProductService" >
    </bean>

</beans>

DAO
-----------------------------------------------------------------------
package com.dao;

import com.domain.Author;

public interface AuthorMapper {

    int deleteByPrimaryKey(Integer authorId);
}

domain
-----------------------------------------------------------------------
package com.domain;

public class Author {
    private Integer authorId;

    private String sourceId;

    private String name;

    private String personalName;

    private Integer revision;

    private String lastModified;

    public Integer getAuthorId() {
        return authorId;
    }

    public void setAuthorId(Integer authorId) {
        this.authorId = authorId;
    }

    public String getSourceId() {
        return sourceId;
    }

    public void setSourceId(String sourceId) {
        this.sourceId = sourceId == null ? null : sourceId.trim();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public String getPersonalName() {
        return personalName;
    }

    public void setPersonalName(String personalName) {
        this.personalName = personalName == null ? null : personalName.trim();
    }

    public Integer getRevision() {
        return revision;
    }

    public void setRevision(Integer revision) {
        this.revision = revision;
    }

    public String getLastModified() {
        return lastModified;
    }

    public void setLastModified(String lastModified) {
        this.lastModified = lastModified == null ? null : lastModified.trim();
    }
}

package com.service;
ProductService
-----------------------------------------------------------------------

@Service
public class ProductService{
    @Autowired
    private AuthorMapper authorMapper;

    public Author selectAuthorByPrimaryKey(Integer authorId) {
        Author author = null;
        try{
            System.out.println("authorMapper:"+authorMapper);
            author = authorMapper.selectByPrimaryKey(authorId);
            System.out.println("Author Obj:"+author);
            return author;
        }catch(Exception e){
            log.info(e.getMessage()+" selectAuthorByPrimaryKey authorId:"+authorId);
        }
        return author;
    }
}

package com.test;
ProductServiceTest
-----------------------------------------------------------------------

@ContextConfiguration(locations = {"classpath:springTest/springcontext-*.xml"})
@Controller
public class ProductServiceTest extends AbstractTransactionalJUnit4SpringContextTests {

    @Autowired
    private ProductService productService;

    @Test
    public void test01_selectAuthor(){
        try{
        System.out.println("productService:"+productService);
            Author author = productService.selectAuthorByPrimaryKey(1);


        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

AuthorMapper.xml
-----------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.dao.AuthorMapper" >

  <resultMap id="BaseResultMap" type="com.domain.Author" >
    <id column="author_id" property="authorId" jdbcType="INTEGER" />
    <result column="source_id" property="sourceId" jdbcType="VARCHAR" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="personal_name" property="personalName" jdbcType="VARCHAR" />
    <result column="revision" property="revision" jdbcType="INTEGER" />
    <result column="last_modified" property="lastModified" jdbcType="VARCHAR" />
  </resultMap>

  <sql id="Base_Column_List" >
    author_id, source_id, name, personal_name, revision, last_modified
  </sql>

  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from author_t
    where author_id = #{authorId,jdbcType=INTEGER}
  </select>  
</mapper>

config.properties
-----------------------------------------------------------------------

driverClassName = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/test
username =root
password =satish



author_t Table
-----------------------------------------------------------------------

CREATE TABLE `author_t` (
  `author_id` int(15) NOT NULL AUTO_INCREMENT,
  `source_id` varchar(15) NOT NULL,
  `name` varchar(1000) NOT NULL,
  `personal_name` varchar(1000) DEFAULT NULL,
  `revision` int(3) DEFAULT NULL,
  `last_modified` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`author_id`),
  UNIQUE KEY `source_id` (`source_id`)
) ENGINE=InnoDB


OutPut Of Junit Test
----------------------------------------------------------------------
productService:com.service.ProductService@ee9867d
authorMapper:null
Author Obj:null


pom.xml
-----------------------------------------------------------------------
<dependencies>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.6.11</version>
    </dependency>           
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>   
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>   
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>   
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>   
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>   
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>3.0.5.RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>       

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.0.5</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.0.1</version>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.17</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
    </dependency>
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils-core</artifactId>
        <version>1.8.3</version>
    </dependency>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib-nodep</artifactId>
        <version>2.2.2</version>
    </dependency>
</dependencies>
4

1 回答 1

0

要使用带有注释的自动装配,应包含<context:annotation-config/>在 applicatonContext.xml 中

于 2013-07-08T22:24:48.750 回答