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 the resultType value in mybatis Mapper's xml file

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

Share

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

How to use mybatis Mapper xml file resultType value, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

The resultType value in the xml file of Mapper ① returns the value of the general data type

For example, query to get a field value in a table according to id or field condition.

User Sel (int id); / / query according to id

SQL mapping file

/ / notice that the full name of the class select username from user_test where id = # {id} is written.

If you need an abbreviation, you need to define an alias for resultType.

The basic type of java does not require an alias:

Type of alias mapping _ bytebyte_longlong_shortshort_intint_booleanbooleanintegerIntegerstringStringdateDatebooleanBoolean ② when the return type is javaBean select * from user_test where id = # {id} ③ when the return is List type

Sometimes we need a fuzzy query or a full table query, and the returned data is multiple, so we can save multiple pieces of data to list.

Mapper interface

List getUsers ()

SQL mapping file:

Select * from user

It is important to note that the return type is List, but resultType is still javaBean. Some people will wonder why this is not a collection type. In fact, looking at the essence through the phenomenon, it is still JavaBean.

④ returns the number of types Map structure

When we return a piece of data during a query, we can encapsulate {field name, field value} into a Map structure.

Map findUserByName (Integer id)

SQL mapping file:

Select userName from user where id=# {id}; ⑤ talk about the method of passing multiple parameters into the mapper layer in mybatis

1. Actually, it can be regarded as multiple parameters.

Public List findUser (String name1, String name2)

Corresponding SQL mapping file:

Select * from user_test where userName = # {0} and realName = # {1}

Among them, # {0} and # {1} are indexed by default according to the order in which values are transmitted by mybatis, but errors will be reported in springboot2.1 (integrated mybatis framework). I think it is possible to say yes on the Internet. Many attempts do not seem to work. Error is posted below:

~ ~ org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter'0' not found. Available parameters are [arg1, arg0, param1, param2] ~ ~

two。 It can be regarded as annotated.

Public List findUser (@ Param ("name1") String name1, @ Param ("name2") String name2)

Corresponding SQL file:

Select * from user_test where userName = # {name1} and realName = # {name2}

3. Parameters can be encapsulated in Map

Sometimes our business data query does not define the corresponding POJO, so we encapsulate the parameters.

Public List findUser1 (Map map)

Corresponding SQL file:

ResultType Analysis of select * from user_test where userName = # {username} and realName = # {realname} mybatis Learning

ResultType is the return value type defined in the sql mapping file. The return value has basic type, object type, List type, Map type and so on. Now sum up and explain.

Summary

ResultType:

1. Basic type: resultType= basic type

2. List type: the type of element in resultType=List

3. Map type:

Single record: resultType = map

Multiple records: resultType = type of value in Map

1. Object type

For the object type resultType, just write the full class name of the object directly.

Example:

HotelMapper interface

Package com.pjf.mybatis.dao;import com.pjf.mybatis.po.Hotel;public interface HotelMapper {/ / return value type is Hotel public Hotel getHotel (Integer I);}

HotelMapper.xml

Select * from hotel where id=# {id}

Test class:

Package com.pjf.mybatis;import java.io.IOException;import java.io.InputStream;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Test;import com.pjf.mybatis.dao.HotelMapper;import com.pjf.mybatis.po.Hotel Public class TestHotel {public SqlSessionFactory sqlSessionFactory () throws IOException {/ / mybatis configuration file String resource = "mybatis_config.xml"; / / use the classloader to load the configuration file of mybatis (which also loads the associated mapping file) TestHotel.class.getClassLoader () InputStream is = Resources.getResourceAsStream (resource); / / build sqlSession factory SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder (). Build (is) Return sessionFactory;} / / check @ Test public void getHotel () throws IOException {SqlSessionFactory sessionFactory = sqlSessionFactory (); SqlSession session = sessionFactory.openSession (); HotelMapper hotelMapper = session.getMapper (HotelMapper.class); System.out.println (hotelMapper.getClass ()); / / return the Hotel object directly and print out Hotel hotel = hotelMapper.getHotel (1001) System.out.println (hotel); session.close ();}} 2, List type

The returned value is List, and resultType is the type of object in List. For example, List,resultType is Hotel.

Example:

HotelMapper interface

Package com.pjf.mybatis.dao;import java.util.List;import com.pjf.mybatis.po.Hotel;public interface HotelMapper {/ / return value is List public List getHotel (Integer I);}

