一,mybatis源码解析核心配置文件
1.1传统的jdbc
先看一看传统的jdbc,这样有助于学习mybatis:
@Test
public void test() throws SQLException {
Connection conn=null;
PreparedStatement pstmt=null;
try {
// 1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 2.创建连接
conn= DriverManager.
getConnection("jdbc:mysql://localhost:3306/mybatis_example", "root", "123456");
// SQL语句
String sql="select id,user_name,create_time from t_user where id=?";
// 获得sql执行者
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1,1);
// 执行查询
pstmt.execute();
ResultSet rs= pstmt.getResultSet();
rs.next();
User user =new User();
user.setId(rs.getLong("id"));
user.setUserName(rs.getString("user_name"));
user.setCreateTime(rs.getDate("create_time"));
System.out.println(user.toString());
} catch (Exception e) {
e.printStackTrace();
}finally{
// 关闭资源
try {
if(conn!=null){
conn.close();
}
if(pstmt!=null){
pstmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2022/5/26大约 21 分钟