JDBC: This configuration uses JDBC's commit and rollback settings
MANAGE: Let the container manage the entire life cycle of the entire transaction.
7. mapper element
Specify the location of the mybatis mapping file, and generally use the following four import methods.
1. Classpath resource attribute
2. Local file url attribute
3. Interface class class attribute
4. Package name attribute
[](() default alias of mybatis
Since aliases are case insensitive. Therefore, it is not recommended to use it, and it is prone to the coverage problem of repeated definitions.
Below (left: alias, right: mapped type)
_byte byte
_long long
_int int
_double double
[](() data source type
1.UNPOOLED
The connection is opened and closed for each request. It is a good choice for simple applications with no performance requirements.
2. PARTIES
This data source uses the possibility of "pooling" to organize JDBC connection objects, avoiding the initialization and authentication time required when creating new connection instances. It can make concurrent web applications respond to requests quickly. is the processing method of the current manifold.
3.JNDI
Can be used in containers such as EJB or application server. The container can centrally configure the data source externally, and then place a JNDI context reference.
[](()mapper mapping file main elements
[](()1.select element
[](()2.insert element
Other elements are similar to select, with the following elements added
[](()3.update element and delete element
Attribute configuration is basically the same as select.
[](()4.sql element
Scope of application: when many sql statements need to be defined in a mapping file. Some parts of these sql statements are the same (such as id, user, job and other fields). By extracting the same part and then referencing it to the corresponding element, it is bound to reduce the bloat of the code.
The include element is applicable to the sql element, and the include element can also be used for basic elements (select, insert, etc.). The attribute value of name=prefix in the property element can be obtained through ${prefix}.
as follows:
id,user
// Future shorthand. Do not write some attribute values
select
from customer
where id=#{id}
// Among them, the refid attribute in the include element refers to the custom code fragment. The refid attribute value is the id of the custom code fragment
[](()5.resultMap element
resultMap: Indicates the result mapping set. Used to define mapping rules, cascading updates, type converters.
[](() It has the following commonly used sub-elements:
1.constructor element:
used to configure the constructor
2. Association elements and collection elements:
It is used to deal with the association relationship when dealing with multiple tables. (Where association is generally used to deal with one-to-one relationship, while collection is used to deal with one-to-many or many-to-many relationship)
3. The discriminator element:
Handle situations where a single database query returns many different types of result sets.
4.id element: identifies which column is the primary key
5.result element: identifies the mapping relationship between java class attributes and table fields.
[](() Several attributes commonly used in resultMap
1.type: Identify the java class to be mapped
2.id: the unique identifier of the resultMap element
[](() association element
Several attributes commonly used in association:
1.property: One-to-one correspondence between entity class object properties and table fields
2.column: Specify the corresponding field in the table
3.javaType: Specify the attribute type mapped to the entity object
4. select: Specify the embedded sub-sql statement (for nested queries in association mapping)
5.fetchType: Specifies whether to enable lazy loading when linking. There are 2 attribute values: lazy (lazy loading), eager, the specific explanation is as follows:
[](() one-to-one
Because it's one-to-one here: so use the association element. It can be understood like this:
After querying the business table, assign the id of the order in the business to the column below. Finally, find out the corresponding order through the SQL statement mapped by the following select statement (of course, in real life, it is generally one-to-many, or many-to-many)
select* from business
where id=#{id}
[](() one-to-many
There are 2 ways: (recommended to use nested result query)
1. Through nested query (in simple terms, the foreign key from the query of the previous table is passed to another statement, and finally the query result is returned)
2. The other is nested result query
details as follows:
1. Nested query
For example, a user has multiple orders
Query steps:
Query the record corresponding to the user ( just use business as the left user ), and then assign the user's id to the column below. Then select the associated SQL to query all orders with user_id=user.id from the order table, and finally return the data. Note: The attribute that receives the order table data is a List collection (because there may be multiple pieces of data)
2. Nested result query
Note: the field names in the collection cannot be the same as those in the resultMap, otherwise the results of the following query will be wrong.
select b.*,o.id as order_id,o.number,o.username as order_username,o.order_name
from business b,order
o
where o.user_id=b.id
and b.id=#{id}
[](() many to many
Idea: Put the primary keys of the many-to-many two tables into an intermediate table. When a many-to-many query is to be performed, the intermediate table will be used to find the corresponding id, and then the id of another table will be found to find out the data. Just give an example:
Now there are 3 tables: order (order), order_user (intermediate table), user (table). I now need to find out how many orders the user with id=1 in the user table has.
solution:
Directly on the sql statement
select
from order,order_user,user
where
user.id=#{id} First get the id value to be queried Here # is not a comment! !
order_user.user_id=user.id #Query the data in the intermediate table whose user_id is equal to user.id, then you can get the id value of the order table
order.id=order_user.order_id #Query the data whose id in the order table is equal to the order_id in oder_user, and then you can query all the associated data
You can refer to the following code:
Note: the field names in the collection cannot be the same as those in the resultMap, otherwise the results of the following query will be wrong.
select b.* ,o.id as order_id,o.number,o.username oder_username,o.order_name
from order
o,business b,t_order_business t
where b.id=#{id}
and t.business_id=b.id
and o.id=t.order_id
[](()) spring integrates mybatis
!!! Recommended MapperScanerConfigurer integration method
Several related packages are required:
... (and some other packages)
org.mybatis
mybatis-spring
2.0.3
org.mybatis
my shoe
3.5.6
The main difference between spring integrated mybatis (marked as sm) and native mybatis (m) is:
1. sm is integrated in the form of beans (put the global configuration file of mybatis into it, in a word: it is all loaded into the bean configuration file). And m is mainly loaded through the global configuration file.
2. The m of the element needs to configure the data operation object SqlSession by itself, while the sm simplifies this step relatively. (Through bean, inject SqlSessionFactory into the implementation class of the interface, so as to obtain SqlSession, and then operate the database)
[](()) Spring integrates several ways of mybatis
In the following, we describe these integration methods in detail.
[](()1. Traditional Dao integration
important point:
1. The method name of the interface needs to be the same as the id of the sql statement in the mapper file.
2. The value of the namespace space in the mapper must be the full path of the interface.
3. Interface and implementation class are required (by injecting the SqlSession object into the implementation class of the interface to obtain the operation object)
<?xml version="1.0" encoding="UTF-8"?><beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xmlns:context=“http://www.springframework.org/schema/context”
xmlns:aop=“http://www.springframework.org/schema/aop”
xmlns:tx=“http://www.springframework.org/schema/tx”
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
Read extrinsic database connection properties
<context:property-placeholder location=“classpath:db.properties” />
Read extrinsic database connection properties
<tx:annotation-driven transaction-manager=“transactionManager”/>
[](()2.Mapper interface integration
----Mapper interface integration is divided into MapperFactoryBean integration and MapperScanerConfigurer integration
Among them, the integration of MapperScanerConfigurer is simpler. The integration of MapperFactoryBean is relatively strict (error-prone)