jdbc

 

批更新(BatchUpdate)

 核心:PreparedStatement

  • 1.将绑定的参试,缓存在java本地
    • pstm.addBatch()
  • 2.将缓存在java本地的多组参数,一次性发给Oracle
    • pstm.executeBatch()
  • for(int i=0;i<100000;i++) { pstm.setInt(1, 113); pstm.setString(2,"老华"); pstm.setInt(3,1); pstm.setInt(4,18); pstm.setString(5, "456789"); pstm.setString(6,"123@qq.com"); pstm.addBatch(); if(i%500==0) { pstm.executeUpdate(); } }

 

工具封装

properties作用:加载properties配置文件中的内容,读取到properties对象内部

  • 1.load(输入流);//根据文件输入流给定的文件,加载到properties对象中
  • 2.getProperty(“key”);//根据key 获得value
1.创建Propertie锁对象
Properties props = new Properties();
2.输入流,通过load方法加载配置文件信息 InputStream is
= 类名.class.getResourceAsStream("相对路径 ");//从src下开始 props.load(is); Class.forName(props.getProperty("名称"));
conn = DriveManager.getConnection(props.getProperty("url"),props.getProperty("user"),props.getProperty("pws");

 

 静态封装

 1 public class Test3{
 2      private static final Properties props = new Properties();
 3      static {
 4                InpuStream is = null;
 5                is = JdbcUtil3.class.getResourceAsStream("/**/***/**/**/jdbc.properties");
 6                props.load(is);
 7     }
 8                is.close();
 9                    
10 }

  

ORM模型设计

  • ORM:将数据库中的一行数据映射成一个java对象,【对象-----表行数据】
  • ORM类设计原则: 对象-----一行数据

 

Entity实体对象

public class 表名 implements Serializable{
          private Integer   id ;//id列
          private String     name;
          private Integer   sex;
          private String     ;
          //要求:提供get/set方法
          //建议:提供无参构造方法,有参构造方法
}

 

DAO模型设计

  

 Service设计模式:

  •  1.一张表
  • 2.书写实体
public class 表名 implements Serializable{
    private String id ;
    ..
    ..
    ..
   //有参  无参构造方法
   //get   set  方法
}

java代码表示一条数据

  • 3.书写DAO
 public calss  UserDAO{
  public User selectByUsername(String username){   
    Connection conn = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try{
          1.获得链接conn
          2.准备好sql,创建pstm
          3.绑定参数
          4.发送参数,执行sql
          5.处理结果集
    }catch(Exception e){
           throw new RuntimeException("xxxx",e);
    }finally{
           6.释放资源
            JdbcUtil.close(rs,pstm,null);//不关闭conn
    } 
   }
}
  • 4.书写Service

登陆功能(方法)的实现

public class UserService {
    //登录:用户输入username和password,验证是否能够登录成功
    public boolean login(String username,String password) {
        Connection conn = null;
        try {
            //0.开启事务,设置事务提交为手动
            conn = JdbcUtil.getConnection();
            conn.setAutoCommit(false);
            //1.判断username是否存在
            UserDAO userDAO = new UserDAO();
            User user = userDAO.selectByUsername(username);
            if(user == null) 
                return false;
            //2.判断password是否正确    
            if(!password.equals(user.getPassword()))
                return false;
            //提交事务  commit
            conn.commit();
        } catch (Exception e) {
            //回滚事务 rollback
            conn.rollback();
            throw new RunTimeException("登录异常",e);
        }finally {
            //关闭conn
            JdbcUtil.close(null,null,conn);
        }
        //3.登录成功
        return true;
    }
}

 

 

JDBC控制事务

核心思想:

  • 1.一个功能对应service的一个方法
  • 2.service一个方法就是不可分割的执行单元
  • 3.service每个方法的代码都要放在事务控制之内

Connection事务相关方法:

  • setAutoCommit(false)   开启事务   将事务控制设置为手动提交
  • commit()  提交
  • rollback() 回滚

模板:

public class XxxService{
    public void 功能方法(){
      try{
            //1.开启事务
            业务操作+DAO调用
            //2.提交事务   commit  
      }catch(Exception e){
            //3.回滚操作,rollback
     }finally{
     //关闭conn(dao不能关闭conn)
    
} } }

 

 

事务控制问题

  • service和dao使用的不是同一个链接
  • 将service的conn通过dao的方法,传递给dao
  • dao接受service的conn
  • dao不能关闭conn
  • service最后要关闭conn 
  • 缺点是代码污染

 

 ThreadLocal:

作用:操作当前线程内部一块存储空间(属性)

借助当前线程Thread共享(传递)数据

创建:new ThreadLocal<泛型>();

  • 1.threadLocal.set(值);将数据存入当前线程
  • 2.threadLocal.get();从当前线程中获取值
  • 3.threadLocal.remove();从当前线程中移除值

 

 编码步骤:

  •  1.导入jar   导入工具类   导入配置文件

  • 2.表

  • 3.实体

  • 4.开发UserDAO    接口:XxxDAO

  • 5.开发UserDAO    实现类:XxxDAOImpl

  • 6.开发UserService   接口:XxxService

  • 7.开发UserService   实现类:XxxServiceImpl

  • 8.开发View

 

 

 

 

 

 

  

 

posted @ 2020-05-27 10:14  华哥好棒棒  阅读(182)  评论(0编辑  收藏  举报