MyBatis build project
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.