JDBC中自定义工具类(以及properties配置文件的使用)

10 篇文章 0 订阅


JDBC中自定义工具类

Java数据库连接后的大部分操作都需要经过

定义需要的工具类对象(定义变量)
加载驱动
定义用户名密码及url来获得链接
得到(预)状态通道、(预状态通道则还需要绑定参数)
增删改查
关闭资源

等步骤,我们可以将这些重复步骤代码以方法的形式进行封装


方法封装

新建工具包util,包中新建类DBUtils,在类中将上述操作的重复代码以方法的形式进行封装

定义需要的工具类对象(定义变量)

(子类中方法的访问权限不能比父类中的访问权限低)

	protected Connection connection = null;
    protected PreparedStatement pps = null;//后续都是用预状态通道来实现
    protected ResultSet rs = null;//结果集
    protected int count = 0;//受影响的行数

加载驱动

静态代码块在类加载到内存中时便执行

	static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

定义用户名密码及url来获得链接

	//登录的用户名和密码
    private String username = "root";
    private String password = "123456";

    //url地址
    private String url = "jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT";

    protected Connection getConnection() {
        try {
            connection = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

得到预状态通道

	protected PreparedStatement getPps(String sql) {
        try {
            getConnection();
            pps = connection.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pps;
    }

绑定参数

给占位符赋值,list中保存的是给占位符所赋的值

	 private void setParams(List list) {
        try {
            if (list != null && list.size() > 0) {//集合中有内容
                for (int i = 0; i < list.size(); i++) {
                    pps.setObject(i + 1, list.get(i));//赋值,位置从1开始所以为i+1
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

增删改

    protected int update(String sql, List list) {
        try {
            getPps(sql);//得到预状态通道
            setParams(list);//绑定参数
            count = pps.executeUpdate();//pps.executeUpdate()执行sql语句,返回受影响的行数
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;//返回受影响的行数
    }

查询

    protected ResultSet query(String sql, List list) {
        try {
            getPps(sql);//得到预状态通道
            setParams(list);//绑定参数
            rs = pps.executeQuery();//pps.executeUpdate()执行sql语句,返回结果集
            return rs;//返回结果集
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

关闭资源

    protected void closeAll() {
        try {
            if (rs != null) {
                rs.close();
            }
            if (pps != null) {
                pps.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

应用

例1

以student表的通过学生id查学生信息为例

DaoImplements类除了实现Dao接口,还要继承DBUtil类

	public Student getByStuId(int stuid) {
        Student student = new Student();
        try {
            String sql = "select* from student where stuid = ?";
            List list = new ArrayList();
            list.add(stuid);
            ResultSet query = query(sql, list);//使用封装的查询方法
            while(rs.next()){
                student.setStuId(rs.getInt("stuid"));
                student.setStuName(rs.getString("stuname"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll();//使用封装的关闭资源方法
        }
        return student;
    }

编写测试类Test

public class Test{
    public static void main(String[] args) {
        DaoImplements dao = new DaoImplements();
        Student student = dao.getByStuId(1);//查询id为1的学生
        System.out.println(student.getStuId() + "," + student.getStuName());
    }
}

输出结果:

1,张三

例2

以非反射处理结果集和反射处理结果集为例,原替换的代码用/**/注释

反射处理结果集内容可阅读文章:JDBC中批处理及通过反射处理结果集

DaoImplements类除了实现Dao接口,还要继承DBUtil类

非反射处理:

    @Override
    public List<Student> getAllStudent() {
        /*
        Connection connection = null;
        PreparedStatement pps = null;
        ResultSet resultSet = null;
         */

        try {
            /*
            //1加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2获得链接
            String userName = "root";
            String passWord = "123456";
            String url = "jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT";
            connection = DriverManager.getConnection(url, userName, passWord);
            //3定义sql,创建预状态通道(进行sql语句的发送)
            String sql = "select * from student ";
            pps = connection.prepareStatement(sql);
            //执行sql
            resultSet = pps.executeQuery();
             */

			List<Student> students = new ArrayList<>();//创建学生集合
            String sql = "select * from student ";
            List list = new ArrayList();
            query(sql,null);//查询
            //取值
            while(rs.next()){
                Student student = new Student();
                student.setStuId(rs.getInt("stuid"));
                student.setStuName(rs.getString("stuname"));
                student.setTeacherid(rs.getInt("teacherid"));
                students.add(student);
            }
            return students;
        }catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll();//关闭资源
        }
        return null;
    }

编写测试类Test

public class Test{
    public static void main(String[] args) {
        DaoImplements dao = new DaoImplements();
        List<Student> allStudent2 =dao.getAllStudent2();
        for (Student student : allStudent) {
            System.out.println(student.getStuId()+","+student.getStuName()+","+student.getTeacherid());
        }

    }//end main
}//end class

运行输出结果:

1,张三,3
2,李四,1
3,王五,3
4,赵六,1
5,花花,1
6,潇潇,2

反射处理:

   @Override
    public List<Student> getAllStudent(Class cla) {
        /*
        Connection connection = null;
        PreparedStatement pps = null;
         */

        try {
            /*
            //1加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2获得链接
            String userName = "root";
            String passWord = "123456";
            String url = "jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT";
            connection = DriverManager.getConnection(url, userName, passWord);
            //3定义sql,创建预状态通道(进行sql语句的发送)
            String sql = "select * from student ";
            pps = connection.prepareStatement(sql);
            //执行sql
            resultSet = pps.executeQuery();
            List students = new ArrayList();//创建学生集合
            */

			List students = new ArrayList();//创建学生集合
            String sql = "select * from student ";
            List list = new ArrayList();
            list.add(cla);
            query(sql,list);
            //通过反射,让结果集自动匹配set方法

            //得到当前数据库查询结果的列信息
            ResultSetMetaData metaData = rs.getMetaData();//存储结果集信息
            int columnCount = metaData.getColumnCount();//得到列数
            String[] columnNames = new String[columnCount];//数组长度为列的数量
            //获取列名
            for(int i = 0;i < columnCount;i++){
                columnNames[i] = metaData.getColumnName(i+1);//列从1开始
                System.out.println("columnName = "+columnNames[i]);
            }
            //获取类中所有的方法
            Method[] declaredMethods = cla.getDeclaredMethods();
            //取值
            while(rs.next()){
                try {
                    Object stu = cla.newInstance();//得到一个学生对象
                    //从对应列取出值,赋给对应的属性
                    for (String columnName : columnNames) {//遍历列名
                        String methodName = "set" + columnName;//装饰,set某一属性
                        for (Method declaredMethod : declaredMethods) {//遍历所有方法
                            //忽略大小写,通过反射获得的方法名,如果相等,则该方法为对此属性进行赋值的方法
                            if(declaredMethod.getName().equalsIgnoreCase(methodName)){
                                //不确定类为什么类型,因此为object
                                //给某一对象赋值,值为某一列中取出的值
                                declaredMethod.invoke(stu,rs.getObject(columnName));
                                break;
                            }
                        }
                    }
                    students.add(stu);
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }//end while
            return students;
        }catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll();//关闭资源
        }
        return null;
    }

编写测试类Test

public class Test{
    public static void main(String[] args) {
        DaoImplements dao = new DaoImplements();
        List<Student> allStudent =dao.getAllStudent(Student.class);
        for (Student student : allStudent) {
            System.out.println(student.getStuId()+","+student.getStuName()+","+student.getTeacherid());
        }

    }//end main
}//end class

运行输出结果:

columnName = stuid
columnName = stuname
columnName = teacherid
1,张三,3
2,李四,1
3,王五,3
4,赵六,1
5,花花,1
6,潇潇,2

由上述例子可见,重复代码在DBUtil类中以方法形式进行封装有效简化了代码,使得程序更简洁更具可读性,在后续的JDBC编程中建议使用


封装优化(properties配置文件)

使用ResourceBundle访问本地资源

在设计时往往需要访问一些适合本地修改的配置信息,如果作为静态变量,那么每次修改都需要重新编译一个class
↓↓↓↓↓↓
这时我们需要通过ResourceBundle,访问位于/WEB-INF/classes目录下后缀名为properties的文本类型文件,从里面读取我们需要的值

properties文件(属性文件)存储数据库信息的格式:key = value

优化的部分:

  • 加载驱动
    静态代码块在类加载到内存中时便执行
	static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
  • 定义用户名密码及url来获得链接
	//登录的用户名和密码
    private String username = "root";
    private String password = "123456";

    //url地址
    private String url = "jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT";

    protected Connection getConnection() {
        try {
            connection = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

优化

方法一

新建属性文件db.properties,并将username、password 、url 、driver这些包含配置信息的字段写入文件中,并赋值

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
username=root
password=123456

在静态代码块中编写读取属性文件的代码

	//用户名和密码、url地址
    private static String username;
    private static String password;
    private static String url;

    private static String driverName;

    //加载驱动
    static {
        try {
            InputStream inputStream = DBUtils.class.getClassLoader().getResourceAsStream("db.properties");
            Properties properties = new Properties();
            properties.load(inputStream);
            driverName = properties.getProperty("driver");//“”为文件中的字段名
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
            
            Class.forName(driverName);
        } catch (ClassNotFoundException| IOException e) {
            e.printStackTrace();
        }
    }

方法二

在方法一的基础上进行修改

方法一中读取属性文件需要写文件的后缀名,此方法参数只写属性文件名,不需要写后缀

	//用户名和密码、url地址
    private static String username;
    private static String password;
    private static String url;

    private static String driverName;

 	//加载驱动
    static {
        try {
            /*
            优化方法一:
            InputStream inputStream = DBUtils.class.getClassLoader().getResourceAsStream("db.properties");
            Properties properties = new Properties();
            properties.load(inputStream);
            driverName = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
             */

			//优化方法二:
			//参数只写属性文件名,不需要写后缀
            ResourceBundle bundle = ResourceBundle.getBundle("db");
            driverName = bundle.getString("driver");
            url = bundle.getString("url");
            username = bundle.getString("username");
            password = bundle.getString("password");

            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
目录 前言 1. 翻译说明 2. 版权声明 1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 1.3.4. 值类型的集合 1.3.5. 双向关联 1.3.6. 使双向连起来 1.4. 第三部分 - EventManager web应用程序 1.4.1. 编写基本的servlet 1.4.2. 处理与渲染 1.4.3. 部署与测试 1.5. 总结 2. 体系结构(Architecture) 2.1. 概况(Overview) 2.2. 实例状态 2.3. JMX整合 2.4. 对JCA的支持 2.5. 上下文相关的(Contextual)Session 3. 配置 3.1. 可编程的配置方式 3.2. 获得SessionFactory 3.3. JDBC连接 3.4. 可选的配置属性 3.4.1. SQL方言 3.4.2. 外连接抓取(Outer Join Fetching) 3.4.3. 二进制流 (Binary Streams) 3.4.4. 二级缓存与查询缓存 3.4.5. 查询语言的替换 3.4.6. Hibernate的统计(statistics)机制 3.5. 日志 3.6. 实现NamingStrategy 3.7. XML配置文件 3.8. J2EE应用程序服务器的集成 3.8.1. 事务策略配置 3.8.2. JNDI绑定的SessionFactory 3.8.3. 在JTA环境下使用Current Session context (当前session上下文)管理 3.8.4. JMX部署 4. 持久化类(Persistent Classes) 4.1. 一个简单的POJO例子 4.1.1. 实现一个默认的(即无参数的)构造方法(constructor) 4.1.2. 提供一个标识属性(identifier property)(可选) 4.1.3. 使用非final的类 (可选) 4.1.4. 为持久化字段声明访问器(accessors)和是否可变的标志(mutators)(可选) 4.2. 实现继承(Inheritance) 4.3. 实现equals()和hashCode() 4.4. 动态模型(Dynamic models) 4.5. 元组片断映射(Tuplizers) 5. 对象/关系数据库映射基础(Basic O/R Mapping) 5.1. 映射定义(Mapping declaration) 5.1.1. Doctype 5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对一(many-to-one) 5.1.11. 一对一 5.1.12. 自然ID(natural-id) 5.1.13. 组件(component), 动态组件(dynamic-component) 5.1.14. properties 5.1.15. 子类(subclass) 5.1.16. 连接的子类(joined-subclass) 5.1.17. 联合子类(union-subclass) 5.1.18. 连接(join) 5.1.19. 键(key) 5.1.20. 字段和规则元素(column and formula elements) 5.1.21. 引用(import) 5.1.22. any 5.2. Hibernate 的类型 5.2.1. 实体(Entities)和值(values) 5.2.2. 基本值类型 5.2.3. 自定义值类型 5.3. 多次映射同一个类 5.4. SQL引号包围的标识符 5.5. 其他元数据(Metadata) 5.5.1. 使用 XDoclet 标记 5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generat
符合Java习惯的关系数据库持久化 前言 1. 翻译说明 2. 版权声明 1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 1.3.4. 值类型的集合 1.3.5. 双向关联 1.3.6. 使双向连起来 1.4. 第三部分 - EventManager web应用程序 1.4.1. 编写基本的servlet 1.4.2. 处理与渲染 1.4.3. 部署与测试 1.5. 总结 2. 体系结构(Architecture) 2.1. 概况(Overview) 2.2. 实例状态 2.3. JMX整合 2.4. 对JCA的支持 2.5. 上下文相关的(Contextual)Session 3. 配置 3.1. 可编程的配置方式 3.2. 获得SessionFactory 3.3. JDBC连接 3.4. 可选的配置属性 3.4.1. SQL方言 3.4.2. 外连接抓取(Outer Join Fetching) 3.4.3. 二进制流 (Binary Streams) 3.4.4. 二级缓存与查询缓存 3.4.5. 查询语言的替换 3.4.6. Hibernate的统计(statistics)机制 3.5. 日志 3.6. 实现NamingStrategy 3.7. XML配置文件 3.8. J2EE应用程序服务器的集成 3.8.1. 事务策略配置 3.8.2. JNDI绑定的SessionFactory 3.8.3. 在JTA环境下使用Current Session context (当前session上下文)管理 3.8.4. JMX部署 4. 持久化类(Persistent Classes) 4.1. 一个简单的POJO例子 4.1.1. 实现一个默认的(即无参数的)构造方法(constructor) 4.1.2. 提供一个标识属性(identifier property)(可选) 4.1.3. 使用非final的类 (可选) 4.1.4. 为持久化字段声明访问器(accessors)和是否可变的标志(mutators)(可选) 4.2. 实现继承(Inheritance) 4.3. 实现equals()和hashCode() 4.4. 动态模型(Dynamic models) 4.5. 元组片断映射(Tuplizers) 5. 对象/关系数据库映射基础(Basic O/R Mapping) 5.1. 映射定义(Mapping declaration) 5.1.1. Doctype 5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对一(many-to-one) 5.1.11. 一对一 5.1.12. 自然ID(natural-id) 5.1.13. 组件(component), 动态组件(dynamic-component) 5.1.14. properties 5.1.15. 子类(subclass) 5.1.16. 连接的子类(joined-subclass) 5.1.17. 联合子类(union-subclass) 5.1.18. 连接(join) 5.1.19. 键(key) 5.1.20. 字段和规则元素(column and formula elements) 5.1.21. 引用(import) 5.1.22. any 5.2. Hibernate 的类型 5.2.1. 实体(Entities)和值(values) 5.2.2. 基本值类型 5.2.3. 自定义值类型 5.3. 多次映射同一个类 5.4. SQL引号包围的标识符 5.5. 其他元数据(Metadata) 5.5.1. 使用 XDoclet 标记 5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合元素(Collection elements) 6.2.3. 索引集合类(Indexed collections) 6.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) 6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary associations) 6.3.5. 使用<idbag> 6.4. 集合例子(Collection example) 7. 关联关系映射 7.1. 介绍 7.2. 单向关联(Unidirectional associations) 7.2.1. 多对一(many to one) 7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. 双向关联(Bidirectional associations) 7.4.1. 一对多(one to many) / 多对一(many to one) 7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合出现的依赖对象 (Collections of dependent objects) 8.3. 组件作为Map的索引(Components as Map indices ) 8.4. 组件作为联合标识符(Components as composite identifiers) 8.5. 动态组件 (Dynamic components) 9. 继承映射(Inheritance Mappings) 9.1. 三种策略 9.1.1. 每个类分层结构一张表(Table per class hierarchy) 9.1.2. 每个子类一张表(Table per subclass) 9.1.3. 每个子类一张表(Table per subclass),使用辨别标志(Discriminator) 9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承映射混合使用 9.2. 限制 10. 与对象共事 10.1. Hibernate对象状态(object states) 10.2. 使对象持久化 10.3. 装载对象 10.4. 查询 10.4.1. 执行查询 10.4.2. 过滤集合 10.4.3. 条件查询(Criteria queries) 10.4.4. 使用原生SQL的查询 10.5. 修改持久对象 10.6. 修改脱管(Detached)对象 10.7. 自动状态检测 10.8. 删除持久对象 10.9. 在两个不同数据库间复制对象 10.10. Session刷出(flush) 10.11. 传播性持久化(transitive persistence) 10.12. 使用元数据 11. 事务和并发 11.1. Session和事务范围(transaction scope) 11.1.1. 操作单元(Unit of work) 11.1.2. 长对话 11.1.3. 关注对象标识(Considering object identity) 11.1.4. 常见问题 11.2. 数据库事务声明 11.2.1. 非托管环境 11.2.2. 使用JTA 11.2.3. 异常处理 11.2.4. 事务超时 11.3. 乐观并发控制(Optimistic concurrency control) 11.3.1. 应用程序级别的版本检查(Application version checking) 11.3.2. 扩展周期的session和自动版本化 11.3.3. 脱管对象(deatched object)和自动版本化 11.3.4. 定制自动版本化行为 11.4. 悲观锁定(Pessimistic Locking) 11.5. 连接释放模式(Connection Release Modes) 12. 拦截器与事件(Interceptors and events) 12.1. 拦截器(Interceptors) 12.2. 事件系统(Event system) 12.3. Hibernate的声明式安全机制 13. 批量处理(Batch processing) 13.1. 批量插入(Batch inserts) 13.2. 批量更新(Batch updates) 13.3. StatelessSession (无状态session)接口 13.4. DML(数据操作语言)风格的操作(DML-style operations) 14. HQL: Hibernate查询语言 14.1. 大小写敏感性问题 14.2. from子句 14.3. 关联(Association)与连接(Join) 14.4. join 语法的形式 14.5. select子句 14.6. 聚集函数 14.7. 多态查询 14.8. where子句 14.9. 表达式 14.10. order by子句 14.11. group by子句 14.12. 子查询 14.13. HQL示例 14.14. 批量的UPDATE和DELETE 14.15. 小技巧 & 小窍门 15. 条件查询(Criteria Queries) 15.1. 创建一个Criteria 实例 15.2. 限制结果集内容 15.3. 结果集排序 15.4. 关联 15.5. 动态关联抓取 15.6. 查询示例 15.7. 投影(Projections)、聚合(aggregation)和分组(grouping) 15.8. 离线(detached)查询和子查询 15.9. 根据自然标识查询(Queries by natural identifier) 16. Native SQL查询 16.1. 使用SQLQuery 16.1.1. 标量查询(Scalar queries) 16.1.2. 实体查询(Entity queries) 16.1.3. 处理关联和集合类(Handling associations and collections) 16.1.4. 返回多个实体(Returning multiple entities) 16.1.5. 返回非受管实体(Returning non-managed entities) 16.1.6. 处理继承(Handling inheritance) 16.1.7. 参数(Parameters) 16.2. 命名SQL查询 16.2.1. 使用return-property来明确地指定字段/别名 16.2.2. 使用存储过程来查询 16.3. 定制SQL用来create,update和delete 16.4. 定制装载SQL 17. 过滤数据 17.1. Hibernate 过滤器(filters) 18. XML映射 18.1. 用XML数据进行工作 18.1.1. 指定同时映射XML和类 18.1.2. 只定义XML映射 18.2. XML映射元数据 18.3. 操作XML数据 19. 提升性能 19.1. 抓取策略(Fetching strategies) 19.1.1. 操作延迟加载的关联 19.1.2. 调整抓取策略(Tuning fetch strategies) 19.1.3. 单端关联代理(Single-ended association proxies) 19.1.4. 实例化集合和代理(Initializing collections and proxies) 19.1.5. 使用批量抓取(Using batch fetching) 19.1.6. 使用子查询抓取(Using subselect fetching) 19.1.7. 使用延迟属性抓取(Using lazy property fetching) 19.2. 二级缓存(The Second Level Cache) 19.2.1. 缓存映射(Cache mappings) 19.2.2. 策略:只读缓存(Strategy: read only) 19.2.3. 策略:读/写缓存(Strategy: read/write) 19.2.4. 策略:非严格读/写缓存(Strategy: nonstrict read/write) 19.2.5. 策略:事务缓存(transactional) 19.3. 管理缓存(Managing the caches) 19.4. 查询缓存(The Query Cache) 19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2. Lists, maps 和sets用于更新效率最高 19.5.3. Bag和list是反向集合类效率最高的 19.5.4. 一次性删除(One shot delete) 19.6. 监测性能(Monitoring performance) 19.6.1. 监测SessionFactory 19.6.2. 数据记录(Metrics) 20. 工具箱指南 20.1. Schema自动生成(Automatic schema generation) 20.1.1. 对schema定制化(Customizing the schema) 20.1.2. 运行该工具 20.1.3. 属性(Properties) 20.1.4. 使用Ant(Using Ant) 20.1.5. 对schema的增量更新(Incremental schema updates) 20.1.6. 用Ant来增量更新schema(Using Ant for incremental schema updates) 20.1.7. Schema 校验 20.1.8. 使用Ant进行schema校验 21. 示例:父子关系(Parent Child Relationships) 21.1. 关于collections需要注意的一点 21.2. 双向的一对多关系(Bidirectional one-to-many) 21.3. 级联生命周期(Cascading lifecycle) 21.4. 级联与未保存值(Cascades and unsaved-value) 21.5. 结论 22. 示例:Weblog 应用程序 22.1. 持久化类 22.2. Hibernate 映射 22.3. Hibernate 代码 23. 示例:复杂映射实例 23.1. Employer(雇主)/Employee(雇员) 23.2. Author(作家)/Work(作品) 23.3. Customer(客户)/Order(订单)/Product(产品) 23.4. 杂例 23.4.1. "Typed" one-to-one association 23.4.2. Composite key example 23.4.3. 共有组合键属性的多对多(Many-to-many with shared composite key attribute) 23.4.4. Content based discrimination 23.4.5. Associations on alternate keys 24. 最佳实践(Best Practices) 等等。。。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Selcouther

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值