1. 程式人生 > >myBatis實現讀寫資料庫

myBatis實現讀寫資料庫

一、MyBatis:
MyBatis是一個支援普通SQL查詢,儲存過程和高階對映的優秀持久層框架。MyBatis消除了幾乎所有的JDBC程式碼和引數的手工設定以及對結果集的檢索封裝。MyBatis可以使用簡單的XML或註解用於配置和原始對映,將介面和Java的POJO(Plain Old Java Objects,普通的Java物件)對映成資料庫中的記錄。
二、準備:
資料庫結構:
這裡寫圖片描述
使用maven新增相應的jar包:

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency
>
<groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId
>
mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.6</version> </dependency>

專案目錄結構:
這裡寫圖片描述
資原始檔的conf.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <!-- 配置資料庫連線資訊 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="root" /> <property name="password" value="mysql" /> </dataSource> </environment> </environments> </configuration>

實體類:User

/**   
* @Title: User.java 
* @Package com.king.model 
* @Description: TODO 
* @date 2017年12月25日 上午9:21:57 
* @version V1.0   
*/
package com.king.model;

public class User {

    private int id;
    private String username;
    private String password;
    private int age;

    public int getId() {
        return id;
    }
    public void setId(int 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;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", password=" + password + ", age=" + age + "]";
    }


}

對映檔案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="com.king.mapping.userMapper">
    <!-- 在select標籤中編寫查詢的SQL語句,設定select標籤的id屬性為getUser,id屬性必須唯一的,不能重複使用
    parameterType屬性指名查詢時使用的引數型別,resultType屬性指明查詢返回的結果集型別
    resultType="com.king.model.User"就表示將查詢結果封裝成一個User類的物件返回
    User就是user表所對應的實體類 -->

    <!-- 根據ID查詢一個user物件 -->
    <select id="getUser" parameterType="int" resultType="com.king.model.User">
        select * from user where id=#{id}
    </select>
</mapper>

然後配置conf.xml將對映加進去:

 <mappers>
        <!-- 註冊userMapper.xml檔案
        userMapper.xml位於com.king.mapping這個包下,所以resource寫成com/king/mapping/userMapper.xml
         -->
        <mapper resource="com/king/mapping/userMapper.xml"></mapper>
    </mappers>

測試類,測試是否能夠對資料庫進行讀寫:

package com.king.test;

import java.io.InputStream;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.king.model.User;

public class Test {

    public static void main(String[] args) {
        String resource="conf.xml";
        InputStream is = Test.class.getClassLoader().getResourceAsStream(resource);

        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = sessionFactory.openSession();
        String statement = "com.king.mapping.userMapper.getUser";
        User user = session.selectOne(statement,1);
        System.out.println(user);
    }
}