实习已经半年多了,从刚进入公司开始接触到框架,其中ORM框架Hibernate、mybatis。到目前为止,都会用,但是具体的原理不懂,所以开始系统的学习一下。先从mybatis开始。
参照 《深入浅出Mybatis技术原理与实战——杨开振》,代码:https://github.com/yangyang5214/mybatis
传统的jdbc编程
Java程序都是通过JDBC(Java Data Base Connectivity连接数据库的,这样就可以通过Sql对数据库编码。
传统的jdbc编程主要分为下面几步:
1、连接数据库,注册驱动和数据库信息
2、操作Connection,打开Statement对象
3、通过Statement执行sql,返回结果到ResultSet对象。
4、使用ResultSet读取数据,然后通过代码转换为pojo对象。
5、关闭数据库相关资源。
下面以一个根据id查询用户的例子看一下具体的实现:
1、基础数据,表的创建
CREATE TABLE USER (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR (10),
age LONG,
pwd VARCHAR (10)
);
INSERT INTO USER(username,age,pwd)
VALUES ('爬爬',20,111),
('殃殃','10',222),
('祸祸','30',333)
2、具体实现
public class JdbcTest {
public static void main(String[] args) {
JdbcTest jdbcTest = new JdbcTest();
UserDto userDto = jdbcTest.getUserById(1);
System.out.println(userDto);
}
/**
* 注册驱动和数据库相关信息
* @return
*/
public Connection getConnection(){
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/mybatis";
String username = "root";
String pwd = "admin";
connection = DriverManager.getConnection(url,username,pwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
/**
* 通过Statement执行sql,返回结果到resultSet对象
* @param userId
* @return
*/
public UserDto getUserById(long userId){
Connection connection = getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "SELECT * FROM user where id = ?";
try {
ps = connection.prepareStatement(sql);
ps.setLong(1,userId);
rs = ps.executeQuery();
while (rs.next()){
UserDto userDto = new UserDto();
userDto.setId(rs.getLong("id"));
userDto.setAge(rs.getLong("age"));
userDto.setUsername(rs.getString("username"));
userDto.setPwd(rs.getString("pwd"));
return userDto;
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
colse(rs,ps,connection);
}
return null;
}
/**
* 关闭数据库相关资源
* @param rs
* @param ps
* @param connection
*/
public void colse(ResultSet rs,PreparedStatement ps,Connection connection){
try {
if (rs != null && !rs.isClosed()){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null && !ps.isClosed()){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null && !connection.isClosed()){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
3、UserDto类
public class UserDto {
private long id;
private String username;
private String pwd;
private long age;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public long getAge() {
return age;
}
public void setAge(long age) {
this.age = age;
}
@Override
public String toString() {
return "UserDto{" +
"id=" + id +
", username='" + username + '\'' +
", pwd='" + pwd + '\'' +
", age=" + age +
'}';
}
}
4、结果