Mybatis 輕鬆入門教程

Mybatis框架 的快速入門html

MyBatis

 簡介

什麼是 MyBatis?

MyBatis 是支持普通 SQL 查詢,存儲過程和高級映射的優秀持久層框架。MyBatis 消除 了幾乎全部的 JDBC 代碼和參數的手工設置以及結果集的檢索。MyBatis 使用簡單的 XML 或註解用於配置和原始映射,將接口和 Java 的 POJOs(Plan Old Java Objects,普通的 Java 對象)映射成數據庫中的記錄。sql

Mybatis快速入門

1.添加jar包

mybatis:數據庫

  mybatis-3.1.1.jarsession

oracle:mybatis

 ojdbc6.jar(根據我的狀況,不一樣數據庫選擇相應的包)oracle

2.表

create table user_name (id number,name varchar2(200),age number);

insert into user_name values(1,'ddd',23);

insert into user_name values(2.'fff',24)

3.添加mybatis的配置文件config.xm

 

複製代碼

1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
 4 
 5 <configuration>
 6 
 7          <environments default="development">
 8 
 9                    <environment id="development">
10 
11                             <transactionManager type="JDBC" />
12 
13                             <dataSource type="POOLED">
14 
15                                      <property name="driver" value="oracle.jdbc.driver.OracleDriver" />
16 
17                                      <property name="url" value="jdbc:oracle:thin:@localhost:1521:Oracle" />
18 
19                                      <property name="username" value="root" />
20 
21                                      <property name="password" value="root" />
22 
23                             </dataSource>
24 
25                    </environment>
26 
27          </environments>
28 
29 </configuration>

複製代碼

 

4.定義表所對應的實體類

複製代碼

package cn.zr.maybatis;

public class User {
 
    private String id;
    
    private String userName;
    
    private String passWord;

    public User() {
        super();
        // TODO Auto-generated constructor stub
    }

    public User(String id, String userName, String passWord) {
        super();
        this.id = id;
        this.userName = userName;
        this.passWord = passWord;
    }

    public String getId() {
        return id;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", userName=" + userName + ", passWord=" + passWord + "]";
    }
    
    
    
}

複製代碼

 

5.定義操做user_name表的sql映射文件userMapper.xml

複製代碼

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
<mapper namespace="cn.zr.mybatis.userMapper"> 
<select id="getUser" parameterType="int" 
resultType="cn.zr.mybatis.User">
select * from user_name where id=#{id}
</select>
</mapper>

複製代碼

 

6.在config.xml文件中註冊userMapper.xml文件

<mappers>
<mapper resource="cn/zr/mybatis/userMapper.xml"/>
</mappers>

 

7.編寫測試代碼,執行定義的select語句

複製代碼

public class Test {

         public static void main(String[] args) throws IOException {

                   String resource = "config.xml";

                   //加載mybatis的配置文件(它也加載關聯的映射文件)

                   Reader reader = Resources.getResourceAsReader(resource);

                   //構建sqlSession的工廠

                   SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);

                   //建立能執行映射文件中sql的sqlSession

                   SqlSession session = sessionFactory.openSession();

                   //映射sql的標識字符串

                   String statement = "com.atguigu.mybatis.bean.userMapper"+".selectUser";

                   //執行查詢返回一個惟一user對象的sql

                   User user = session.selectOne(statement, 1);

                   System.out.println(user);

                  session.commit();

                  session.close();

         }

}

複製代碼

相關文章
相關標籤/搜索