1. What is MyBatis

MyBatis is provided by Apache for persistence layer open source framework, JDBC access to the database process is simplified and encapsulated using MyBatis can only focus on the SQL statement itself, and do not need to pay attention to (JDBC) registration driver, access to the connection, access to the transport, release resources and other processes. MyBatis can configure the SQL statement to be executed by using XML files or annotations. During execution, the parameter values carried in the Java object are mapped to the SQL skeleton to generate the final SQL statement to be executed, and the executed results are processed and returned.

2. Some advantages of MyBatis

1) JDBC connection access database has a lot of duplicate code, and MyBatis can greatly simplify the JDBC code registration driver, access to the connection, access to the transport, release resources 2) JDBC does not have its own connection pool, 3) In JDBC, SQL statements and connection parameters are written in the program, while MyBatis writes SQL statements and connection parameters in the configuration file. 4) We need to manually process the ResultSet obtained by JDBC after executing the query, while the results obtained by MyBatis after executing the query will be processed, and the processed results will be returned.

2. Quick Start of MyBatis 1.

Perform/unit08 mybatis/SQL scripts. - all the SQL statements in the TXT file, create a yonghedb library, and create the emp table, to emp insert number of records in the table.

Create the project, import the JAR package, and provide the test class

2.1. Create Maven Java Project: CGB-MyBatis 01 2.2. Import dependencies in the POM file of the project: JUnit, MySQL driver, MyBatis, Log4J, etc. 2.3. Create com. Tedu. MybatisTest01 test class -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- < dependencies > <! Mybatis </groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version>  </dependency> <! > <dependency> < grouppid >mysql</ grouppid > <artifactId>mysql-connector-java</artifactId> The < version > 8.0.11 < / version > < / dependency > <! > <dependency> <groupId> jUnit </groupId> <artifactId> jUnit </artifactId> <version>4.10</version> </dependency> <! > <dependency> < grouppid >org.slf4j</ grouppid > <artifactId>slf4j-log4j12</artifactId> < version > 1.6.4 < / version > < / dependency > < / dependencies > -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -

3. Provide configuration files (mybatis-config.xml, empmap.xml)

Mybatis -config file is mainly configured with transaction management mode, parameters to connect to the database, and import Mapper file 3.2. The EmpMapper file mainly configures the SQL statements to be executed in the future (query, add, delete, modify)

Provide an EMP entity class

If you want to query all the employee information, the employee information needs to be encapsulated in the Java object because here we need to provide the EMP (employee) class, this class is used to encapsulate all the employee information com.TEDU.POJO.EMP We provide four variables (id, name, job, salary) to encapsulate the four columns of id, name, job, salary in the EMP table. An additional four variables (id, name, job, salary) corresponding to the Get and Set methods -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 1) what is a POJO? A plain old/ordinary Java object is a plain old/ordinary Java object that encapsulates data. To encapsulate employee information, we can provide an EMP class to encapsulate employee information 2) ID and Salary why not use the basic data type instead of the wrapper data type? Basic data types have default values. For example, a variable of type int has a default value of 0, If a wrapper type is used, the default value is null and the default value is 0, which can sometimes affect our decision (for example, if a variable of type int is 0, we cannot tell whether 0 is assigned or the default value) 3) Why do variable names in an EMP class have to be the same as column names in an EMP table? When the framework encapsulates the results of the query into an EMP object, it generates corresponding set methods (setId, setName, setJob, setSalary) from the column names (ID, Name, Job, Salary) in the EMP table. If there is no set method, the variable in the EMP class (ID, Name, Job, Salary) is found by the column name in the EMP table, and the result of the query is encapsulated into the EMP object by the violent reflection. ----------------------------------------------------

Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J Log4J

Since MyBatis has integrated log4j by default, you only need to complete the following two steps to use log4j: 2) Add the log4j configuration file (filename must be log4j.properties, the file must be in the class directory) because the bottom of log4j is to look for the file named log4j.properites in the class directory

1, #{} placeholder: This is the JDBC question mark (?). #{} placeholders are translated as question marks (?) in MyBatis. A placeholder

