MyBitis(iBitis)系列随笔之六:mybitis与spring集成
目前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实体
- package com.jefry;
- public class Mobile {
- private int id;
- private String telnumber;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getTelnumber() {
- return telnumber;
- }
- public void setTelnumber(String telnumber) {
- this.telnumber = telnumber;
- }
- }
三、编写Mobile数据库接口
- package com.jefry;
- public interface MobileMapper {
- public Mobile getMoble(int id);
- }
四、编写Mobile业务Bean,里面注入Mobile数据库接口对象
- package com.jefry;
- public class MobileService {
- private MobileMapper mobileMapper;
- public void setMobileMapper(MobileMapper mobileMapper) {
- this.mobileMapper = mobileMapper;
- }
- public Mobile getMoble(int id){
- return mobileMapper.getMoble(id);
- }
- }
五、准备ibitis配置文件
- <?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 alias="Mobile" type="com.jefry.Mobile"/>
- </typeAliases>
- <mappers>
- <mapper resource="com/jefry/MobileMapper.xml"/>
- </mappers>
- </configuration>
六、编写Mobile数据库映射文件
- <?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.jefry.MobileMapper">
- <resultMap id="mobileResultMap" type="Mobile">
- <id property="id" column="id" javaType="int" jdbcType="INTEGER"/>
- <result property="telnumber" column="telnumber" javaType="string" jdbcType="VARCHAR"/>
- </resultMap>
- <select id="getMoble" parameterType="int" resultMap="mobileResultMap" >
- select * from t_mobile where id = #{id}
- </select>
- </mapper>
七、由Spring管理业务bean对象(bean.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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd">
- <!-- more bean definitions go here -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
- <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
- <property name="username" value="root"/>
- <property name="password" value="root"/>
- </bean>
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <!--dataSource属性指定要用到的连接池-->
- <property name="dataSource" ref="dataSource"/>
- <!--configLocation属性指定mybatis的核心配置文件-->
- <property name="configLocation" value="mybatis-config.xml"/>
- </bean>
- <bean id="mobileMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
- <!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例-->
- <property name="sqlSessionFactory" ref="sqlSessionFactory" />
- <!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象-->
- <property name="mapperInterface" value="com.jefry.MobileMapper" />
- </bean>
- <bean id="mobileService" class="com.jefry.MobileService">
- <property name="mobileMapper" ref="mobileMapper"/>
- </bean>
- </beans>
八、编写测试代码
- package com.jefry;
- import java.io.IOException;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Test {
- static String resource = "mybatis-config.xml";
- public static void main(String[] args) throws IOException {
- ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"bean.xml"});
- MobileService mobileService = context.getBean("mobileService", MobileService.class);
- System.out.println("编号1的电话号码:" + mobileService.getMoble(1).getTelnumber());
- }
- }
MyBitis(iBitis)系列随笔之六:mybitis与spring集成的更多相关文章
- MyBitis(iBitis)系列随笔之五:多表(一对多关联查询)
MyBitis(iBitis)系列随笔之一:MyBitis入门实例 MyBitis(iBitis)系列随笔之二:类型别名(typeAliases)与表-对象映射(ORM) MyBitis(iBitis ...
- MyBitis(iBitis)系列随笔之三:简单实现CRUD
Mybitis(iBitis)实现对对象增删改查操作要借助<select/>查询,<insert/>增加,<update/>更新,<delete/>删除 ...
- MyBitis(iBitis)系列随笔之一:MyBitis入门实例
MyBits前身是iBitis,相对于Hibernate而言,它是半自动化ORM框架.本着分享和学习的目的,笔者将陆续把学习笔记与代码贴出,希望对想学习mybitis的同窗们有所帮助. ...
- MyBitis(iBitis)系列随笔之四:多表(多对一查询操作)
前面几篇博客介绍的都是单表映射的一些操作,然而在我们的实际项目中往往是用到多表映射.至于多表映射的关键要用到mybitis的association来加以实现. 这篇介绍的是多表中 ...
- MyBitis(iBitis)系列随笔之二:类型别名(typeAliases)与表-对象映射(ORM)
类型别名(typeAliases): 作用:通过一个简单的别名来表示一个冗长的类型,这样可以降低复杂度. 类型别名标签typeAliases中可以包含多个typeAlias,如下 < ...
- Spring系列之新注解配置+Spring集成junit+注解注入
Spring系列之注解配置 Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率 你本来要写一段很长的代码来构造一个 ...
- 消息中间件系列四:RabbitMQ与Spring集成
一.RabbitMQ与Spring集成 准备工作: 分别新建名为RabbitMQSpringProducer和RabbitMQSpringConsumer的maven web工程 在pom.xml文 ...
- AI人工智能系列随笔
初探 AI人工智能系列随笔:syntaxnet 初探(1)
- ASP.NET MVC 系列随笔汇总[未完待续……]
ASP.NET MVC 系列随笔汇总[未完待续……] 为了方便大家浏览所以整理一下,有的系列篇幅中不是很全面以后会慢慢的补全的. 学前篇之: ASP.NET MVC学前篇之扩展方法.链式编程 ASP. ...
随机推荐
- Execute failed: java.io.IOException: Cannot run program "sdk-linux/build-tools/22.0.0/aapt": error=2
在Linux上使用ant编译打包apk的时候,出现以下的错误及解决方法: 1./usr/local/android-sdk-linux/tools/ant/build.xml:698: Execute ...
- 03-maven学习-eclipse中创建maven项目
一,更改eclipse默认的maven配置 window->preference 选择本地maven目录 一直确定后 二,更改maven默认settings window->prefere ...
- 公用的css
/*公共开始*/ body, ol, ul, h1, h2, h3, h4, h5, h6, p, th, td, dl, dd, form, fieldset, legend, input, tex ...
- c#(winform)中自定义ListItem类方便ComboBox添加Item项
1.定义ListItem类 public class ListItem { private string _key = string.Empty; private string _value = st ...
- Django模版中的过滤器详细解析 Django filter大全
就象本章前面提到的一样,模板过滤器是在变量被显示前修改它的值的一个简单方法. 过滤器看起来是这样的: {{ name|lower }} 显示的内容是变量 {{ name }} 被过滤器 lower 处 ...
- MySQL中多表删除方法
如果您是才接触MySQL数据库的新人,那么MySQL中多表删除是您一定需要掌握的,下面就将为详细介绍MySQL中多表删除的方法,供您参考,希望对你学习掌握MySQL中多表删除能有所帮助. 1.从MyS ...
- Atitit.故障排除系列---NoClassDefFoundError NoClassDefFoundError
Atitit.故障排除系列---NoClassDefFoundError NoClassDefFoundError java.lang.ClassNotFoundException找不到类异常.当应 ...
- atitit.软件开发--socket框架选型--netty vs mina j
atitit.软件开发--socket框架选型--netty vs mina j . Netty是由JBOSS提供的一个java开源框架 Apache mina 三.文档比较 mina文档多,,, 好 ...
- 1. A + B Problem【easy】
Write a function that add two numbers A and B. You should not use + or any arithmetic operators. Not ...
- ssm开发系的统架构图