框架-MyBatis学习

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 statement = null;
		ResultSet rs = null;
		try {
			// 1.注册驱动
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/day04_db", "root", "123");
			statement = con.createStatement();
			//发送并执行sql
			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) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			// 6 释放资源
			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 statement = null;
		try {
			//注册驱动
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/day04_db", "root", "123");
			statement = connection.createStatement();
			//发送sql
			String sql = "insert into user values(null, 'zhaoliu', 'abc')";
			int update = statement.executeUpdate(sql);
			System.out.println("插入了"+update+"条数据");
			//释放资源
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			// 6 释放资源
			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");
			// 获得发送sql的对象
			stmt = conn.createStatement();
			// 执行sql 获得结果
			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 {
			// 6 释放资源
			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");
			// 获得发送sql的对象
			stmt = conn.createStatement();
			// 执行sql 获得结果
			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 {
			// 6 释放资源
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}

			try {
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值