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 realize ResultMap result set by Mybatis

2025-03-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you how to achieve Mybatis ResultMap result set related knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can learn something after reading this article, let's take a look at it.

Fields in the database

Create a new project, copy the previous, test the entity class field inconsistency

1. Create a new mybatis-03

two。 Create a new db.properties profile

Driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useEncoding=false&characterEncoding=UTF-8&serverTimezone=GMTusername=rootpassword=root

3. Create a new mybatis-config.xml profile

4. Create the com.kuang.utils package and create the MybatisUtils utility class under the package

Package com.kuang.utils;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 java.io.IOException;import java.io.InputStream;//sqlSessionFactory is used to build the scope private static SqlSessionFactory sqlSessionFactory of sqlSessionpublic class MybatisUtils {/ / promote SqlSessionFactory Static {try {/ / the first step is to get the sqlSessionFactory object String resource = "mybatis-config.xml" using Mybatis; / / read the configuration file InputStream inputStream = Resources.getResourceAsStream (resource) through Resources; / / load a stream through SqlSessionFactoryBuilder to build a sqlSession factory sqlSessionFactory = new SqlSessionFactoryBuilder () .build (inputStream) } catch (IOException e) {e.printStackTrace ();}} / / now that we have SqlSessionFactory, as the name implies, we can get an instance of SqlSession from it. / / SqlSession provides all the methods needed to execute SQL commands in the database. Public static SqlSession getSqlSession () {return sqlSessionFactory.openSession ();}}

5. Create a com.kuang.pojo package that stores the entity class object User

Package com.kuang.pojo;public class User {private int id; private String name; private String password; public User (int id, String name, String password) {this.id = id; this.name = name; this.password = password;} public User () {} public int getId () {return id;} public void setId (int id) {this.id = id } public String getName () {return name;} public void setName (String name) {this.name = name;} public String getPassword () {return password;} public void setPassword (String password) {this.password = password } @ Override public String toString () {return "User {" + "id=" + id + ", name='" + name +'\'+ ", password='" + password +'\'+'}';}}

6. Create a com.kuang.dao package that stores the interface UserMapper and the interface implementation class UserMapper.xml

UserMapper

Package com.kuang.dao;import com.kuang.pojo.User;import java.util.List;import java.util.Map;public interface UserMapper {/ / query user information according to user id User getUserById (Integer id);}

UserMapper.xml

Select * from user where id=# {id}

7. Create a test class

Package com.kuang.dao;import com.kuang.pojo.User;import com.kuang.utils.MybatisUtils;import org.apache.ibatis.session.SqlSession;import org.junit.Test;import java.util.HashMap;import java.util.List;import java.util.Map;public class UserDaoTest {@ Test public void testGetUserById () {/ / first step: get the SqlSession object SqlSession sqlSession = MybatisUtils.getSqlSession () / / execute Sql// method 1: getMapper (recommended) UserMapper userDao = sqlSession.getMapper (UserMapper.class); User user1 = userDao.getUserById (1); System.out.println (user1); / / close sqlSession sqlSession.close ();}}

8 test results and problem analysis

Solution 1: alias the pwd field in the sql query statement

Change the original sql statement

Select id,name,pwd as password from user where id=# {id}

Change to

Select id,name,pwd as password from user where id=# {id}

8.2 resultMap result set mapping

Field name: id name pwd

Attribute name: id name password

Select * from user where id=# {id}

The resultMap element is the most important and powerful element in MyBatis

The design idea of ResultMap is to achieve zero configuration for simple statements and only describe the relationship between statements for more complex statements.

The beauty of ResultMap-you don't have to configure them explicitly

These are all the contents of the article "how to achieve ResultMap result sets in Mybatis". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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