從JDBC程式看為什麼需要Mybatis

來源:互聯網
上載者:User

標籤:etc   inno   ati   cut   mail   最好   url   statement   catch   

package com.xuebusi.jdbc;import java.sql.*;/** *  從JDBC程式看為什麼需要Mybatis * *  1.載入驅動程式和資料庫連接所需要的url、使用者名稱和密碼都是寫死在程式中的, *      後期更換資料庫的話還要修改程式。所以最好是定義到外部的設定檔中; * *  2.SQL語句也是寫死在程式中: *      (1)後期如果需要修改SQL語句的話,必須修改程式,重新編譯; *      (2)無法達到代碼重用的作用; *      (3)解決方案:最好將SQL語句也能夠寫死在設定檔; * *  3.給SQL語句設定參數有問題: *      (1)參數寫死了,不便於維護,最好能夠從外部傳遞進來; *      (2)需要判斷參數的類型,很繁瑣,最好能夠自動進行判斷; *      (3)需要手動判斷參數的位置(角標),繁瑣,最好能夠自動判斷; * *  4.遍曆結果集也存在問題: *      (1)需要判斷結果欄位的類型,麻煩; *      (2)需要手動指定欄位名稱,麻煩; *      最好能夠直接將結果集映射為JavaBean; * *  5.釋放資源也有問題,頻繁的建立串連和關閉串連,造成資源浪費,影響系統效能,最好能夠使用串連池; * *  而Mybatis正好可以解決上述JDBC程式所存在的一系列問題。 * Created by SYJ on 2017/2/14. */public class JdbcTest {    public static void main(String[] args) {        Connection connection = null;        PreparedStatement preparedStatement = null;        ResultSet resultSet = null;        try {            //載入驅動            Class.forName("com.mysql.jdbc.Driver");            //建立資料庫連接            String url = "jdbc:mysql://localhost:3306/xuebusi";            String user = "root";            String password = "root";            connection = DriverManager.getConnection(url, user, password);            //建立PreparedStatement            String sql = "SELECT * FROM tb_user WHERE id > ? limit ?";            preparedStatement = connection.prepareStatement(sql);            //給上面的SQL語句設定第一個參數            //注意參數角標是從1開始的            preparedStatement.setLong(1, 1L);            //給上面的SQL語句設定第二個參數,分頁參數            preparedStatement.setInt(2, 5);            //執行查詢            resultSet = preparedStatement.executeQuery();            //遍曆結果集            while (resultSet.next()){                String username = resultSet.getString("username");                String pass = resultSet.getString("password");                String phone = resultSet.getString("phone");                String email = resultSet.getString("email");                Date created = resultSet.getDate("created");                Date updated = resultSet.getDate("updated");                System.out.println("username:" + username + ",password:" + pass + ",phone:"                        + phone + ",email:" + email + ",created:" + created + ",updated:" + updated);            }        } catch (Exception e) {            e.printStackTrace();        } finally {            //釋放資源            if (null != resultSet) {                try {                    resultSet.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }            if (null != preparedStatement){                try {                    preparedStatement.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }            if (null != connection){                try {                    connection.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }        }    }}

 

/** 資料庫表結構 */CREATE TABLE `tb_user` (  `id` bigint(20) NOT NULL AUTO_INCREMENT,  `username` varchar(50) NOT NULL COMMENT ‘使用者名稱‘,  `password` varchar(32) NOT NULL COMMENT ‘密碼,加密儲存‘,  `phone` varchar(20) DEFAULT NULL COMMENT ‘註冊手機號‘,  `email` varchar(50) DEFAULT NULL COMMENT ‘註冊郵箱‘,  `created` datetime NOT NULL,  `updated` datetime NOT NULL,  PRIMARY KEY (`id`),  UNIQUE KEY `username` (`username`) USING BTREE,  UNIQUE KEY `phone` (`phone`) USING BTREE,  UNIQUE KEY `email` (`email`) USING BTREE) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8 COMMENT=‘使用者表‘;

 

從JDBC程式看為什麼需要Mybatis

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.