spring+mybatis框架搭建

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<Swjg> queryswjg(String swjg_dm);//映射SwjgMapper.xml中的查询
}


SwjgMapper.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.cn.mapper.SwjgMaper">
<resultMap type="Swjg" id="result"><!--定义返回类型-->

<result column="swjg_dm" property="swjg_dm"/>
<result column="sjswjg_dm" property="sjswjg_dm"/>
<result column="swjgmc" property="swjgmc"/>
</resultMap>
<select id="queryswjg" parameterType="String" resultMap="result"><!--查询语句-->
select t.swjg_dm,t.swjgmc,sjswjg_dm from dm_gy_swjg t where t.swjg_dm=#{swjg_dm}
</select>
</mapper>

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的配置文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases><!-- mybatis中加载bean,减少mapperxml中输入多余的完整类 -->
<typeAlias alias="Swjg" type="com.cn.bean.Swjg" />

</typeAliases>
<mappers >
<mapper resource="com/cn/mapper/SwjgMapper.xml"/>
</mappers>
</configuration>

(3)、applicationContext.xml为spring的配置文件,主要配置连接数据库、mybatis的工厂及加载bean实例(我的项目中将数据库信息放在properties中),内容如下:

<?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:aop="http://www.springframework.org/schema/aop"
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">
<!-- 加载数据库 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="config/database.properties"></property>
</bean>
<bean id="dateSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- mybatis加载数据库并创建数据工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--dataSource属性指定要用到的连接池-->
<property name="dataSource" ref="dateSource"></property>
<!--configLocation属性指定mybatis的核心配置文件-->
<property name="configLocation" value="config/Configuration.xml"></property>
</bean>
<bean id="swjgMaper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
<!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例-->
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象-->
<property name="mapperInterface" value="com.cn.mapper.SwjgMaper" />
</bean>
</beans>

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<Swjg> swjg=swjgmaper.queryswjg("24100000000");
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.<init>(MapperMethod.java:178)
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:38)
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.<init>(MapperMethod.java:176)
... 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所在目录
















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值