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 mybatis

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly shows you "how to use mybatis", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use mybatis" this article.

First, let's talk about what Mybatis,MyBatis is. It is an excellent persistence layer framework that supports custom SQL, stored procedures, and advanced mapping. MyBatis eliminates almost all the JDBC code and the work of setting parameters and getting result sets. MyBatis can configure and map primitive types, interfaces, and Java POJO (Plain Old Java Objects, plain old Java objects) to records in the database through simple XML or annotations.

Let's do a simple introduction to Mybatis (the project is built using Idea)

Prepare to create the corresponding database and table

Create database lagou;create table users (id int primary key, username varchar (20), password varchar (10))

Create a maven project

Named mybatis-start

The overall project structure is shown in the figure

two。 We introduce the jar package of mybatis, we introduce the lombok toolkit to facilitate the creation of set/get methods for entity classes, and we introduce junit for testing.

4.0.0 org.example mybatis-start 1.0-SNAPSHOT mysql mysql-connector-java 8.0.21 org.mybatis mybatis 3.5.5 org.projectlombok lombok 1.18.12 Junit junit 4.12 test

First, let's create the entity class User

@ Data annotations can help us create get/set methods automatically, and @ ToString annotations can help us create toString methods.

@ Data@ToStringpublic class User {private Integer id; private String username; private String password;}

Create a Mapper interface for User

It contains three methods, two queries, one to add, if you need to add more methods yourself

Public interface IUserDao {List getUserList (); User getUserById (Integer userId); void addUser (User user);}

Create a mapping file userMapper.xml for User entities

Be careful

We use the name user here because we have an alias in the configuration file of mybatis. If not, we need to use the full path of User.java.

Namespace is going to work with our IUserDao.java all the time.

Tag select and id in insert should be consistent with the method name defined in IUserDao.java

Select * from users select * from users where id = # {id} insert into users values (# {id}, # {username}, # {password})

Create a configuration file sqlMapConfig.xml for Mybatis

Here, in order not to hard code the database configuration, we introduce the jdbc.properties configuration file, which is specifically configured for the jdbc parameter.

Create a jdbc.properties profile

Jdbc.driver=com.mysql.jdbc.Driverjdbc.ur=jdbc:mysql://localhost:3306/lagoujdbc.username=rootjdbc.password=root

8. Next, we create a test class to test.

Public class IUserDaoTest {private SqlSession sqlSession; private IUserDao userDao; @ Before public void getMapper () {InputStream resourceAsStream = null; try {resourceAsStream = Resources.getResourceAsStream ("sqlMapConfig.xml");} catch (IOException e) {System.out.println ("failed to load configuration");} SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder () .build (resourceAsStream); this.sqlSession = sqlSessionFactory.openSession () This.userDao = sqlSession.getMapper (IUserDao.class);} @ Test public void addUser () {User user = new User (); user.setId (1); user.setUsername ("lagou"); user.setPassword ("123456"); userDao.addUser (user); / / commit transaction sqlSession.commit () } @ Test public void getUserList () {List userList = userDao.getUserList (); for (User user: userList) {System.out.println (user);} @ Test public void getUserById () {User user = userDao.getUserById (1); System.out.println (user);}}

We can insert a piece of data first, and then query whether the data is inserted successfully.

The log prints normally, and we can see that the inserted data is already in the database.

The above is all the contents of the article "how to use mybatis". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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

Internet Technology

Wechat

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

12
Report