1、在搭建之前,首先我们需要了解下spring与mybatis的jar包
我在搭建的时候使用了以下jar包,如图所示:
具体jar包的作用可百度
2、新建一个javaweb项目,使用的工具为:
jdk 1.6
myeclipse 8.6
项目目录如下:
3、bean下面的Swjg是我需要用到的,实例对象Swjg.java代码如下
package com.cn.bean;
public class Swry {
String swry_dm;
String swryxm;
public String getSwry_dm() {
return swry_dm;
}
public void setSwry_dm(String swryDm) {
swry_dm = swryDm;
}
public String getSwryxm() {
return swryxm;
}
public void setSwryxm(String swryxm) {
this.swryxm = swryxm;
}
@Override
public String toString() {
return "Swry [swry_dm=" + swry_dm + ", swryxm=" + swryxm + "]";
}
}
SwjgMaper.java定义sql映射的接口,内容如下:
package com.cn.mapper;
import java.util.List;
import com.cn.bean.Swjg;
public interface SwjgMaper {
public List
}
SwjgMapper.xml用于定义查询数据对象映射,如下:
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
4、配置文件如下:
(1)、database.properties为连接数据库的数据,内容如下:
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:oracle
jdbc.username=test
jdbc.password=test
(2)、Configuration.xml为mybatis的配置文件,内容如下:
"http://mybatis.org/dtd/mybatis-3-config.dtd">
(3)、applicationContext.xml为spring的配置文件,主要配置连接数据库、mybatis的工厂及加载bean实例(我的项目中将数据库信息放在properties中),内容如下:
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
default-autowire="byName" default-lazy-init="false">
5、编写测试方法:
package com.cn.main;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cn.bean.Swjg;
import com.cn.mapper.SwjgMaper;
public class SwjgMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx= new ClassPathXmlApplicationContext("config/applicationContext.xml");
//
SwjgMaper swjgmaper=(SwjgMaper) ctx.getBean("swjgMaper");
List
System.out.println(swjg);
}
}
输出结果为:
6、在项目编写过程中出现的问题总结:
1)、在项目运行时提示“Exception in thread "main" org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.cn.mapper.SwjgMaper.queryswjg
at org.apache.ibatis.binding.MapperMethod$SqlCommand.
at org.apache.ibatis.binding.MapperMethod.
at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:49)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:42)
at $Proxy3.queryswjg(Unknown Source)
at com.cn.main.SwjgMain.main(SwjgMain.java:26)
Caused by: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.cn.mapper.SwjgMaper.queryswjg
at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:768)
at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:603)
at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:596)
at org.apache.ibatis.binding.MapperMethod$SqlCommand.
... 5 more”
解决方法:检查SwjgMapper.xml中的namespace与项目的路径文件是否完全一致
2)、项目运行时提示“java.lang.ExceptionInInitializerError
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [config/applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse config resource: class path resource [config/Configuration.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.RuntimeException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Swjg'. Cause: java.lang.ClassNotFoundException: Cannot find class: Swjg”
解决方法:经检查发现mybatis中的配置文件未加载bean,SwjgMapper.xml中也未写全Swjg.java所在目录