使用maven来Spring与Mybatis的整合及其问题的解决

所需要的jar包:

    
        
        
            org.springframework
            spring-core
            4.3.2.RELEASE
        
        
            org.springframework
            spring-context
            4.3.2.RELEASE
        
        
            org.springframework
            spring-test
            4.3.2.RELEASE
        
        
            org.springframework
            spring-tx
            4.3.2.RELEASE
        
        
            org.springframework
            spring-beans
            4.3.2.RELEASE
        
        
            org.springframework
            spring-jdbc
            4.3.2.RELEASE
        
        
        
            org.mybatis
            mybatis
            3.2.8
        
        
        
            org.mybatis
            mybatis-spring
            1.3.0
        
        
            mysql
            mysql-connector-java
            5.1.38
        
        
        
            com.alibaba
            druid
            1.0.18
        
        
        
            junit
            junit
            4.12
            test
        
    

jdbc配置文件:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
jdbc.username=root
jdbc.password=root
validationQuery=SELECT 1

applicationContext配置文件:

 
    
    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    
    
    
        
        
        
        
        
        
        
    

    
    
        
        
    

    
        
        
    

 

表结构:

domain:

public class User {
    private Integer id;
    private String userName;
    private Integer age;
    
    // getter,setter,toString
}

Mapper配置文件:




    

Dao:

public interface IUserDao {
    public List selectAllUsers();
}

Service:

public interface IUserService {
    public List selectAllUsers();
}

// 接口的实现类
public class UserServiceImpl implements IUserService {
    private IUserDao userDao;

    public void setUserDao(IUserDao userDao) {
        this.userDao = userDao;
    }

    public List selectAllUsers() {
        return userDao.selectAllUsers();
    }
}

Test程序:

public class Test01 {
    private IUserService userService;

    @Before
    public void setUp(){
        String resource = "spring-mybatis.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
        userService = (IUserService) ac.getBean("userService");
    }

    @Test
    public void fun1(){
        List users = userService.selectAllUsers();
        Iterator iter = users.iterator();
        while (iter.hasNext()){
            User next = iter.next();
            System.out.println(next);
        }
    }
}

通过上述整合操作,我们就能够完成了Spring和Mybatis的整合操作了,以下是查询结果:

User{id=1, userName='大一', age=21}
User{id=2, userName='李四', age=24}
User{id=3, userName='王五', age=25}
User{id=5, userName='马六', age=26}
User{id=6, userName='网二', age=24}

当然,整合过程并不是一帆风顺的,也出现了几个问题。

 

使用maven来Spring与Mybatis的整合及其问题的解决_第1张图片

看到这个错误,我们能够大体的猜到,在Mapper的配置文件中,并没有出现于dao层接口方法名一致