My second day of learning Java in Shanghai LeByte

The next day I learned the ssm framework and searched a lot of content on the Internet. In Zhihu, everyone said that ssh has been abandoned, so today’s goal is to finish reading the ssm introductory online course, and then watch a few more projects to accumulate experience After clarifying the concept, it should be much easier to write code. Here are the notes I made:


What is MyBatis, one of the SSM framework series ?
MyBatis was originally an open source project iBatis of apache. In 2010, this project was migrated from apache software foundation to google code and renamed 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 JDBC code and manual setting of parameters and obtaining result sets. MyBatis can use simple XML or annotations for configuration and native Maps, and map interfaces and Java POJOs (Plain Old Java Objects) to records in the database.
All in all, MyBatis is a lightweight framework that simplifies database operations.

Why use MyBatis?
In order to solve the problems of JDBC and simplify database operations, MyBatis provides excellent solutions;
for example:
1. The connection pool can be configured through the main configuration file to solve the performance impact caused by frequent creation and release of database connections;
2. Dynamic SQL solves JDBC Hard-coded issues:
a) Where conditions change;
b) Placeholder positions change;
3. The database query result set objects can be easily obtained through packaging classes;
4. The Dao layer business logic and database access separation are easier to maintain and test.

What can I learn from this course?
1. Understand the MyBatis architecture;
2. Master the construction and configuration of the MyBatis framework;
3. Use MyBatis to add, delete, modify, and check the database.
4. Master the Mapper agent development;
5. Master the input and output mapping;
6. Master the multi-table associated query;
7. Master the dynamic SQL to write SQL statements;
8. Use the MyBatis Generator tool to quickly generate Bean, Interface, and mapper.xml;
9. Master MyBatis+Spring development (requires some Spring knowledge);

Where do I get MyBatis?
MyBatis has been migrated to github, visit the following website to get various versions of MyBatis
https://github.com/mybatis/mybatis-3/releases

Use Jdbc to develop a small demo and observe the problems of Jdbc;

Start learning MyBatis framework
MyBatis architecture diagram:

Insert picture description here

Part.1 HelloMyBatis: Build MyBatis HelloWorld project
1. Download MyBatis, create project, guide package;
2. Create test case, test database, test Bean object;
3. Create sqlMapConfig.xml main configuration file;
a) Import main configuration file header :

<?xml version="1.0" encoding="UTF-8" ?>

4. Create the Mapper.xml mapping file;
a) Import the Mapper file header:

<?xml version="1.0" encoding="UTF-8" ?>

5. Import constraints;

Part.2 Use MyBatis to add, delete, modify, and check the table;
1. Query a user by ID
2. Find the matching user list by fuzzy user name
3. Finish adding user
4. Modify user
5. Delete according to id User
6. Use MyBatis to develop Dao layer and test;
7. Summary 1: The difference between Jdbc and MyBatis development (the advantages of MyBatis), review the MyBatis development process;

Part.3 MyBatis Mapper dynamic agent development 4+1 (4 principles+1 note):
1. The name of the interface method needs to be consistent with the id of the SQL statement to be called in mapper.xml;
2. The parameter type of the interface needs to be the same as the mapper. xml parameterType is the same;
3. The return value of the interface needs to be consistent with the mapper.xml resultType;
4. The namespace in mapper.xml should be consistent with the full package name of the interface;
5. Note that in the development of mapper dynamic proxy, it is automatically selected according to the return value type ;

Part.4 MyBatis main configuration file SqlMapConfig.xml description: (note the order)
1. properties (read configuration file)
2. settings (global configuration parameters)
3. typeAliases (type aliases)
4. typeHandlers (type handlers)
5. objectFactory (object factory)
6, plugins (plug-in)

7. environments (environment collection attribute object, Say Good Bye after integration with Spring framework)
a) environment (environment sub-attribute object)
b) transactionManager (transaction management)
c) dataSource (data source)

8. mappers (configure mapper location)

Part.5 MyBatis input and output mapping
1. Input mapping parameterType;
a) basic type;
b) custom object;
c) custom packaging class;

2. Output mapping resultType, resultMap;

a) resultType:
i. Basic type;
ii. Custom object;
iii. Collection;

b) resultMap;
i. The bean object field does not match the data table field;
ii. Custom packaging class;
iii. Association query;

Part.6 MyBatis related query
1. One-to-one;
2. One -to-many;

Insert picture description here

Part.7 MyBatis dynamic sql: more convenient splicing SQL statements

1、 if标签 - 多条件查询,获取用户列表; 2、 where标签 - 解决if标签拼接字符串AND符号问题; 3、 trim标签 - 定制where标签的规则 4、 set标签 - 解决更新数据表时字符串拼接逗号”,”问题; 5、 foreach标签 – 如果需要使用IN查询多条相同数据,可以使用foreach遍历; 6、 sql标签 – 可以提取重复sql语句片段;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Part.8 MyBatis Generator (MBG):
Function: Automatically generate Bean object, Java interface and SqlMapper.xml configuration file according to the database table;
official document: http://www.mybatis.org/generator/
download address: https:// github.com/mybatis/generator/releases

1. Build MBG project;
a) Download MBG core package;
b) Create java project;
c) Obtain configuration table and example code from official documents;
d) Import dependent package;
2. MBG configuration and generate required files according to database table ( Bean, Interface, Mapper.xml);
3. Use automatically generated files to manipulate the database;

Part.9 MyBatis + Spring integrated development (if you have not studied Spring, this part will wait until you finish learning Spring)
Purpose:
a) Use Spring container to manage Mybatis sqlSessionFactory in singleton mode;
b) Use Spring to manage connection pool , Data source, etc.;
c) Inject the Dao/Mapper dynamic proxy object into the Spring container and obtain it directly when using it;

1. Integration of Mybatis and Spring framework;
a) Import the required packages;
b) Create the main Mybatis configuration file sqlMapConfig.xml;
c) Create the main Spring configuration file applicationContext.xml (the following is the header of the spring framework xml, which requires import constraints) ;

<?xml version="1.0" encoding="UTF-8"?>

  • 1

d) Configure 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-style development;
3. Mapper dynamic agent development;
4. Mapper dynamic scanning development;

Guess you like

Origin blog.csdn.net/dirft_lez/article/details/109173786