• ibatis和mybatis


    ibatis和mybatis

    Posted on 2011-09-29 12:56 技术总监 阅读(225) 评论(0) 编辑 收藏 


    研究ibatis和mybatis有一段时间,在网上找了很多资料,很多人都说二者没有区别,确实如些,他们的原理是一样的,但在映射文件的写法了,有了区别,不知

    被谷歌收购了就命名为mybatis,写法上,有了改变,要是再被其他公司收购,难道不会再发生改变吗?这害惨的我们这些写程序的人,在这里我写了一些例子供大家参考:

    本人写了个简单图书管理的程序了,就做了一下简单的CRUD操作

    一、ibatis

    1.配制数源源的xml文件(SqlMapConfig.xml)

    代码如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
    2 <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd" >
    3 <sqlMapConfig>
    4 <transactionManager type="JDBC">
    5 <dataSource type="SIMPLE">
    6 <property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
    7 <property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/test" />
    8 <property name="JDBC.Username" value="root" />
    9 <property name="JDBC.Password" value="123456" />
    10 </dataSource>
    11 </transactionManager>
    12
    13 <sqlMap resource="com/softeam/beans/Myemp.xml" />
    14 <sqlMap resource="com/softeam/beans/Category.xml" />
    15 <sqlMap resource="com/softeam/beans/Book.xml" />
    16 </sqlMapConfig>

    sqlManConfig中有两个子节点 transactionManager(事物管理)和sqlMap(类xml文件地址)

    transactionManager中datasource节点就是我们配制的sql数据源(Driver,ConnectionURL,Username,Password),我采用的数据是mysql

    2.我们再来看一下类相关的配制文件,这可以本节中的重点哦!!!

    就将book.xml中的源码贴出来吧:

     1 <?xml version="1.0" encoding="UTF-8"?>
    2 <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
    3 <sqlMap namespace="Book">
    4 <typeAlias alias="Book" type="com.softeam.beans.Book" />
    5 <resultMap class="com.softeam.beans.Category" id="categoryMapper">
    6 <result property="c_id" column="c_id" javaType="Integer"
    7 jdbcType="INTEGER" />
    8 <result property="c_name" column="c_name" javaType="java.lang.String"
    9 jdbcType="VARCHAR" />
    10 </resultMap>
    11 <resultMap class="com.softeam.beans.Book" id="bookMapper">
    12 <result property="id" column="id" javaType="Integer" jdbcType="INTEGER" />
    13 <result property="name" column="name" javaType="java.lang.String"
    14 jdbcType="VARCHAR" />
    15 <result property="author" column="author" javaType="java.lang.String"
    16 jdbcType="VARCHAR" />
    17 <result property="categoryid" column="categoryid" javaType="INTEGER"
    18 jdbcType="INTEGER" />
    19 <result property="category" column="categoryid" select="SelBookById" />
    20 </resultMap>
    21 <insert id="InsBook" parameterClass="com.softeam.beans.Book">
    22 insert into
    23 book(name,author,categoryid)
    24 values(#name#,#author#,#categoryid#)
    25 </insert>
    26 <select id="SelBookByAll" resultMap="bookMapper">
    27 select b.*,c.id as
    28 c_id,c.name as c_name from book
    29 b,category c where
    30 b.categoryid=c.id
    31 limit #begin#,5
    32 </select>
    33 <select id="SelBookById" resultClass="com.softeam.beans.Category">
    34 select * from category where id=#id#
    35 </select>
    36 <delete id="DelBookById" parameterClass="INTEGER">
    37 delete from book where
    38 id=#id#
    39 </delete>
    40 <update id="UpdateBook" parameterClass="com.softeam.beans.Book">
    41 update book set
    42 name=#name#,author=#author#,categoryid=#categoryid# where
    43 id=#id#
    44 </update>
    45 </sqlMap>

          这就是与hibernate的区别,hibernate不需要写大量的sql语句,这样灵活性低,而我的ibatis和mybaits,就可以写相应的sql语句,灵活性比较大。我们在这里就重点说一下联合查询。

          我们来联合一下book(图书),Category(图书种类),如下两个resultmap,第二个book列表是我们的重点。这就与我们的类book.java有关了,现贴出代码,我想众位一看便知:

     

     1 package com.softeam.beans;
    2 import java.io.Serializable;
    3 public class Book implements Serializable {
    4 private Integer id;
    5 private String name;
    6 private String author;
    7 private Integer categoryid;
    8 private Category category;
    9 public Book(Integer id, String name, String author, Integer categoryid) {
    10 this.id = id;
    11 this.name = name;
    12 this.author = author;
    13 this.categoryid = categoryid;
    14 }
    15 public Book() {
    16 }
    17 public Book(Integer id) {
    18 this.id = id;
    19 }
    20 public Integer getId() {
    21 return this.id;
    22 }
    23 public void setId(Integer id) {
    24 this.id = id;
    25 }
    26 public Category getCategory() {
    27 return category;
    28 }
    29 public void setCategory(Category category) {
    30 this.category = category;
    31 }
    32 public String getName() {
    33 return this.name;
    34 }
    35
    36 public void setName(String name) {
    37 this.name = name;
    38 }
    39
    40 public String getAuthor() {
    41 return this.author;
    42 }
    43
    44 public void setAuthor(String author) {
    45 this.author = author;
    46 }
    47
    48 public Integer getCategoryid() {
    49 return this.categoryid;
    50 }
    51
    52 public void setCategoryid(Integer categoryid) {
    53 this.categoryid = categoryid;
    54 }
    55 }

          我们是定义了Category的类字段名的,所以我们的配制文件,有该属性,这也是方便我们联合查询,也不再此说太多了,说太多可能把大家搞混了。我们就再来看一下mybatis吧,到底和ibatis是不是一样呢

          一样贴出数据源配制文件(configuration.xml)和一个xml映射文件(book.xml)

     

     1 <?xml version="1.0" encoding="UTF-8"?>
    2 <!DOCTYPE configuration PUBLIC
    3 "-//mybatis.org//DTD Config 3.0//EN"
    4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
    5 <configuration>
    6 <environments default="development">
    7 <environment id="development">
    8 <transactionManager type="JDBC" />
    9 <dataSource type="POOLED">
    10 <property name="driver" value="com.mysql.jdbc.Driver" />
    11 <property name="url" value="jdbc:mysql://localhost:3306/test" />
    12 <property name="username" value="root" />
    13 <property name="password" value="123456" />
    14 </dataSource>
    15 </environment>
    16 </environments>
    17 <mappers>
    18 <mapper resource="com/softeam/beans/User.xml" />
    19 <mapper resource="com/softeam/beans/Book.xml" />
    20 <mapper resource="com/softeam/beans/Myemp.xml" />
    21 <mapper resource="com/softeam/beans/Category.xml" />
    22 </mappers>
    23 </configuration>

     

    book.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
    2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4 <mapper namespace="Book">
    5 <resultMap type="com.softeam.beans.Category" id="categoryMapper">
    6 <id property="c_id" column="c_id" javaType="java.lang.Integer" jdbcType="INTEGER"/>
    7 <result property="c_name" column="c_name" javaType="java.lang.String" jdbcType="VARCHAR"/>
    8 </resultMap>
    9 <resultMap type="com.softeam.beans.Book" id="bookMapper">
    10 <id property="id" column="id" javaType="java.lang.Integer" jdbcType="INTEGER"/>
    11 <result property="name" column="name" javaType="java.lang.String" jdbcType="VARCHAR"/>
    12 <result property="author" column="author" javaType="java.lang.String" jdbcType="VARCHAR"/>
    13 <association property="category" column="categoryid" javaType="com.softeam.beans.Category" resultMap="categoryMapper" />
    14 </resultMap>
    15
    16 <insert id="InsBook" parameterType="com.softeam.beans.Book">
    17 insert into book(name,author,categoryid) values(#{name},#{author},#{categoryid})
    18 </insert>
    19 <select id="SelBookByAll" parameterType="INTEGER" resultMap="bookMapper">
    20 select b.*,c.id as c_id,c.name as c_name from book b,category c where b.categoryid=c.id limit #{begin},5
    21 </select>
    22 <delete id="DelBookById" parameterType="INTEGER">
    23 delete from book where id=#{id}
    24 </delete>
    25 <update id="UpdateBook" parameterType="com.softeam.beans.Book">
    26 update book set name=#{name},author=#{author},categoryid=#{categoryid} where id=#{id}
    27 </update>
    28 </mapper>

    数据操作session的创建也跟大家贴一下:

    ibatis:

     1 package com.softeam.dao;
    2
    3 import java.io.IOException;
    4 import java.io.Reader;
    5
    6 import com.ibatis.common.resources.Resources;
    7 import com.ibatis.sqlmap.client.SqlMapClient;
    8 import com.ibatis.sqlmap.client.SqlMapClientBuilder;
    9
    10 public class MyConn {
    11 private static SqlMapClient sqlMapper;
    12
    13 static{
    14 try {
    15 Reader reader=Resources.getResourceAsReader("SqlMapConfig.xml");
    16 sqlMapper=SqlMapClientBuilder.buildSqlMapClient(reader);
    17 reader.close();
    18 } catch (IOException e) {
    19 e.printStackTrace();
    20 }
    21 }
    22 public static SqlMapClient getSqlMapper() {
    23 return sqlMapper;
    24 }
    25 public static void main(String[] args) {
    26 MyConn conn=new MyConn();
    27 }
    28 }

    mybatis:

     1 package com.softeam.dao;
    2
    3 import java.io.IOException;
    4 import java.io.Reader;
    5
    6 import org.apache.ibatis.io.Resources;
    7 import org.apache.ibatis.session.SqlSession;
    8 import org.apache.ibatis.session.SqlSessionFactory;
    9 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    10
    11 public class DBSession {
    12 public static SqlSession GetSession() {
    13 String resource = "configuration.xml";
    14 Reader reader;
    15 try {
    16 reader = Resources.getResourceAsReader(resource);
    17 SqlSessionFactory sqlmapper = new SqlSessionFactoryBuilder()
    18 .build(reader);
    19 SqlSession session = sqlmapper.openSession();
    20 return session;
    21 } catch (IOException e) {
    22 // TODO Auto-generated catch block
    23 System.out.println("创建session出错");
    24 return null;
    25 }
    26
    27
    28 }
    29 }

     

    有什么区别大家不访自己比较一下,结果请留言!!!谢谢!

    源码下载地址:http://download.csdn.net/detail/chenjie200280/3646602

    相关参考资料下载地址:http://download.csdn.net/detail/chenjie200280/3646609




     

  • 相关阅读:
    EOS 数据库RAM使用量的计算
    EOS require_auth函数
    EOS测试链智能合约部署调用
    安装EOS合约工具eosio.cdt
    Linux Ubuntu下搭建Flutter桌面环境
    VMware虚拟机与物理机共享文件的方法总结
    虚拟机macOS Mojave10.14安装XCode10.1
    VMware14 安装 macOS Mojave教程
    VMware 14安装OS X10.11
    Mac OS虚拟机安装VMware Tools的方法
  • 原文地址:https://www.cnblogs.com/lexus/p/2340786.html
Copyright © 2020-2023  润新知