<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://m.tkk7.com/sxyx2008/謝謝合作!!!

    雪山飛鵠

    溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://m.tkk7.com/sxyx2008/謝謝合作!!!

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
    環境:
            ibatis-2.3.4.726
            使用ibatis2最小jar包配置
            commons-collections-3.2.1.jar
            commons-dbcp-1.4.jar
            commons-pool-1.5.4.jar
            ibatis-2.3.4.726.jar
            數據庫驅動
            mysql-connector-java-3.1.13-bin.jar
            ibatis入門小程序
            使用ibatis完成單張表的crud操作
    在使用ibatis之前,我們需要明確表之間的關系,需要先創建數據庫和表及表之間的對應關系
    這里先使用單張表來介紹ibatis的用法
    創建book表
    CREATE TABLE book
    (
    id 
    int auto_increment primary key,
    name 
    varchar(200),
    author 
    varchar(200),
    price 
    int,
    pub 
    varchar(200)
    )ENGINE
    =InnoDB DEFAULT CHARSET=utf8;
    編寫對應book的實體類
    package com.ibatis.model;

    public class Book {

        
    private int id;
        
    private String name;
        
    private String author;
        
    private int price;
        
    private String pub;

        
    public Book() {

        }

        
    public Book(int id, String name, String author, int price, String pub) {
            
    super();
            
    this.id = id;
            
    this.name = name;
            
    this.author = author;
            
    this.price = price;
            
    this.pub = pub;
        }

        
    public int getId() {
            
    return id;
        }

        
    public void setId(int id) {
            
    this.id = id;
        }

        
    public String getName() {
            
    return name;
        }

        
    public void setName(String name) {
            
    this.name = name;
        }

        
    public String getAuthor() {
            
    return author;
        }

        
    public void setAuthor(String author) {
            
    this.author = author;
        }

        
    public int getPrice() {
            
    return price;
        }

        
    public void setPrice(int price) {
            
    this.price = price;
        }

        
    public String getPub() {
            
    return pub;
        }

        
    public void setPub(String pub) {
            
    this.pub = pub;
        }

        @Override
        
    public String toString() {
            
    return "id:" + this.getId() + "\tname:" + this.getName() + "\tauthor:"
                    
    + this.getAuthor() + "\tprice:" + this.getPrice() + "\tpub:"
                    
    + this.getPub();
        }
    }
    在Book類的同目錄(包)下創建Book.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE sqlMap      
        PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"      
        "http://ibatis.apache.org/dtd/sql-map-2.dtd"
    >
        
    <sqlMap namespace="Book">
            
    <typeAlias alias="Book" type="com.ibatis.model.Book"/>
            
            
    <!-- 查詢所有 -->
            
    <select id="selectAllBook" resultClass="Book">
                select * from book order by price desc
            
    </select>
            
            
    <!-- 根據編號查詢 -->
            
    <select id="queryBookById" parameterClass="java.lang.Integer" resultClass="Book">
                select * from book where id=#id#
            
    </select>
            
            
            
    <!-- 根據書名稱模糊查詢 -->
            
    <select id="queryBookLikeName" resultClass="Book" parameterClass="java.lang.String">
                select * from book where name like '%$name$%'
            
    </select>
            
            
            
    <!-- 模糊查詢 -->
            
    <select id="pageQueryBook" resultClass="Book">
                select * from book
            
    </select>
            
            
    <!-- 添加 -->
            
    <insert id="insertBook" parameterClass="Book">
                insert into book (name,author,price,pub) values(#name#,#author#,#price#,#pub#)
            
    </insert>
            
            
    <!-- 刪除 -->
            
    <delete id="deleteBook" parameterClass="java.lang.Integer">
                delete from book where id=#id#
            
    </delete>
            
            
    <!-- 修改 -->
            
    <update id="updateBook" parameterClass="Book">
                update book set name=#name#,author=#author#,price=price where id=#id#
            
    </update>
            
            
            
    <!-- 動態查詢 查詢書名中含o的且作者姓名含李的 -->
            
    <select id="dynaicQuery" parameterClass="Book" resultClass="Book">
                select * from book
                
    <dynamic prepend="WHERE">
                    
    <isNotEmpty prepend="AND" property="name">
                        (name like '%$name$%')
                    
    </isNotEmpty>
                    
    <isNotEmpty prepend="AND" property="author">
                        (author like '%$author$%')
                    
    </isNotEmpty>
                    
    <isNotEmpty prepend="AND" property="price">
                        (price != #price#)
                    
    </isNotEmpty>
                
    </dynamic>
            
    </select>
            
        
    </sqlMap>
    在classpath目錄下一次添加ibatis.properties、SqlMapConfig.xml
    ibatis.properties
    JDBC.Driver=com.mysql.jdbc.Driver
    JDBC.ConnectionURL=jdbc:mysql://localhost:3306/ibatis
    JDBC.Username=root
    JDBC.Password=root
    SqlMapConfig.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE sqlMapConfig      
        PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"      
        "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"
    >
        
    <sqlMapConfig>
            
    <properties resource="ibatis.properties"/>
            
    <settings
                
    cacheModelsEnabled="true"
                enhancementEnabled
    ="true"
                lazyLoadingEnabled
    ="true"
                maxRequests
    ="10"
                maxSessions
    ="5"
                useStatementNamespaces
    ="false"
                maxTransactions
    ="5"
                errorTracingEnabled
    ="true"
            
    />
            
    <transactionManager type="JDBC" commitRequired="false">
                
    <dataSource type="DBCP">
                    
    <property name="JDBC.Driver" value="${JDBC.Driver}"/>
                    
    <property name="JDBC.ConnectionURL" value="${JDBC.ConnectionURL}"/>
                    
    <property name="JDBC.Username" value="${JDBC.Username}"/>
                    
    <property name="JDBC.Password" value="${JDBC.Password}"/>
                    
    <property name="Pool.MaximumActiveConnections" value="25"/>
                    
    <property name="Pool.MaximumIdleConnections" value="5"/>
                    
    <property name="Pool.MaximumCheckoutTime" value="12000"/>
                    
    <property name="Pool.TimeToWait" value="500"/>
                
    </dataSource>
            
    </transactionManager>
            
    <sqlMap resource="com/ibatis/model/Book.xml"/>
        
    </sqlMapConfig>
    編寫ibatis工具類
    package com.ibatis.util;

    import java.io.IOException;
    import java.io.Reader;

    import com.ibatis.common.resources.Resources;
    import com.ibatis.sqlmap.client.SqlMapClient;
    import com.ibatis.sqlmap.client.SqlMapClientBuilder;

    public class IbatisUtil {
        
        
    private static SqlMapClient client;
        
        
    static{
            Reader reader 
    = null;
            
    try {
                reader 
    = Resources.getResourceAsReader("SqlMapConfig.xml");
            } 
    catch (IOException e) {
                e.printStackTrace();
            }
            client
    =SqlMapClientBuilder.buildSqlMapClient(reader);
        }

        
    public static SqlMapClient getClient() {
            
    return client;
        }
    }
    編寫BookDAO
    package com.ibatis.dao;

    import java.sql.SQLException;
    import java.util.List;

    import org.junit.Test;

    import com.ibatis.model.Book;
    import com.ibatis.sqlmap.client.SqlMapClient;
    import com.ibatis.util.IbatisUtil;

    public class BookDAO {
        
        
    /**
         * 查詢所有
         
    */
        @SuppressWarnings(
    "unchecked")
        @Test
        
    public void queryAllBook()
        {
            SqlMapClient client
    =IbatisUtil.getClient();
            
    try {
                List
    <Book> list=client.queryForList("selectAllBook");
                
    for (Book book : list) {
                    System.out.println(book);
                }
            } 
    catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        
    /**
         * 按Id查詢
         
    */
        @SuppressWarnings(
    "unchecked")
        @Test
        
    public void queryBookById(){
            SqlMapClient client
    =IbatisUtil.getClient();
            
    try {
                List
    <Book> list=client.queryForList("queryBookById"2);
                
    for (Book book : list) {
                    System.out.println(book);
                }
            } 
    catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        
    /**
         * 根據屬名稱模糊查詢
         
    */
        @SuppressWarnings(
    "unchecked")
        @Test
        
    public void queryBookLikeName()
        {
            SqlMapClient client
    =IbatisUtil.getClient();
            
    try {
                List
    <Book> list=client.queryForList("queryBookLikeName","ext");
                
    for (Book book : list) {
                    System.out.println(book);
                }
            } 
    catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        
    /**
         * 分頁查詢
         
    */
        @SuppressWarnings(
    "unchecked")
        @Test
        
    public void pageQueryBook()
        {
            SqlMapClient client
    =IbatisUtil.getClient();
            
    try {
                List
    <Book> list=client.queryForList("pageQueryBook"55);
                
    for (Book book : list) {
                    System.out.println(book);
                }
            } 
    catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        
    /**
         * 添加
         
    */
        @Test
        
    public void insertBook()
        {
            SqlMapClient client
    =IbatisUtil.getClient();
            
    try {
                client.startTransaction();
                Book book
    =new Book(0,"oracle dba 入門手冊","馮靖",108,"電子工業出版社");
                client.insert(
    "insertBook", book);
                client.commitTransaction();
            } 
    catch (SQLException e) {
                e.printStackTrace();
                
    try {
                    client.endTransaction();
                } 
    catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            
        }
        
        
        
    /**
         * 刪除
         
    */
        @Test
        
    public void deleteBook(){
            SqlMapClient client
    =IbatisUtil.getClient();
            
    try {
                client.startTransaction();
                client.delete(
    "deleteBook"91);
                client.commitTransaction();
            } 
    catch (SQLException e) {
                e.printStackTrace();
                
    try {
                    client.endTransaction();
                } 
    catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            
        }
        
        
    /**
         * 修改
         
    */
        @Test
        
    public void updateBook(){
            SqlMapClient client
    =IbatisUtil.getClient();
            
    try {
                client.startTransaction();
                Book book
    =(Book) client.queryForObject("queryBookById",3);
                book.setName(
    "oracle入門診斷實例手冊");
                book.setAuthor(
    "蓋國強");
                book.setPrice(
    108);
                client.update(
    "updateBook", book);
                client.commitTransaction();
            } 
    catch (SQLException e) {
                e.printStackTrace();
                
    try {
                    client.endTransaction();
                } 
    catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        }
        
        
    /**
         * 根據書名以及(AND)作者信息動態查詢
         * 查詢書名是包含o以及作者姓名中含強的書籍
         
    */
        @SuppressWarnings(
    "unchecked")
        @Test
        
    public void dynaicQuery(){
            
            SqlMapClient client
    =IbatisUtil.getClient();
            Book book
    =new Book();
            book.setName(
    "o");
            book.setAuthor(
    "");
            book.setPrice(
    55);
            
    try {
                List
    <Book> list=client.queryForList("dynaicQuery", book);
                
    for (Book books : list) {
                    System.out.println(books);
                }
            } 
    catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        
    }
    over!到此ibatis但張表的crud結束
    任何疑問QQ:184675420 sxyx2008@163.com
    下篇ibatis的多對一雙向關聯
    posted on 2010-10-13 12:06 雪山飛鵠 閱讀(2756) 評論(1)  編輯  收藏 所屬分類: ibatis

    Feedback

    # re: 使用ibatis完成持久化工作 2011-12-27 13:35 akwolf
    樓主,很好的文章,下篇怎么沒有了?  回復  更多評論
      

    主站蜘蛛池模板: 成人毛片18女人毛片免费96| 五月天婷婷精品免费视频| 51精品视频免费国产专区| 午夜亚洲www湿好大| 国产97视频人人做人人爱免费| 亚洲精品无码日韩国产不卡?V| 免费看内射乌克兰女| 亚洲男人天堂2020| 你懂的网址免费国产| 久久亚洲中文字幕精品有坂深雪 | 亚洲乱码一区二区三区国产精品| 在线观看特色大片免费视频| 亚洲偷自拍另类图片二区| 在线日韩av永久免费观看| 高潮毛片无遮挡高清免费| 亚洲国产精品无码专区影院| 日本视频在线观看永久免费| 亚洲熟妇无码爱v在线观看| 久久综合AV免费观看| 特黄aa级毛片免费视频播放| 自拍偷自拍亚洲精品情侣| 久久精品免费一区二区三区| 亚洲乱码一二三四区国产| 女人18毛片免费观看| 黄页网站在线观看免费| 久久久久久久尹人综合网亚洲| 久久w5ww成w人免费| 久久亚洲精品国产亚洲老地址| 亚洲精品第一国产综合境外资源| 手机看片国产免费永久| 国产成人精品亚洲日本在线| gogo全球高清大胆亚洲| 日本中文字幕免费高清视频| 亚洲中文字幕无码爆乳app| 亚洲精品国精品久久99热| 中文字幕成人免费视频| 免费在线观看亚洲| 久久亚洲精品国产精品| 免费国产真实迷j在线观看| 日本在线免费观看| 老司机免费午夜精品视频|