欢迎来到DIVCSS5查找CSS资料与学习DIV CSS布局技术!
Mybatis简介
 
学习框架前,先梳理原始JDBC如何操作数据库
 
原始JDBC操作
 
查询数据
 
引入依赖
 
    <dependencies>
 
        <dependency>
 
            <groupId>junit</groupId>
 
            <artifactId>junit</artifactId>
 
            <version>4.13.2</version>
 
            <scope>test</scope>
 
        </dependency>
 
        <dependency>
 
            <groupId>mysql</groupId>
 
            <artifactId>mysql-connector-java</artifactId>
 
            <version>8.0.26</version>
 
        </dependency>
 
    </dependencies>
 
测试查询数据
 
测试用表
 
数据库表映射类
 
package com.jtyhnet.domain;
 
public class Account {
 
    private int id;
 
    private String name;
 
    private float money;
 
    public int getId() {
 
        return id;
 
    }
 
    public void setId(int id) {
 
        this.id = id;
 
    }
 
    public String getName() {
 
        return name;
 
    }
 
    public void setName(String name) {
 
        this.name = name;
 
    }
 
    public float getMoney() {
 
        return money;
 
    }
 
    public void setMoney(float money) {
 
        this.money = money;
 
    }
 
    @Override
 
    public String toString() {
 
        return "account{" +
 
                "id=" + id +
 
                ", name='" + name + '\'' +
 
                ", money=" + money +
 
                '}';
 
    }
 
}
 
测试代码
 
import com.jtyhnet.domain.Account;
 
import org.junit.Test;
 
import java.sql.*;
 
public class JDBCTest1 {
 
    @Test
 
    public void test1() throws ClassNotFoundException, SQLException {
 
        //注册驱动
 
        Class.forName("com.mysql.jdbc.Driver");
 
        //获得连接
 
        Connection connection = DriverManager.getConnection("jdbc:mysql:///test", "root", "root");
 
        //获得statement
 
        PreparedStatement preparedStatement = connection.prepareStatement("select * from account");
 
        //执行查询
 
        ResultSet resultSet = preparedStatement.executeQuery();
 
        //遍历结果集
 
        while (resultSet.next()){
 
            //封装实体
 
            Account account = new Account();
 
            account.setId(resultSet.getInt("id"));
 
            account.setName(resultSet.getString("name"));
 
            account.setMoney(resultSet.getFloat("money"));
 
            //实体封装完成
 
            System.out.println(account);
 
        }
 
        //释放资源
 
        resultSet.close();
 
        preparedStatement.close();
 
        connection.close();
 
    }
 
}

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h64794.shtml