java之 Mybatis框架

  一、三層框架:mysql

      表現層:sql

          是用於展現數據數據庫

      業務層:mybatis

          是處理業務需求app

      持久層:框架

          是和數據庫交互spa

  注:MyBatis在持久層code

 

  二、JDBC操做數據庫對象

public static void main(String[] args) {
  Connection connection = null;
  PreparedStatement preparedStatement = null;
  ResultSet resultSet = null;
  try {
    //加載數據庫驅動
    Class.forName("com.mysql.jdbc.Driver");
    //經過驅動管理類獲取數據庫連接
    connection = DriverManager
    .getConnection("jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8","ro
    ot", "root");
    //定義 sql 語句 ?表示佔位符
    String sql = "select * from user where username = ?";
    //獲取預處理 statement
    preparedStatement = connection.prepareStatement(sql);
    //設置參數,第一個參數爲 sql 語句中參數的序號(從 1 開始),第二個參數爲設置的參數值
    preparedStatement.setString(1, "王五");
    //向數據庫發出 sql 執行查詢,查詢出結果集
    resultSet = preparedStatement.executeQuery();
    //遍歷查詢結果集
    while(resultSet.next()){
      System.out.println(resultSet.getString("id")+" "+resultSet.getString("username"));
    }
  } catch (Exception e) {
    e.printStackTrace();
  }finally{
    //釋放資源
    if(resultSet!=null){
    try {
      resultSet.close();
    } catch (SQLException e) {
      e.printStackTrace();
    } 
  }
  
if(preparedStatement!=null){     try {       preparedStatement.close();     } catch (SQLException e) {       e.printStackTrace();     }
  }
  
if(connection!=null){   try {     connection.close();   } catch (SQLException e) { // TODO Auto-generated catch block     e.printStackTrace();     }
  }
}
}

   三、ORM(對象關係映射):Object Relational Mapping  blog

    簡單說:

      就是把 數據庫表實體類 以及 實體類 對應的 屬性 對應起來

      讓咱們能夠經過操做 實體類 實現對 數據庫表 的操做。

  四、<selectKey>標籤

  五、QueryVo 實現多個實體類的參數傳遞。

  六、

 

 待續。。。。

相關文章
相關標籤/搜索