MyBatis框架概述
- mybatis是一个优秀的基于java的持久层框架,它内部封装了jdbc,使开发者只需要关注sql语句的本身,而不需要花费精力去处理驱动、创建连接、创建statement等繁杂的过程。
- mybatis通过xml或者注解的方式将要执行的各种statement配置起来,并通过Java对象和statement中sql的动态参数经行映射生成最终的执行的sql语句,最后由mybatis框架执行sql并将结果映射为java对象返回
- 采用了ORM思想解决了实体和数据库映射的问题,对jdbc进行了封装,屏蔽了jdbc api 底层访问细节,使我们不用与jdbc api 打交道,就可以完成对数据库的持久化操作。
jdbc程序回顾
public static void main(String[] args) {
Connection con = null;
Statement statement = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/day04_db", "root", "123");
statement = con.createStatement();
String sql = "select * from user";
rs = statement.executeQuery(sql);
while(rs.next()){
String id = rs.getInt("id");
String name = rs.getString(2);
int pwd= rs.getString(3);
System.out.println(id+"+++"+name+"++++"+pwd);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (statement != null) {
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/day04_db", "root", "123");
statement = connection.createStatement();
String sql = "insert into user values(null, 'zhaoliu', 'abc')";
int update = statement.executeUpdate(sql);
System.out.println("插入了"+update+"条数据");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if (statement != null) {
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/day04_db", "root", "123");
stmt = conn.createStatement();
String sql = "update user set username='tianqi' where username='zhaoliu'";
int sum = stmt.executeUpdate(sql);
System.out.println(sum);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/day04_db", "root", "123");
stmt = conn.createStatement();
String sql = "delete from user where id=4";
int sum = stmt.executeUpdate(sql);
System.out.println(sum);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}