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

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

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

    溫馨提示:您的每一次轉(zhuǎn)載,體現(xiàn)了我寫此文的意義!!!煩請(qǐng)您在轉(zhuǎn)載時(shí)注明出處http://www.tkk7.com/sxyx2008/謝謝合作!!!

    雪山飛鵠

    溫馨提示:您的每一次轉(zhuǎn)載,體現(xiàn)了我寫此文的意義!!!煩請(qǐng)您在轉(zhuǎn)載時(shí)注明出處http://www.tkk7.com/sxyx2008/謝謝合作!!!

    BlogJava 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
      215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
    環(huán)境:
            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
            數(shù)據(jù)庫(kù)驅(qū)動(dòng)
            mysql-connector-java-3.1.13-bin.jar
            ibatis入門小程序
            使用ibatis完成單張表的crud操作
    在使用ibatis之前,我們需要明確表之間的關(guān)系,需要先創(chuàng)建數(shù)據(jù)庫(kù)和表及表之間的對(duì)應(yīng)關(guān)系
    這里先使用單張表來(lái)介紹ibatis的用法
    創(chuàng)建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;
    編寫對(duì)應(yīng)book的實(shí)體類
    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類的同目錄(包)下創(chuàng)建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>
            
            
    <!-- 根據(jù)編號(hào)查詢 -->
            
    <select id="queryBookById" parameterClass="java.lang.Integer" resultClass="Book">
                select * from book where id=#id#
            
    </select>
            
            
            
    <!-- 根據(jù)書名稱模糊查詢 -->
            
    <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>
            
            
            
    <!-- 動(dòng)態(tài)查詢 查詢書名中含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;
        }
    }
    編寫B(tài)ookDAO
    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();
            }
        }
        
        
    /**
         * 根據(jù)屬名稱模糊查詢
         
    */
        @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();
            }
        }
        
        
    /**
         * 分頁(yè)查詢
         
    */
        @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 入門手冊(cè)","馮靖",108,"電子工業(yè)出版社");
                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入門診斷實(shí)例手冊(cè)");
                book.setAuthor(
    "蓋國(guó)強(qiáng)");
                book.setPrice(
    108);
                client.update(
    "updateBook", book);
                client.commitTransaction();
            } 
    catch (SQLException e) {
                e.printStackTrace();
                
    try {
                    client.endTransaction();
                } 
    catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        }
        
        
    /**
         * 根據(jù)書名以及(AND)作者信息動(dòng)態(tài)查詢
         * 查詢書名是包含o以及作者姓名中含強(qiáng)的書籍
         
    */
        @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結(jié)束
    任何疑問(wèn)QQ:184675420 sxyx2008@163.com
    下篇ibatis的多對(duì)一雙向關(guān)聯(lián)
    posted on 2010-10-13 12:06 雪山飛鵠 閱讀(2756) 評(píng)論(1)  編輯  收藏 所屬分類: ibatis

    Feedback

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

    主站蜘蛛池模板: 精品国产污污免费网站aⅴ| 亚洲视频在线免费播放| 黄页免费在线观看| 97av免费视频| 在线免费观看中文字幕| 亚洲精品视频免费观看| 久久国产精品亚洲一区二区| 亚洲精品乱码久久久久久下载| 亚洲中文字幕AV每天更新| 九九九精品视频免费| 久草福利资源网站免费| 国产卡二卡三卡四卡免费网址| 四虎永久成人免费影院域名| 激情综合色五月丁香六月亚洲| 91亚洲精品视频| 鲁死你资源站亚洲av| 久久九九全国免费| 成人免费视频网址| 亚洲欧洲成人精品香蕉网| 亚洲人xxx日本人18| 一个人看的hd免费视频| 中国xxxxx高清免费看视频| 国产成人免费a在线视频色戒| 亚洲色偷拍另类无码专区| 亚洲一级片在线播放| 九一在线完整视频免费观看| 中文字幕天天躁日日躁狠狠躁免费| 国产精品视频免费一区二区三区 | 歪歪漫画在线观看官网免费阅读 | 精品亚洲一区二区三区在线观看| 亚洲国产精品自在线一区二区| 亚洲精品又粗又大又爽A片| 青青操免费在线观看| 女人18毛片a级毛片免费视频| 国产aⅴ无码专区亚洲av| 亚洲日产乱码一二三区别| 久久免费高清视频| 亚洲?V无码乱码国产精品| 亚洲午夜久久久精品电影院| igao激情在线视频免费| 成人免费无遮挡无码黄漫视频|