In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
What this article shares with you is about how to map input and output in Mybatis_day03. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Input mapping and output mapping
The sql that operates the database is defined in the Mapper.xml mapping file. Each sql is a statement, and the mapping file is the core of mybatis.
1.parameterType (input type)
Pass simple types
Refer to the first day.
Pass pojo object
Mybatis uses ognl expressions to resolve the value of the object field, and the value in # {} or ${} parentheses is the name of the pojo attribute.
Pass pojo wrapper object
In the development, the query condition is passed through pojo, which is a comprehensive query condition, including not only user query conditions but also other query conditions (for example, user purchase information is also used as query conditions). In this case, packaging objects can be used to transfer input parameters.
The Pojo class contains pojo.
Requirements: query the user information according to the user name, and put the query conditions in the user attribute of QueryVo.
Public class QueryVo {private User user; public User getUser () {return user;} public void setUser (User user) this.user = user;*}}
Sql statement
SELECT * FROM user where username like'% Liu%'
Mapper file
SELECT * FROM user where username like'% ${user.username}% 'interface public interface UserMapper {User findUserByid (Integer id); List findUserByUserName (String name); List findUserByUserNameAndSex (String name,String sex); Integer insertUser (User user); Integer deleteByUserId (Integer id); Integer updateUserById (User user); List QueryVoByUserList (QueryVo queryVo);} Test method @ Test public void testFindUserByQueryVo () throws Exception {SqlSession sqlSession = sessionFactory.openSession () / / get mapper proxy object UserMapper userMapper = sqlSession.getMapper (UserMapper.class); / / create QueryVo object QueryVo queryVo = new QueryVo (); / / create user object User user = new User (); user.setUsername ("Liu"); queryVo.setUser (user); / / query user List list = userMapper.findUserByQueryVo (queryVo) according to queryvo; System.out.println (list); sqlSession.close ();}
1.resultType (output type)
Output simple types
Refer to the getnow output date type and see the following example to output an integer:
Mapper.xml file
Select count (1) from userMapper interface public int findUserCount () throws Exception; call: Public void testFindUserCount () throws Exception {/ / get session SqlSession session = sqlSessionFactory.openSession (); / / get mapper interface instance UserMapper userMapper = session.getMapper (UserMapper.class); / / pass Hashmap object to query user list int count = userMapper.findUserCount (); / / close session session.close ();}
The result set that the output simple type must query has a record, and finally converts the value of the first field to the output type.
Use session's selectOne to query a single record.
Output pojo object
Refer to the first day.
Output pojo list
Refer to the first day.
ResultMap
ResultType can specify pojo to map the query results to pojo, but requires that the attribute names of the pojo match the column names of the sql query to map successfully.
If the sql query field name does not match the attribute name of the pojo, you can make a correspondence between the field name and the attribute name through resultMap, and resultMap essentially needs to map the query results to the pojo object.
ResultMap can implement pojo that maps query results to complex types, such as pojo and list to implement one-to-one queries and one-to-many queries in query result mapping objects.
Mapper.xml definition
Use resultMap to specify the personmap defined above.
Define resultMap
Since the sql query column in the mapper.xml above is inconsistent with the Users.java class attribute, you need to define a resultMap:userListResultMap to correspond the Users.java class attribute to the sql query column.
This property represents the unique identity of the query result set, which is very important If multiple fields are compound unique constraints, multiple fields are defined.
Property: represents the properties of the User class.
Column: represents the field name queried by sql.
Column and property are put together to map the fields queried by sql to the specified pojo class attributes.
The normal result, that is, the property of pojo
Mapper interface definition
Public List findUserListResultMap () throws Exception; above is how to input and output mapping in Mybatis_day03. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.