Other concepts:
1. Interface programming
Primitive: Dao ------------------> DaoImpl
Mybatis Mapper -------------> xxMapper.xml
2. SqlSession represents a session with the database, it must be closed when it is used up
3. Both SqlSession and Connection are not thread-safe. You should get a new object every time you use it.
4. There is no implementation class for the Mapper interface, but mybatis will generate a proxy object for it. (Bind the interface with xml)
EmployeeMapper empMapper = sqlSession.getMapper(EmployeeMappper.getClass);
5. Two important configuration files
Mybatis global configuration file: database connection pool information, transaction manager information, system operating environment information, etc.
sql mapping file: save the mapping information of each sql statement: extract sql
Mybatis global configuration file
5. Mybatis can use the properties tag to import the content of the external properties configuration file
<properties resource="dbconfig.properties"></properties>
resource:引入类路径下的资源
url:引入网络路径下或者磁盘路径下的资源
6, settings configuration label for runtime behavior
setting:用来设置每一个设置项
name:设置项名
value:设置项取值
7. typeAliases tag: alias processor: we can alias our Java types (recommend batch aliasing)
<package name="com.xuan.mybatis.bean"/>
package:为某个包下所有的类批量起别名
name:指定包名(为当前包以及下面所有的子包的每一个类都起一个默认的别名(类型小写 ))
Note: In the case of batch aliasing, use the @Alisa annotation to specify a new alias for a certain type (to prevent two classes with the same name under the same package)
8. The environments tag: You can configure multiple environments for mybatis (development environment, test environment, etc.), default specifies the use of a certain environment
environment:配置一个具体的环境信息;必须包含两个标签(transactionManager、dataSource),id代表当前环境唯一标识
transactionManager:事务管理器
dataSource:数据源
9. The databaseIdProvider tag: The type attribute that supports multiple data vendors is to get the database vendor's identification (Mysql, Oracle, SQL server...)
<databaseIdProvider>
<!--为不数据库厂商起别名-->
<property name="Mysql" value="mysql"/>
<property name="Oracle" value="oracle"/>
<property name="SQL server" value="sqlserver"/>
</databaseIdProvider>
10, mappers tag: register the sql mapping to the global configuration
mapper:注册一个sql映射
resource属性:引用类路径下的sql映射文件
url属性:引用网络路径或磁盘路径下的sql映射文件
class属性:引用(注册)接口
1. There is a sql mapping file, the mapping file name must be the same name as the interface, and it must be placed in the same directory as the interface;
2. There is no sql mapping file, all sql is written on the interface with annotations;
Recommended:
a) The more important and complex Dao interface is written using sql mapping file
b) The unimportant and simple Dao interface can use annotations for rapid development