If there is only one #{} placeholder in the SQL statement, the {} name is not required, but cannot be empty; Parameters can be passed directly without encapsulation. If there is more than one #{} placeholder in the SQL statement, the parameter values need to be encapsulated by a Map or POJO object. If you use the Map collection to encapsulate SQL parameter values, the #{} placeholder name should match the key in the Map!! Because at the bottom of MyBatis, the name in the #{} placeholder is used as the key to get the corresponding value in the map; If a POJO object is used to encapsulate SQL parameter values, the name in the #{} placeholder should have a corresponding getXXX method in the POJO object, or a corresponding variable such as #{job} placeholder name is job, which means that there should be a getJob() method or a job variable in the EMP. If both are present, the getXXX method is used first to get the value of the property stored in the POJO object. If no getXXX method is used, the value of the JOB variable in the EMP is obtained directly through brute force reflection. Summary: In MyBatis framework, most situations use #{} placeholder,#{} is the JDBC question mark (?). A placeholder for a parameter value in an SQL statement. Select * from emp where salary = salary select * from emp where salary = salary select * from emp where salary = salary UPDATE EMP SET COLUMN = PARAMETER, COLUMN = PARAMETER,.. WHERE column = Parameter value... DELETE FROM EMP WHERE DELETE FROM EMP WHERE DELETE FROM EMP WHERE DELETE FROM EMP WHERE...

${}; ${};

select * from emp where id>5; ${} placeholder is a placeholder for a segment of an SQL statement. When the parameter is passed to you, it is directly concatenated at the same location as the ${} placeholder. If there is only one #{} placeholder in the SQL statement, the argument can be passed without wrapping it! However, if there is only one ${} placeholder in the SQL statement, the parameter must be wrapped in a Map or POJO object and then passed to it! PreparedStatement object is used to transfer SQL statement when executing SQL statement in MyBatis bottom layer! SELECT * FROM emp WHERE name=? SELECT * FROM emp WHERE name=? SELECT * FROM emp WHERE name=? 2) Then pass the SQL parameters to the server (if the parameters contain keywords or SQL special strings, it will not affect the skeleton, but will be treated as normal text!)

IV. Mapper interface development

Mapper interface development should meet the following four rules: 2) The SQL to be executed in the Mapper file must have corresponding interface methods in the interface, and the ID value of the SQL tag must be equal to the method name If you are querying SQL, the type specified in the ResultType attribute should be the same as the return value type of the interface method (If the interface method returns a List collection, the ResultType property only needs to specify the generics in the collection.) 4) The parameter types on the SQL tag (which can be omitted) should be the same as the parameter types of the interface method /* 1. * The underlying framework can provide the implementation class of the EmpMapper interface based on the bytecode object of the EmpMapper interface received by the getMapper method. */ EmpMapper = session.getMapper(EmpMapper.class) */ EmpMapper = session.getMapper(EmpMapper.class); /* sql_name =namespace; /* sql_name =namespace; /* sql_name =namespace; After the execution of the SQL statement, the results of encapsulation processing, will be processed the results returned! */ List<Emp> list = mapper.findAll01();

1. The difference between XML and annotation development

2) Advantages: The content of XML configuration can be modified at any time, and there is no need to recompile and republish the content after modification. Or write the configuration in a Java class, and if it changes in the future, you'll need to modify the Java source file, then you'll need to recompile and redistribute it. 2) Advantages: It is much simpler to configure than XML.

6, MVC design pattern 1, pattern 1: only JSP

Servlet: Process requests received by the server. Servlet is not suitable for output of a full HTML page. HTML: This is a technique for developing web pages and can be output as web pages. It is also the development technology of web pages (Servlet in essence), which can output web pages, and can also display dynamic data through Java code. The emergence of JSP not only solves the problem that Servlet is not suitable for output web pages, but also solves the problem that HTML can not display dynamic data JSP can do what servlets do, or it can be used as HTML to develop Web pages. Many people start developing projects with just JSPs 1) for fetching the parameters (if any) carried in the request 2) for processing the request 3) for connecting and accessing the data (JDBC code) and 4) for displaying the results of the request processing But if only use JSP development projects,JSP is bound to write a lot of Java code,JSP code will become very chaotic, difficult to maintain later, and the code can not be reused!

2. Pattern 2 :Servlet+JavaBean+JSP (in line with MVC design pattern)

Servlet(Controller: Controller): 1) is responsible for receiving parameters in the request (if any) 2) is responsible for calling methods in JavaBeans to process the request 3) is responsible for calling JSP, which is responsible for displaying the result of the request processing JavaBeans: 1) Enclosure of data (POJO) 2) Process business logic 3) Access database JSP(View: View): Only responsible for displaying the results of the request processing JavaBeans: Is a plain Java class that provides private properties that encapsulate data, or business methods that handle business logic, and access databases. Entity bean (POJO) : Java classes designed specifically to encapsulate data business bean: designed to handle the business logic of the Java class Emp: (id, name, job, salary, get/set, the.findall ())