Mybatis framework----->(1) What is Mybatis framework and its main solution to the defects of jdbc programming

One, three-tier architecture

1. Responsibilities corresponding to the three-tier structure:
  1. Interface layer (view layer): The main function is to receive user request data and display the processing result
  2. Business logic layer: Receive data from the page, calculate business logic, and call the data access layer to get data.
  3. Data access layer: When dealing with the database, it mainly realizes the addition, deletion, modification and checking of data. Submit the data stored in the database to the business layer, and save the data processed by the business layer to the database.
2. The package corresponding to the three layers:
  • Interface layer: controller package (servlet)
  • Business logic layer: service package (XXXService class)
  • Data access layer: dao package (XXXDao class)
3. The processing request and interaction relationship between the three layers:

User<——>Interface layer<——>Business logic layer<——>Data access layer (persistence layer)<——>Database

4. The processing framework corresponding to the three layers:
  • Interface layer: servlet----springmvc framework
  • Business logic layer: serivce class-spring framework
  • Data access layer: dao class-mybatis framework

Second, what is the Mybatis framework

1. Mybatis framework
  • It is a persistence layer framework that encapsulates jdbc inside. Programmers only need to pay attention to the SQL statement itself, without having to deal with processes such as loading drivers, creating connections, creating statements, and closing connections.
  • Mybatis mainly configures the SQL statement to be executed through xml or annotation, and maps the dynamic parameters of the instance object and the SQL statement to generate the final executed SQL statement, and finally the SQL statement is executed by the Mybatis framework and the result is mapped to Java Object and return.
2, jdbc programming
public void findStudent(){
    
    
    Connection conn = null;
    Statement st = null;
    ResultSet rs = null;

    try {
    
    
        //注册MySQL驱动
        Class.forName("com.mysql.jdbc.Driver");
        //连接数据的基本信息
        String url = "jdbc:mysql://localhost:3306/test";
        String username="root";
        String password = "123456";
        //创建连接对象
        conn = DriverManager.getConnection(url, username, password);
        //保存查询结果
        List<Student> stuList = new ArrayList<>();
        //创建Statement,用来执行SQL语句
        st = conn.createStatement();
        //执行查询,创建记录集
        rs = st.executeQuery("select * from text ");
        while (rs.next()){
    
    
            Student stu = new Student();
            stu.setId(rs.getInt("id"));
            stu.setName(rs.getString("name"));
            //从数据库取出的数据转为Student对象,封装到List集合中
            stuList.add(stu);
        }
    } catch (Exception e) {
    
    
        e.printStackTrace();
    } finally {
    
    
        try {
    
    
            //关闭资源
            if (rs != null) ;{
    
    
                rs.close();
            }
            if (st != null) {
    
    
                st.close();
            }
            if (conn != null) {
    
    
                conn.close();
            }
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }
}
3. Defects of using jdbc programming
  • There are more codes and more repetitive codes, resulting in low development efficiency
  • Need to pay attention to the creation and destruction of Connection, Statement, ResultSet objects
  • To convert the result of the ResultSet query to an object, you need to encapsulate it as a List collection
  • Business code and operation database code mixed together
4. Main problems solved by Mybatis
  1. Register the database driver, for example:
Class.forName("com.mysql.jdbc.Driver");
  1. Provides the ability to create Connection, Statement, ResultSet, without the developer creating these objects
  2. The ability to get sql from xml file and execute sql, convert ResultSet result into Java object, List collection
List<Student> list = new ArrayLsit<>(); 
ResultSet rs = state.executeQuery(“select * from student”); 
while(rs.next){
    
     
	Student student = new Student(); 
	student.setName(rs.getString(“name”)); 
	student.setAge(rs.getInt(“age”)); 
	list.add(student); 
}
  1. Provides the ability to close resources without you having to close Connection, Statement, ResultSet
  2. Developer : Provide sql statement—>Mybatis process sql—>Developer get List collection or Java object (data in table)
5. Two major functions of Mybatis
  • (1) Sql mapper:sql mapping:

You can map a row of data in a database table to a Java object. Operating this object is equivalent to operating the data in the table

  • (2) Data Access Objects (DAOs): data access

Add, delete, modify, and check the database

Guess you like

Origin blog.csdn.net/hcz666/article/details/113059475