Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use DAO Agent in MyBatis

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of how to use the DAO agent in MyBatis, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to use the DAO agent in MyBatis. Let's take a look.

DAO agent implements database operation 1. Remove the Dao interface implementation class

2. GetMapper gets the proxy object

Simply call the getMapper () method of SqlSession to get the object of the implementation class for the specified interface. The parameter to this method is the class value of the specified Dao interface class.

SqlSession session = factory.openSession (); StudentDao dao = session.getMapper (StudentDao.class)

Use tool CLA

StudentDao studentDao = MyBatisUtil.getSqlSession () .getMapper (StudentDao.class)

The object created by getMapper () is instead of the StudentDaoImpl class we created ourselves.

3. Use the Dao proxy object method to execute sql statements

Select method for query

@ Testpublic void testSelect () throws IOException {final List studentList = studentDao.selectStudents (); studentList.forEach (stu-> System.out.println (stu));}

Insert method for insertion

@ Testpublic void testInsert () throws IOException {Student student = new Student (); student.setId (1006); student.setName ("Lin Hao"); student.setEmail ("linhao@163.com"); student.setAge (26); int nums = studentDao.insertStudent (student); System.out.println ("add data using Dao:" + nums);} 4, in-depth understanding of parameters

Pass the parameters from the java code to the mapper.xml file.

ParameterType

ParameterType: the type of method parameter in the interface, the fully qualified name or alias of the type. This attribute is optional because MyBatis can infer the parameters of a specific incoming statement, and the default value is not set (unset). The parameters of the methods in the interface are passed from the java code to the sql statement of the mapper file.

Int or java.lang.Integer

Hashmap or java.util.HashMap

List or java.util.ArrayList

Student or com.bjpowernode.domain.Student

You can specify the type using parameterType.

Eg:

Delete from student where id=# {studentId} is equivalent to a simple parameter of delete from student where id=# {studentId}

The parameters of a method in the Dao interface have only one simple type (java basic type and String), and the placeholder # {any character} is independent of the parameter name of the method.

Interface method

Student selectById (int id)

Mapper file

Select id,name,email,age from student where id=# {studentId}

# {studentId}, studentId is a custom variable name, independent of the method parameter name.

Testing method

@ Testpublic void testSelectById () {/ / one parameter Student student = studentDao.selectById (1005); System.out.println ("query student whose id is 1005:" + student);} use @ Param

When the Dao interface method has more than one parameter, you need to use the parameter by name. Add @ Param ("custom parameter name") before the method parameter, and the mapper file uses # {custom parameter name}.

For example, define List selectStudent (@ Param ("personName")

String name) {… }

Mapper file select * from student where name =

# {personName}

Interface method

List selectMultiParam (@ Param ("personName") String name, @ Param ("personAge") int age)

Mapper file

Select id,name,email,age from student where name=# {personName} or age = # {personAge}

Testing method

@ Testpublic void testSelectMultiParam () {List stuList = studentDao.selectMultiParam ("Li Li", 20); stuList.forEach (stu-> System.out.println (stu));} use object

Using the java object to pass parameters, the property value of java is the parameter value required by sql. Each property is a parameter.

Syntax format: # {data type name in property,javaType=java

The name of the jdbcType= data type} javaType. The type of jdbcType can be detected by MyBatis, which generally does not need to be set. Common format # {property}

Create an object QueryParam that holds parameter values

Package com.bjpowernode.vo; public class QueryParam {private String queryName; private int queryAge; / / set, get method}

Interface method

List selectMultiObject (QueryParam queryParam)

Mapper file

Select id,name,email,age from student where name=# {queryName} or age = # {queryAge} or select id,name,email,age from student where name=# {queryName,javaType=string,jdbcType=VARCHAR} or age = # {queryAge,javaType=int,jdbcType=INTEGER}

Testing method

@ Testpublic void selectMultiObject () {QueryParam qp = new QueryParam (); qp.setQueryName ("Li Li"); qp.setQueryAge (20); List stuList = studentDao.selectMultiObject (qp); stuList.forEach (stu-> System.out.println (stu));} this is the end of the article on "how to use DAO Agent in MyBatis". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use DAO agents in MyBatis". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report