Mybatis quick start

Let's talk about the reason why I came to learn and summarize mybatis today..

When I first started learning springboot, I built a bug management platform with spotingboot + jdbctemplate + thymeleaf

Later, I used my own interface to test the impact of the number of DB connections on performance. TPS will be particularly affected by the configuration of the connection pool in the program

Later, a lot of data emerged in the DB. My bug management platform has an interface to list all bugs. This page will be particularly slow, so I thought of paging.

When looking up paging information on the Internet, I found that most of them are for mybatis. There will be no ready-made paging plug-in when querying with jdbctemplate. You need to rewrite some methods in jdbctemplate to add page numbers and the number of entries per page

Every day remains the same, work remains the same.

Suddenly, I had a free day and began to teach myself about springboot, so I automatically generated mapper and entity classes with the springboot + mybatis + mybatis plug-in according to the tutorial.

Does mybatis have to be in the springboot framework? Suddenly, I began to search the history of mybatis and start the simplest learning.

What is mybatis? What was originally developed for?

Mybatis was originally an open source project ibatis of Apache. In 2010, the project was migrated from Apache Software Foundation to Google Code and renamed mybatis. Moved to GitHub in November 2013.

Ibatis comes from the combination of "Internet" and "abatis". It is a persistence layer framework based on Java. The persistence layer framework provided by ibatis includes SQL maps and data access objects (Daos)

Mybatis official website - FQ required

Download the first one in ZIP format. After decompression, it is shown in the figure below. There is a PDF document in it. You can refer to the configuration mapper and config file. License and notice are just some certificate descriptions, which can be ignored. Under the Lib directory are some commonly used jar packages.

mybatis-xx. xx. x. Jar can be placed in its own project source. If you use maven, you can find the path of Maven yourself.

How does mybatis work?

No matter what kind of program, persistent data is the top priority. It's impossible for the previous user to directly operate the database. It's not safe, and not everyone will.

Therefore, it is necessary to program the page and the background interface, and let mybaits operate the object, just like operating the database.

User table, user object, one-to-one correspondence

If you want to view, update and add objects, you can directly operate the objects and store them in the persistent data dB

The user table is created above, and then the user object is created

There are tables and objects. Now it is the role of mybatis. First, configure config XML, and then configure the mapping file usermapper xml. These are introduced in the PDF just downloaded, with examples.

The above is the directory structure of the project. I created it a little casually. The following l two are config XML and usermapper XML Mapping File

 


<select id="selectAll" resultType="com.test.mybatis.entity.User"&gt;
    select * from user
</select>

<insert  id="addUser" parameterType="com.test.mybatis.entity.User"&gt;
    insert into user (name,password) values(#{name},#{age},#{phone},#{password})
</insert>


<update  id="updateUser" parameterType="com.test.mybatis.entity.User"&gt;
    update user set name = #{name},age =#{age},phone =#{phone},password = #{password} where id = #{id}
</update>


<delete  id="deleteUserById" parameterType ="int"&gt;
    delete from user where id = #{id}
</delete>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>