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 create a myBatis project

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

Share

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

This article introduces the knowledge of "how to create a myBatis project". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Introduction 1.1, core components

SqlSessionFactoryBuilder (constructor): generate SqlSessionFactory based on configuration information or code

SqlSessionFactory (factory interface): relies on the factory to generate SqlSession (session)

SqlSession (session): is an interface that can either send SQL to execute the returned result or get the Mapper interface

SQL Mapper: a newly designed component of MyBatis, which consists of java interfaces and XML files (or annotations). You need to give the corresponding SQL and mapping rules, which are responsible for sending SQL to execute and return the results.

1.2. The relationship between components:

1.3.How to obtain myBatis:

1. Maven warehouse

2 、 Github

3. Chinese documents

2. Create project 2.1, database creation

Create database: user table

Create table user (- > id int (20) not null primary key,-> name varchar (30) default null,-> pwd varchar (30) default null); Query OK, 0 rows affected (0.02 sec) mysql > insert into user (id,name,pwd) values (1, "Zhang San", 123456); Query OK, 1 row affected (0.01 sec) mysql > insert into user (id,name,pwd) values (2, "Li Si", 123456) Query OK, 1 row affected (0.00 sec) mysql > insert into user (id,name,pwd) values (3, Wang Wu, 123456); Query OK, 1 row affected (0.01 sec)

Create a normal Maven project

Delete the src folder

Import dependency

2.2. Create a submodule

2.2.1. Write the core configuration file of Mybatis

2.2.2. Write tool classes for myBatis

/ / sqlSessionFactory- > sqlSessionpublic class MybatisUtils {private static SqlSessionFactory sqlSessionFactory; static {try {/ / the first step of using mybatis to get the sqlSessionFactory object String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream (resource); sqlSessionFactory = new SqlSessionFactoryBuilder (). Build (inputStream);} catch (IOException e) {e.printStackTrace () }} / / SqlSession completely includes all the methods for executing Sql commands to the database public static SqlSession getSqlSession () {/ / SqlSession sqlSession = sqlSessionFactory.openSession (); / / return sqlSession; return sqlSessionFactory.openSession ();}} 2.3.Writing code

Entity class

Package com.malajava.pojo;public class User {private int id; private String name; private String pwd; public User (int id, String name, String pwd) {this.id = id; this.name = name; this.pwd = pwd;} 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 getPwd () {return pwd;} public void setPwd (String pwd) {this.pwd = pwd } @ Override public String toString () {return "User {" + "id=" + id + ", name='" + name +'\'+ ", pwd='" + pwd +'\'+'}';}}

Dao interface

Package com.malajava.dao;import com.malajava.pojo.User;import java.util.List;public interface UserDao {List getUserList ();}

Interface implementation class

Transformed from the original UserDaoImpl to a Mapper configuration file

Select * from mybatis.user

2.4.Test package com.malajava.dao;import com.malajava.pojo.User;import com.malajava.utils.MybatisUtils;import org.apache.ibatis.session.SqlSession;import org.junit.Test;import java.util.List;public class UserDaoTest {@ Test public void test () {/ / the first step: get the sqlSession object SqlSession sqlSession = MybatisUtils.getSqlSession (); / / execute the Sql statement UserDao userDao = sqlSession.getMapper (UserDao.class) List userList = userDao.getUserList (); for (User user: userList) {System.out.println (user);} sqlSession.close ();}} "how to create a myBatis project" ends here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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