Addition, deletion and modification of mybatis tutorial_ Power node Java college sorting

select

A select element is very simple. For example:

This statement is called 'getstudent'. It has a string parameter and returns an object of studententity type.

Note that the identification of the parameter is: #{studentID}.

Select statement attribute configuration details:

insert

A simple insert statement:

Insert can use the auto generate primary key policy supported by the database, set usegenerated keys = "true", and then set the keyproperty to the corresponding column. For example, the student entity above uses auto generated to generate the primary key for the ID column

You can also use the selectkey element. The following example uses the MySQL database nextval ('student ') as a custom function to generate a key.

Insert statement attribute configuration details:

Selectkey statement attribute configuration details:

update、delete

A simple update:

A simple delete:

Details of attribute configuration of update and delete statements:

sql

The SQL element is used to define a reusable SQL statement segment for other statements to call. For example:

In this way, it can be directly referenced in the select statement. Change the above select statement to:

parameters

Parameters have been used in many places above, such as query, modify and delete conditions, inserted and modified data, basic data types that mybatis can use and complex data types of Java.

Basic data types: string, int, date, etc.

However, only one parameter can be provided when using the basic data type, so you need to use Java entity class or map type as the parameter type. Through #{} you can get its properties directly.

Basic type parameters

Retrieve the student list according to the enrollment time:

Java entity type parameters

Retrieve the list of students by name and gender. Use entity class as parameter:

Map parameters

Retrieve the list of students by name and gender. Use map as parameter:

Implementation of multi parameter

If you want to pass in multiple parameters, you need to add @ param annotation on the parameters of the interface. Give an example:

Interface writing method:

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
分享
二维码
< <上一篇
下一篇>>