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

MyBatis build project

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Toolkit:

Netbeans8

Maven

MyBatis

Project source code: https://github.com/sun2shadow/simpleMybatis

To create a project using netbeans, select File-> New Project-> Maven- > Web Application

Open the project directory, right-click on dependencies-> add dependencies-> query box enter mysql- > Select mysql:mysql-connector-java- > click to select the corresponding mysql driver version.

Create databases and tables

Create database foretaste;use foretastecreate table user_info (id int (11) not null primary key auto_increment, nickname varchar (50) not null, phone_num char (11) not null, created_time timestamp not null default current_timestamp, last_update_time timestamp not null default current_timestamp)

4. Create an entity for UserInfo

Package com.shadow.foretaste.entity;import java.util.Date;/** @ author sunny * / public class UserInfo {private int id; private String nickname; private String phoneNum; private Date createdTime; private Date lastUpdateTime; public int getId () {return id;} public void setId (int id) {this.id = id;} public String getNickname () {return nickname } public void setNickname (String nickname) {this.nickname = nickname;} public String getPhoneNum () {return phoneNum;} public void setPhoneNum (String phoneNum) {this.phoneNum = phoneNum;} public Date getCreatedTime () {return createdTime;} public void setCreatedTime (Date createdTime) {this.createdTime = createdTime;} public Date getLastUpdateTime () {return lastUpdateTime } public void setLastUpdateTime (Date lastUpdateTime) {this.lastUpdateTime = lastUpdateTime;} @ Override public String toString () {return "UserInfo [id=" + id;}}

5. To create a UserInfoDao, you must first create a Dao interface for the namspace specified when the mapper is bound

Package com.shadow.foretaste.dao;import com.shadow.foretaste.entity.UserInfo;/** @ author sunny * / public interface UserInfoDao {/ * * * query user information according to Id * @ param id * @ return * / UserInfo getUserInfoById (int id);}

6. Click-> other sources of the project-> src/main/source, right-click on the default package, xml file, and name it mybatis-config.xml

7. Under the source file, create a new mapper folder and create a UserInfoMapper.xml file

Select * from user_info where id = # {id}

8. Create MyBatisUtils to get sqlSession

Package com.shadow.foretaste.util;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;/** @ author sunny * / public class MyBatisUtils {private static SqlSessionFactory factory = null; / / initialize session factory public static void initFactory () throws Exception {InputStream inputStream = Resources.getResourceAsStream ("mybatis-config.xml") Factory = new SqlSessionFactoryBuilder (). Build (inputStream);} / * * get sqlSession session * @ return * / public static SqlSession getSession () {if (null = = factory) {try {initFactory ();} catch (Exception ex) {ex.printStackTrace () } return factory.openSession ();}}

8. Create a test method to verify the configuration

Import com.shadow.foretaste.util.MyBatisUtils;import org.apache.ibatis.session.SqlSession;import static org.junit.Assert.assertNotNull;import org.junit.Test;/** @ author sunny * / public class TesMyBatis {@ Test public void testMyBatis () {SqlSession session = MyBatisUtils.getSession (); assertNotNull (session); if (session! = null) {session.close ();}

All right, at this point, the framework of myBatis is finished.

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

*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