MyBatis first lesson

A, jdbc use

 

mybatis is encapsulation of jdbc, so how to review the operation of the database is jdbc to mysql Example.

 

First on a map, he recalled kill.

Again a piece of code, familiar and unfamiliar.

    @Test
    public void showUser(){
        //数据库连接
        Connection connection = null;
        //预编译的Statement,使用预编译的Statement提高数据库性能
        PreparedStatement preparedStatement = null;
        //结果 集
        ResultSet resultSet = null;

        try {
            //加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");

            //通过驱动管理类获取数据库链接
            connection =  DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis", "root", "");
            //定义sql语句 ?表示占位符
            String sql = "select * from user where username = ?";
            //获取预处理statement
            preparedStatement = connection.prepareStatement(sql);

            //设置参数,第一个参数为sql语句中参数的序号(从1开始),第二个参数为设置的参数值
            preparedStatement.setString(1, "王五");
            //向数据库发出sql执行查询,查询出结果集
            resultSet =  preparedStatement.executeQuery();

            preparedStatement.setString(1, "张三");
            resultSet =  preparedStatement.executeQuery();
            //遍历查询结果集
            while(resultSet.next()){
                System.out.println(resultSet.getString("id")+"  "+resultSet.getString("username"));
            }
            resultSet.close();
            preparedStatement.close();

            System.out.println("#############################");

        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            //释放资源 略去不写
        }
    }

Here to mention the difference between a knowledge point, PrepareStatement and Statement of.

PrepareStatement can open pre-compiled, the same sql first be compiled into a function, the function is cached, then will be called repeatedly.

Accordingly, you can ask yourself a few questions:

1. What is the pre-compiler?

2, pre-compiled by whom, PrepareStatement or database?

3, pre-compiled results of anyone save?

4, how to control the pre-compiler?

References: https://blog.csdn.net/Marvel__Dead/article/details/69486947

 

 

Two, jdbc problem

 

Through the above wave meetings, looks like mysql is not so complicated by the use of jdbc, mybatis is not superfluous?

Seemingly reasonable, but in reality not.

1, the code is acquired Connection duplicate code;

2, sql hard-coded in the code, if the length sql, sql more terrible code, maintainability. The long sql, sql and more seems to be a real project of standard, normal;

3, set code redundancy parameters;

4, code redundancy obtain results;

 

According to the research, there is no relative package, mybatis 50% of the amount of code can be reduced.

 

Three, mybatis what had been done for us?

 

1, a simplified sql, sql dynamic techniques, the programmer can focus on the core part of the logic sql, sql generating semi-automatic;

2, through the sql xml tag, any portion of the sql extracted are multiplexed;

3, the automatic acquisition parameters from pojo;

4, the package automatically as a result of the development of POJO;

 

Fourth, why mybatis?

 

mybatis and hibernate are persistence framework, hibernate orm framework is the full sense, mybatis semi-orm framework.

 

That is, using hibernate, only we need to build the table, all other hibernate seal the deal.

 

mybatis can not do so much, it is the third part of this talented say that few things.

 

Small projects using hibernate, higher development efficiency.

 

Large Internet project it?

 

Usually choose mybatis.

 

The reason is that, sql too important to optimize the sql basic literacy is a programmer .

 

But hibernate sql task to produce their own dry, optimizing sql hibernate become optimized, simple things get complicated.

 

 

V. handwriting Mybatis framework

 

 

 

 

 

 

 

 

 

 

 

Published 87 original articles · won praise 14 · views 80000 +

Guess you like

Origin blog.csdn.net/epitomizelu/article/details/104709039