Mybatis源码分析(二) - Mybatis快速入门

1. 加入mybatis的依赖


		
		
			junit
			junit
			4.12
			test
		
		
			org.springframework
			spring-test
			4.3.2.RELEASE
			test
		

		
		
			org.slf4j
			slf4j-api
			1.7.10
		
		
			ch.qos.logback
			logback-classic
			1.1.2
		
		
			ch.qos.logback
			logback-core
			1.1.2
		

		
		
			mysql
			mysql-connector-java
			5.1.18
		


		
		
			org.mybatis
			mybatis
			3.4.1
		

	

	
		
			
				org.apache.maven.plugins
				maven-compiler-plugin
				2.1
				
					1.7
					1.7
				
			

			
				org.mybatis.generator
				mybatis-generator-maven-plugin
				1.3.2
				
					true
					true
				
			


		
	


2. classpath下添加mybatis的配置文件(mybatis-config.xml)





	

	
		
		
		
		
		
	
	


	
	
		
		
			
			
				
				
				
				
			
		

	

	


	
		
	


  


3. 场景介绍

Mybatis源码分析(二) - Mybatis快速入门_第1张图片
4. 编写实体类、mapper接口以及mapper xml文件

实体类:

package com.jiulu.mybatis.entity;

public class TUser {
	private Integer id;

	private String userName;

	private String realName;

	private Byte sex;

	private String mobile;

	private String email;

	private String note;

	private Integer positionId;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public Byte getSex() {
		return sex;
	}

	public void setSex(Byte sex) {
		this.sex = sex;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public Integer getPositionId() {
		return positionId;
	}

	public void setPositionId(Integer positionId) {
		this.positionId = positionId;
	}

	@Override
	public String toString() {
		return "TUser [id=" + id + ", userName=" + userName + ", realName=" + realName + ", sex=" + sex + ", mobile="
				+ mobile + ", email=" + email + ", note=" + note + ", positionId=" + positionId + "]";
	}

}

mapper接口:

package com.jiulu.mybatis.mapper;

import com.jiulu.mybatis.entity.TUser;

public interface TUserMapper {
	
    TUser selectByPrimaryKey(Integer id);
    
}

mapper配置文件(TUserMapper.xml):注意和mapper接口放同一目录





	


5. 编写实例代码

package com.jiulu.mybatis;

import java.io.IOException;
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;
import org.junit.Before;
import org.junit.Test;

import com.jiulu.mybatis.entity.TUser;
import com.jiulu.mybatis.mapper.TUserMapper;

public class MybatisQuickStart {

	private SqlSessionFactory sqlSessionFactory;

	@Before
	public void init() throws IOException {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		// 1.读取mybatis配置文件创SqlSessionFactory
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		inputStream.close();
	}

	@Test
	// 快速入门
	public void quickStart() throws IOException {
		// 2.获取sqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// 3.获取对应mapper
		TUserMapper mapper = sqlSession.getMapper(TUserMapper.class);
		// 4.执行查询语句并返回结果
		TUser user = mapper.selectByPrimaryKey(1);
		System.out.println(user.toString());

	}
	
}

运行结果:

18:38:49.978 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
18:38:50.424 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Opening JDBC Connection
18:38:51.063 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@376b4233]
18:38:51.077 [main] DEBUG c.j.m.m.T.selectByPrimaryKey - ==>  Preparing: select id, user_name , real_name , sex, mobile, email, note, position_id positionId from t_user where id = ? 
18:38:51.151 [main] DEBUG c.j.m.m.T.selectByPrimaryKey - ==> Parameters: 1(Integer)
18:38:51.192 [main] DEBUG c.j.m.m.T.selectByPrimaryKey - <==      Total: 1
TUser [id=1, userName=caojiulu, realName=***, sex=1, mobile=18291282102, [email protected], note=123, positionId=1]
 

你可能感兴趣的:(mybatis,mybatis源码分析)