mybatis入門篇(一):經過SqlSession.selectList進行數據查詢

做爲一個java菜鳥,早就從慕課網中學到一些基本的mybatis的用法,可是一直不成體系,懵懵懂懂,既然正式入了java這個坑,就打算好好學學,因此買了本《MyBatis從入門到精通》,在此記錄下學習的點滴,便於往後溫習。java

強調一下:這些代碼是我根據書籍編寫的代碼並親自運行測試的,嚴格意義上說,這些並不是我我的原創mysql

數據庫版本:Server version: 8.0.15 MySQL Community Server - GPL 數據準備:sql

先寫一個最簡單的查詢 一、新建一個空白的maven項目,引入相關依賴包數據庫

<dependencies>
    <!--測試須要-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <!--mybatis-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.0</version>
    </dependency>
    <!--數據庫鏈接驅動-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.15</version>
    </dependency>
</dependencies>

二、在resources中添加mybatis的配置文件mybatis-config.xml 這裏選用最基本的jdbc事務管理器,不用數據庫鏈接池。此處由於mysql的版本緣由,要禁用SSL。同時配置數據庫實體的位置和mapper文件路徑。apache

<?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>
    <typeAliases>
        <package name="com.forest.owl.entity"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC">
                <property name="" value=""/>
            </transactionManager>
            <dataSource type="UNPOOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/forest?useSSL=false"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/UserMapper.xml" />
    </mappers>
</configuration>

二、編輯數據庫實體Usersession

import java.util.Date;
public class User {
    private int id;
    private String account;
    private String passwd;
    private String nickname;
    private String phone;
    private String email;
    private Date createTime;
    private Date updateTime;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getPasswd() {
        return passwd;
    }
    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }
    public String getNickname() {
        return nickname;
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    public Date getUpdateTime() {
        return updateTime;
    }
    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }
}

三、在resources中添加mapper文件夾,在該文件夾中添加UserMapper.xml文件 mapper文件的命名空間是必須的,這裏是隨便命名,暫且還不清楚該屬性的具體做用。mybatis

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper">
    <select id="selectUsers" resultType="com.forest.owl.entity.User">
        SELECT id, account, passwd, nickname, phone, email, create_time as createTime, update_time as updateTime
        FROM user
    </select>
</mapper>

四、編寫測試類app

import com.forest.owl.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
public class TestUserMapper {
    private static SqlSessionFactory sqlSessionFactory;
    @BeforeClass
    public static void init(){
        try {
            Reader reader = Resources.getResourceAsReader("mybatis.config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void TestMybatis(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> userList = sqlSession.selectList("selectUsers");
        System.out.println(userList.size());
        System.out.println(userList.get(0).getNickname());
        System.out.println(userList.get(0).getCreateTime());
    }
}

測試結果:maven

相關文章
相關標籤/搜索