天天看点

Spring+Mybatis整合

Spring+MyBatis 整合教程

一、添加需要的jar包

1 加入Spring jar包

(注意:在这里需要添加Spring3.X以上版本,Spring2.5不支持Mybatis整合)

我在这里添加添加Spring-3.1.0版本

2、添加mybatis jar和 mybatis-spring jar 包 mybatis-spring.jar 是mybatis提供的一个jar 包

3、在这里使用DBCP数据源

4、添加驱动包 这里使用mysql 数据库为例子

添加junit-4.11 测试包

添加完成之后 的jar截图如下:

二、添加配置文件

主要需要以下配置文件

Jdbc.properties :配置jdbc连接数据库的相关属性

Log4j.properties :log4j日志工具的配置文件

Spring-mybatis.xml :spring+mybatis 整合的主要配置文件

Mybatis.xml :mybatis主配置文件

Jdbc.properties 文件

配置了连接数据需要的一些选项

我们以映射一个User 对象为例子

1、建立实体 并且生成相应的getter setter 方法

 public class User {

private Integer id;

private String name;

}

2 创建映射接口

public interface UserMapper {

int findById(Integer id);

int deleteById(Integer id);

int update(User user);

int insert(User user);

}

3、在同一个包下面创建映射文件

UserMapper.xml(文件名字与映射接口名字保持一致)

这里需要注意的映射语句 id 属性时候 名称要与映射接口名称保持一致

<?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.oa.mapper.UserMapper" >

  <resultMap id="BaseResultMap" type="User" >

    <id column="id" property="id" jdbcType="BIGINT" />

    <result column="NAME" property="name" jdbcType="VARCHAR" />

  </resultMap>

  <sql id="Base_Column_List" >

    id, NAME

  </sql>

  <selectid="findById" resultMap="BaseResultMap"parameterType="java.lang.Integer" >

    select 

    <include refid="Base_Column_List" />

    from user

    where id = #{id,jdbcType=BIGINT}

  </select>

  <delete id="deleteById" parameterType="java.lang.Integer" >

    delete from user

    where id = #{id,jdbcType=BIGINT}

  </delete>

  <update id="update" parameterType="User" >

    update user

    set NAME = #{name,jdbcType=VARCHAR}

    where id = #{id,jdbcType=BIGINT}

  </update>

  <insert id="insert" parameterType="User" >

    insert into user (id, NAME)

    values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR})

  </insert>

</mapper>

4、好了 现在我们的映射文件写好了 下面开始配置 mybatis主配置 也就是

Mybatis.xml 这个配置文件主要是用来设置mybatis 运行时候一些全局的配置

Spring 和 Mybatis 整合时候 其实 这个主配置文件的作用也不太大,要不要都可以,

因为与Spring整合的话 就是要它的控制权交给spring容器来管理,在这里我们添上这个主配置,再后面Spring中引入就行了,一般就用两个配置:

<?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 >

<typeAlias type ="com.ib.domain.User" alias ="User" />

</typeAliases >

<!--这个配置是将映射文件加入进去-->

<mappers>

<mapper resource="com/ib/mapper/UserMapper.xml"/>

</mappers>

</configuration > 

5、下面我们要进行Spring 的核心配置

Spring-Mybatis.xml文件

<?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:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!-- 导入外部的properties文件 -->

<context:property-placeholder location="classpath:jdbc.properties"/>

这里我们的数据源使用的dbcp

<!-- 1、配置数据源 -->

<beanid="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

<propertyname="driverClassName" value="${jdbc_driverClassName}"/>

<property name="url" value="${jdbc_url}"/>

<property name="username" value="${jdbc_username}" />

<property name="password" value="${jdbc_password}" />

</bean>

<!-- 2、配置sqlSessionFactory -->

<beanid="sqlSessionFactory"

Class = "org.mybatis.spring.SqlSessionFactoryBean" >

<property name ="dataSource" ref ="dataSource" />

<!--注意这里:这里是将mybatis的主配置文件读取进来-->

<property name ="configLocation" value ="classpath:mybatis.xml" />

<!--这里如果我们不需要配置mybatis主配置文件的话 也可以通过以下配置将文件自动扫描-->

<!--<property name="mapperLocations" value ="classpath:com/ib/mapper

SqlSession session = sessionFactory.openSession(true);

UserMapper userMapper = session.getMapper(UserMapper.class); 

//测试添加一个对象

User user = new User();

user.setId(1);

user.setName("敏敏");

userMapper.insert(user);

}

}

我们可以看到对象已经保存到数据库,其它方法大家可以去测试

总结:

通过自己学习,与大家分享这种心情的喜悦的,小弟能力有限,有什么不足的,希望大家多多指出!谢谢!