HotelMapper.xml

Select * from hotel where price > # {price}

Test class:

Package com.pjf.mybatis;import java.io.IOException;import java.io.InputStream;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Test;import com.pjf.mybatis.dao.HotelMapper;import com.pjf.mybatis.po.Hotel Public class TestHotel {public SqlSessionFactory sqlSessionFactory () throws IOException {/ / mybatis configuration file String resource = "mybatis_config.xml"; / / use the classloader to load the configuration file of mybatis (which also loads the associated mapping file) TestHotel.class.getClassLoader () InputStream is = Resources.getResourceAsStream (resource); / / build sqlSession factory SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder (). Build (is) Return sessionFactory;} / / check @ Test public void getHotel () throws IOException {SqlSessionFactory sessionFactory = sqlSessionFactory (); SqlSession session = sessionFactory.openSession (); HotelMapper hotelMapper = session.getMapper (HotelMapper.class); System.out.println (hotelMapper.getClass ()); / / the returned value is List List list = hotelMapper.getHotel (1000) For (Hotel hotel: list) {System.out.println (hotel);} session.close ();}} 3, Map type

A. The map,key that returns a single record is the attribute, and the value is the attribute value. ResultType is map

HotelMapper interface

Package com.pjf.mybatis.dao;import java.util.Map;import com.pjf.mybatis.po.Hotel;public interface HotelMapper {/ / return value is Map,key is the attribute name, value is the attribute value public Map getHotel (Integer I);}

HotelMapper.xml

Select * from hotel where id=# {id}

Test category: hotels returning to id=1001

Package com.pjf.mybatis;import java.io.IOException;import java.io.InputStream;import java.util.Map;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Test;import com.pjf.mybatis.dao.HotelMapper;import com.pjf.mybatis.po.Hotel Public class TestHotel {public SqlSessionFactory sqlSessionFactory () throws IOException {/ / mybatis configuration file String resource = "mybatis_config.xml"; / / use the classloader to load the configuration file of mybatis (which also loads the associated mapping file) TestHotel.class.getClassLoader () InputStream is = Resources.getResourceAsStream (resource); / / build sqlSession factory SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder (). Build (is) Return sessionFactory;} / / check @ Test public void getHotel () throws IOException {SqlSessionFactory sessionFactory = sqlSessionFactory (); SqlSession session = sessionFactory.openSession (); HotelMapper hotelMapper = session.getMapper (HotelMapper.class); System.out.println (hotelMapper.getClass ()); / / the returned value is map Map map = hotelMapper.getHotel (1001); System.out.println (map) Session.close ();}}

B. The map,key that returns multiple records is any one of the attributes, and the value is the object type. For example, Map,resultType is Hotel

When returning the map of multiple records, key is any one of the attributes and the value is the object type, but key needs to specify a property in the object named key through @ MapKey ("hotelName").

Example:

HotelMapper interface

Package com.pjf.mybatis.dao;import java.util.Map;import org.apache.ibatis.annotations.MapKey;import com.pjf.mybatis.po.Hotel;public interface HotelMapper {/ / return value as Map,key requires @ MapKey ("attribute name") to specify that a property in javaBean is named key,value as the object @ MapKey ("hotelName") public Map getHotel (Integer I);}

HotelMapper.xml file

Select * from hotel where price > # {price}

Test category: return hotels with prices above 1000

Package com.pjf.mybatis;import java.io.IOException;import java.io.InputStream;import java.util.Map;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Test;import com.pjf.mybatis.dao.HotelMapper;import com.pjf.mybatis.po.Hotel Public class TestHotel {public SqlSessionFactory sqlSessionFactory () throws IOException {/ / mybatis configuration file String resource = "mybatis_config.xml"; / / use the classloader to load the configuration file of mybatis (which also loads the associated mapping file) TestHotel.class.getClassLoader () InputStream is = Resources.getResourceAsStream (resource); / / build sqlSession factory SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder (). Build (is) Return sessionFactory;} / / check @ Test public void getHotel () throws IOException {SqlSessionFactory sessionFactory = sqlSessionFactory (); SqlSession session = sessionFactory.openSession (); HotelMapper hotelMapper = session.getMapper (HotelMapper.class); System.out.println (hotelMapper.getClass ()); / / the returned value is map Map map = hotelMapper.getHotel (1000); System.out.println (map) Session.close ();}} is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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: 255

*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