Mybatis Overviewfollowing the article, today, SE Tran Duy Truong of NAL is going to help readers to have a better understanding of how Mybatis works by showing how to configure Mybatis.

Mybatis Overviewfollowing the article, today, SE Tran Duy Truong of NAL is going to help readers to have a better understanding of how Mybatis works by showing how to configure Mybatis.

1. Configure MyBatis with the XML file and some essential tags when configuring the file.

- I create a file named mybatis-config.xml (image)

- You can see that the above code has a lot of tags. So what is the meaning of these tags ??

+ Tag : to configure the database connection information we use in the application. In MyBatis, you can connect to multiple databases using tags inside .

+ Tag : MyBatis supports two transaction managers that are JDBC and MANAGED

+ Tag : used to configure database connection properties, such as driver-name, URL, user-name, and password. has three types: UNPOOLED, POOLED, JNDI For example

Latest Products

+ Tag : File Mapper XML is an important file, it contains SQL queries. The mapper sub-tag is used to configure the location of the files XML mapper in the MyBatis configuration file (this tag contains four attributes: resources, URL, class, and name).

For example:<mappers><マッパーリソース = "mybatis/ Student.xml"/></mappers>

2. How to query data using XML file

Below is an example:

In the above code, we can see that using MyBatis to access data using the XML file is quite easy. We need to map the column name in the database table through the tag , using common and basic queries: select, update, delete, etc., as we do with pure SQL.

Note: when using XML to query, we need to point to the correct id .

3. How to query data using Annotation

Just like how to query data when using XML, we only need to create an interface to access data when using Annotation. In the interface, the user will declare queries and then use annotations to map SQL.

For example: public interface StudentMapper {

Original article:https://media.nal.vn/mybatis-config

Comments are closed.