目前Spring官方还没有出整合Mybatis的特性,但是mybitis比较给力,开发了一个mybatis-spring插件,达到与Spring的完美整合目的。

在与Spring集成前,一方面我们需要下载Spring的所需的jar文件,我是刚才Spring官方网站上下载的最新的spring-framework-3.2.2.RELEASE-dist.zip压缩文件,里面包含了Spring所有jar文件以及文档;另一方面我们需要从mybitis的官方网站上下载mybatis-spring插件,我是下载最新的mybatis-spring-1.2.0-bundle.zip压缩文件,里面包含了需要的jar文件。由于本文使用的dbcp作为数据库连接池,所以还要准备dbcp jar文件。

关于Spring的事务配置,本博文不会赘述。下面就详细介绍下集成

以下代码按照下图结构组织:

一、准备t_mobile表

        

二、编写Mobile实体

  1. package com.jefry;
  2. public class Mobile {
  3. private int id;
  4. private String telnumber;
  5. public int getId() {
  6. return id;
  7. }
  8. public void setId(int id) {
  9. this.id = id;
  10. }
  11. public String getTelnumber() {
  12. return telnumber;
  13. }
  14. public void setTelnumber(String telnumber) {
  15. this.telnumber = telnumber;
  16. }
  17. }

三、编写Mobile数据库接口

  1. package com.jefry;
  2. public interface MobileMapper {
  3. public Mobile getMoble(int id);
  4. }

四、编写Mobile业务Bean,里面注入Mobile数据库接口对象

  1. package com.jefry;
  2. public class MobileService {
  3. private MobileMapper mobileMapper;
  4. public void setMobileMapper(MobileMapper mobileMapper) {
  5. this.mobileMapper = mobileMapper;
  6. }
  7. public Mobile getMoble(int id){
  8. return mobileMapper.getMoble(id);
  9. }
  10. }

五、准备ibitis配置文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <typeAliases>
  7. <typeAlias alias="Mobile" type="com.jefry.Mobile"/>
  8. </typeAliases>
  9. <mappers>
  10. <mapper resource="com/jefry/MobileMapper.xml"/>
  11. </mappers>
  12. </configuration>

六、编写Mobile数据库映射文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.jefry.MobileMapper">
  6. <resultMap id="mobileResultMap" type="Mobile">
  7. <id property="id" column="id" javaType="int" jdbcType="INTEGER"/>
  8. <result property="telnumber" column="telnumber" javaType="string" jdbcType="VARCHAR"/>
  9. </resultMap>
  10. <select id="getMoble" parameterType="int"  resultMap="mobileResultMap" >
  11. select * from t_mobile where id = #{id}
  12. </select>
  13. </mapper>

七、由Spring管理业务bean对象(bean.xml)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <!-- more bean definitions go here -->
  7. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  8. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  9. <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
  10. <property name="username" value="root"/>
  11. <property name="password" value="root"/>
  12. </bean>
  13. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  14. <!--dataSource属性指定要用到的连接池-->
  15. <property name="dataSource" ref="dataSource"/>
  16. <!--configLocation属性指定mybatis的核心配置文件-->
  17. <property name="configLocation" value="mybatis-config.xml"/>
  18. </bean>
  19. <bean id="mobileMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
  20. <!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例-->
  21. <property name="sqlSessionFactory" ref="sqlSessionFactory" />
  22. <!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象-->
  23. <property name="mapperInterface" value="com.jefry.MobileMapper" />
  24. </bean>
  25. <bean id="mobileService" class="com.jefry.MobileService">
  26. <property name="mobileMapper" ref="mobileMapper"/>
  27. </bean>
  28. </beans>

八、编写测试代码

  1. package com.jefry;
  2. import java.io.IOException;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class Test {
  6. static String resource = "mybatis-config.xml";
  7. public static void main(String[] args) throws IOException {
  8. ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"bean.xml"});
  9. MobileService mobileService = context.getBean("mobileService", MobileService.class);
  10. System.out.println("编号1的电话号码:" + mobileService.getMoble(1).getTelnumber());
  11. }
  12. }

