Spring+Mybatis操作数据库(使用Mapper映射器)

Spring+Mybatis操作数据库(使用Mapper映射器)_第1张图片

jsr250-api:

 
javax.annotation  
jsr250-api  
1.0  

注意事项:mybatis-spring使用的是1.3.2的,此处mybatis版本使用的是3.4.1的,

如果用mybatis3.2及以下版本的会报:

java.lang.IllegalAccessError: org.apache.commons.dbcp.DelegatingPreparedStatement.isClosed()Z

如果使用mybatis3.4.6版本会报:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionTemplate': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.mybatis.spring.SqlSessionTemplate] from ClassLoader [sun.misc.Launcher$AppClassLoader@66d3c617]

一、spring的配置文件


       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:cache="http://www.springframework.org/schema/cache"
       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/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
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
    
    
   
   
   
   
   
   

   
   
   
   
   
   
   
   
   
   

    
   
   
   
   
   

    
   
       
           
   

     
   
     
 

二、Mapper映射文件


PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">



   

 

三、映射器接口

package com.dao;


import org.springframework.stereotype.Repository;

import com.pojo.TmCustomerPO;
@Repository
public interface TmCustomerDao {
	public TmCustomerPO findByPK(String PK_CUSTOMER);
}

四、实体类

/*
 Copyright (c) 2005 KWT, Inc. All  Rights Reserved.
 */

package com.pojo;


public class TmCustomerPO {

	private String vname;
	private String pkCustomer;
	private String creationtime;
	private String modifier;
	private String vmobile;
	private String pkOrg;
	private String pkGroup;
	private String pkFinanceOrg;

	public void setVname(String vname){
		this.vname=vname;
	}

	public String getVname(){
		return this.vname;
	}

	public void setPkCustomer(String pkCustomer){
		this.pkCustomer=pkCustomer;
	}

	public String getPkCustomer(){
		return this.pkCustomer;
	}


	public void setCreationtime(String creationtime){
		this.creationtime=creationtime;
	}

	public String getCreationtime(){
		return this.creationtime;
	}


	public void setModifier(String modifier){
		this.modifier=modifier;
	}

	public String getModifier(){
		return this.modifier;
	}


	public void setVmobile(String vmobile){
		this.vmobile=vmobile;
	}

	public String getVmobile(){
		return this.vmobile;
	}


	public void setPkOrg(String pkOrg){
		this.pkOrg=pkOrg;
	}

	public String getPkOrg(){
		return this.pkOrg;
	}


	public void setPkGroup(String pkGroup){
		this.pkGroup=pkGroup;
	}

	public String getPkGroup(){
		return this.pkGroup;
	}

	public void setPkFinanceOrg(String pkFinanceOrg){
		this.pkFinanceOrg=pkFinanceOrg;
	}

	public String getPkFinanceOrg(){
		return this.pkFinanceOrg;
	}

}

五、测试代码

static TmCustomerDao tmCustomer;
    
    @BeforeClass
    public static void before(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContextByMapper.xml");
        tmCustomer=ctx.getBean(TmCustomerDao.class);
    }
    
    @Test
    public void testSpringMyBatisByMapper() {
        TmCustomerPO tmcustomer=tmCustomer.findByPK("CO201511010000010090");
        System.out.println(tmcustomer.getVname());
    }

引入外部数据源注意事项: uername会报错,所以加上system-properties-mode="FALLBACK"


    
   
   
   
   
   
   

 

 

你可能感兴趣的:(框架)