The next day, I studied SSM framework and searched a lot of content on the Internet. On Zhihu, everyone said THAT SSH had been abandoned, so my goal today is to finish the SSM introduction online course and read a few more projects to accumulate experience and make the concept clear before writing code. It should be much more convenient.
SSM framework series one MyBatis MyBatis MyBatis is an open source project of Apache called iBatis. In 2010, this project was migrated to Google Code by Apache Software Foundation and renamed as MyBatis. Migrated to Github in November 2013. MyBatis is an excellent persistence layer framework that supports customized SQL, stored procedures, and advanced mapping. MyBatis avoids almost all of the JDBC code and manual setting of parameters and fetching result sets. MyBatis can Map interfaces and Java’s POJOs(Plain Old Java Objects) to records in the database using simple XML or annotations for configuration and native maps. All in all, MyBatis is a lightweight framework to simplify database operations.
Why use MyBatis? In order to solve JDBC problems and simplify database operation, MyBatis provides a better solution; For example: 1. You can configure the connection pool in the master configuration file to resolve the performance impact caused by frequently creating or releasing database connections. 2, dynamic SQL solve JDBC hardcoding problems: a) Where conditions change; B) Change of placeholder position; 3, through the packaging class can easily obtain the database query result set object; 4. Make the separation of the Dao layer business logic and database access easier to maintain and test.
What can I learn from this course? 1. Understand the architecture of MyBatis; 2. Master MyBatis framework construction and configuration; 3. Use MyBatis to add, delete, change and check the database. 4. Master Mapper agent development; 5. Master input and output mapping; 6. Master multi-table associated query; 7. Master dynamic SQL to write SQL statements; 8. Use MyBatis Generator to quickly generate Bean, Interface and mapper.xml; 9. Master MyBatis+Spring development (some Spring knowledge is required);
Where do I get MyBatis? MyBatis has been migrated to Github. Please visit the following website to get various versions of MyBatis github.com/mybatis/myb…
Use Jdbc to develop small demo, observe Jdbc problems;
MyBatis framework
Insert a picture description here
Part.1 HelloMyBatis: Build the HelloWorld project of MyBatis 2. Create test cases, test database, test Bean objects; 3. Create sqlmapconfig. XML master configuration file. A) Import the main configuration file header:
4. Create mapper. XML mapping file. A) Import Mapper header:
5, import constraints;
Part.2 Use MyBatis to add, delete, change, check the table operation; 3. Add user 4. Modify user 5. Delete user according to ID 6. Summary 1: Differences between Jdbc and MyBatis development (advantages of MyBatis), review the development process of MyBatis;
Part.3 MyBatis Mapper dynamic proxy development 4+1 (4 principles +1 attention) : 1, the name of the interface method needs to be consistent with the ID of the SQL statement to be called by Mapper. 2. The parameterType of the interface must be consistent with that of mapper. XML parameterType. 3. The interface return value must be consistent with mapper. XML resultType. 4. The namespace in mapper. XML must be the same as the package name of the interface. 5, note that mapper dynamic proxy development, according to the return value type to automatically select;
MyBatis main configuration file sqlmapconfig.xml 1, Properties 2, Settings 3, typeAliases 4, typeHandlers 5, objectFactory 6, Plugins
7, Environments A) environment B) transactionManager C) dataSource
8. Mappers (configure mapper position)
ParameterType MyBatis input and output mapping A) Basic types; B) Custom objects; C) Custom packaging class;
Output mapping resultType, resultMap;
A) resultType: I. Basic type; Ii. User-defined objects; Iii. The collection;
B) the resultMap; I. Bean object field does not match data table field; Ii. Custom packaging class; Iii. Associated query;
Part.6 MyBatis associated query 1, one to one; 2. One to many;
Insert a picture description here
Part.7 MyBatis dynamic SQL: more convenient splicing SQL statements
1. If tag-multi-condition query, obtain the user list; 2, Where tag - solve if tag concatenation string AND symbol problem; 3, trim TAB - custom where TAB rule 4, set TAB - solve the problem of string concatenation comma ", "when updating data table; 5. Foreach tag - If you want to use IN to query the same data, you can use foreach to traverse. 6, SQL tag - can extract repeated SQL statement fragments;
- 1
- 2
- 3
- 4
- 5
- 6
Part.8 MyBatis Generator (MBG) : Function: Automatically generate Bean objects, Java interfaces and SQLMapper.xml configuration files from database tables; The official documentation: www.mybatis.org/generator/ download address: github.com/mybatis/gen…
1. Set up MBG project; A) Download MBG core package; B) Create a Java project; C) Obtain configuration tables and instance codes from official documents; D) Importing dependency packages; 2. MBG configuration and generating required files (Bean, Interface, mapper.xml) according to database tables; 3. Operate database with automatically generated files;
Part.9 MyBatis + Spring integration development (if you have not learned Spring, this Part of the content, etc.) Purpose: a) use Spring container with singleton mode management MyBatis sqlSessionFactory; B) Use Spring to manage connection pools, data sources, etc.; C) Inject Dao/Mapper dynamic proxy objects into The Spring container and directly obtain them when used;
1. Integration of Mybatis and Spring framework; A) Import the required packages; B) Create Mybatis master configuration file sqlmapconfig.xml; C) Create the main Spring configuration file applicationContext.xml.
<? The XML version = "1.0" encoding = "utf-8"? >
- 1
D) Configuring C3P0 connection pool;
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> </bean>
- 1
- 2
- 3
- 4
- 5
- 6
E) Read db.properties;
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_mybatis jdbc.user=root jdbc.password=123
- 1
- 2
- 3
- 4
F) Configure sqlSessionFactory; G) Test:…
2. Dao development; 3. Mapper dynamic proxy development; 4. Mapper dynamic scanning development;