MyBitis(iBitis)系列随笔之六:mybitis与spring集成的更多相关文章

  1. MyBitis(iBitis)系列随笔之五:多表(一对多关联查询)

    MyBitis(iBitis)系列随笔之一:MyBitis入门实例 MyBitis(iBitis)系列随笔之二:类型别名(typeAliases)与表-对象映射(ORM) MyBitis(iBitis ...

  2. MyBitis(iBitis)系列随笔之三:简单实现CRUD

    Mybitis(iBitis)实现对对象增删改查操作要借助<select/>查询,<insert/>增加,<update/>更新,<delete/>删除 ...

  3. MyBitis(iBitis)系列随笔之一:MyBitis入门实例

        MyBits前身是iBitis,相对于Hibernate而言,它是半自动化ORM框架.本着分享和学习的目的,笔者将陆续把学习笔记与代码贴出,希望对想学习mybitis的同窗们有所帮助.     ...

  4. MyBitis(iBitis)系列随笔之四:多表(多对一查询操作)

      前面几篇博客介绍的都是单表映射的一些操作,然而在我们的实际项目中往往是用到多表映射.至于多表映射的关键要用到mybitis的association来加以实现.          这篇介绍的是多表中 ...

  5. MyBitis(iBitis)系列随笔之二:类型别名(typeAliases)与表-对象映射(ORM)

    类型别名(typeAliases):     作用:通过一个简单的别名来表示一个冗长的类型,这样可以降低复杂度.    类型别名标签typeAliases中可以包含多个typeAlias,如下 < ...

  6. Spring系列之新注解配置+Spring集成junit+注解注入

    Spring系列之注解配置 Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率 你本来要写一段很长的代码来构造一个 ...

  7. 消息中间件系列四:RabbitMQ与Spring集成

    一.RabbitMQ与Spring集成  准备工作: 分别新建名为RabbitMQSpringProducer和RabbitMQSpringConsumer的maven web工程 在pom.xml文 ...

  8. AI人工智能系列随笔

    初探 AI人工智能系列随笔:syntaxnet 初探(1)

  9. ASP.NET MVC 系列随笔汇总[未完待续……]

    ASP.NET MVC 系列随笔汇总[未完待续……] 为了方便大家浏览所以整理一下,有的系列篇幅中不是很全面以后会慢慢的补全的. 学前篇之: ASP.NET MVC学前篇之扩展方法.链式编程 ASP. ...

随机推荐

  1. alitomcat maven以及Autoconfig

    maven概述 Maven的核心是POM(Project Object Model),即项目对象模型.最直观的,maven对项目依赖进行统一的管理,让开发者从纷杂错乱的jar包世界摆脱出来,更加专注于 ...

  2. linux shell 总结

    .#!指定执行脚本的shell 如果不写的话,用系统默认的shell s shell是所有linux ,unix都支持的 .#开始的行表示注释(不限于行首) 命令建议写绝对路径 执行: ./examp ...

  3. Hadoop + HBase (自带zookeeper 也可单独加) 集群部署

    Hadoop+HBase搭建云存储总结 PDF http://www.linuxidc.com/Linux/2013-05/83844.htm HBase 结点之间时间不一致造成regionserve ...

  4. 公司交换机arp 绑定操作

    1.首先登入:192.168.1.1 2.sys 3.dis arp | inc 192.168.1.49(查看该ip绑定情况) 4.undo arp 192.168.1.49(不绑定命令)

  5. WebService注解

    package cn.itcast.service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.W ...

  6. Linux-软件包管理-rpm命令管理-安装-卸载

    mount 确认光盘是否挂载 mount /dev/cdrom /mnt/cdrom 将设备名称/dev/cdrom安装到/mnt/cdrom挂载点下面 mount 查看光盘是否已经挂载 (ro表示只 ...

  7. 网页中PNG透明背景图片的完美应用

    PNG 图片在网站设计中是不可或缺的部分,最大的特点应该在于 PNG 可以无损压缩,而且还可以设置透明,对于增强网站的图片色彩效果有重要的作用. 但为什么 PNG 图片却没有 GIF 和 JPG 图片 ...

  8. PHP 5.3以上版本推荐使用mysqlnd驱动

    什么是mysqlnd?mysqldnd(MySQL native driver)是由PHP源码提供的mysql驱动连接代码.它的目的是代替旧的libmysql驱动. 传统的安装php的方式中,我们在编 ...

  9. 11个常用的Linux命令

    Linux命令行吸引了大多数Linux爱好者.一个正常的Linux用户一般掌握大约50-60个命令来处理每日的任务.今天为你解释下面几个命令:sudo.python.mtr.Ctrl+x+e.nl.s ...

  10. 查询ip地址归属地

    查询ip地址归属地方法: curl ip.cn/$ip 如果没有返回,试试地址写全: curl https://www.ip.cn/$ip 如: