mybatis 學習--mybatis基本用法入門

xiaopengyaonixi發表於2016-11-27

mybatis入門

1.建立java web專案


2.新增mybatis的jar包和mysql的驅動jar包


3.在資料庫中建立一個資料庫,並建立一個測試的資料表user並新增資料


4.新增user 的java類

package com.xiaopeng.model;

public class User {
	private int id;
	private String  username;
	private String userAge;
	private String userAddress;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getUserAge() {
		return userAge;
	}
	public void setUserAge(String userAge) {
		this.userAge = userAge;
	}
	public String getUserAddress() {
		return userAddress;
	}
	public void setUserAddress(String userAddress) {
		this.userAddress = userAddress;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", userAge=" + userAge + ", userAddress=" + userAddress
				+ "]";
	}
	
	
	
	
}

5.新增mybatis的mapper配置檔案user.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xiaopeng.dao.UserMapper">
  <select id="getUser" resultType="com.xiaopeng.model.User">
    select * from user where id = #{id}
  </select>
</mapper>


6.新增mybatis的配置檔案mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="mapper/user.xml"/>
  </mappers>
</configuration>


7.新增mybatis的工具類MyBatisUtil 用來建立SqlSessionFactory

package com.xiaopeng.mybatisutil;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 * 建立sqlsessionfactor
 * @author PengChan
 *
 */
public class MyBatisUtil {
	private static SqlSessionFactory sqlSessionFactory = null;
	
	public static SqlSessionFactory getSqlSessionFactory(){
		InputStream inputStream = null;
		if(sqlSessionFactory==null){
			String resource = "config/mybatis-config.xml";
			try {
				sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(resource));
				return sqlSessionFactory;
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		return sqlSessionFactory;
	}
}

優化可以只用單利模式來建立sqlsessionfactory

package com.xiaopeng.mybatisutil;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 * 建立sqlsessionfactor
 * 使用單例模式建立SqlSessionFactory
 * @author PengChan
 *
 */
public class MyBatisUtil {
	private static SqlSessionFactory sqlSessionFactory = null;
	
	private static final Class CLASS_LOCK = MyBatisUtil.class;
	
	private MyBatisUtil(){
		
	}
	
	public static SqlSessionFactory getSqlSessionFactory(){
		InputStream inputStream = null;
		String resource = "config/mybatis-config.xml";
		synchronized (CLASS_LOCK) {
			if(sqlSessionFactory==null){ 
				try {
					sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(resource));			 
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}
		} 
		return sqlSessionFactory;
	}
}


8.新增測試類執行測試

package comxiaopeng.test;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import com.xiaopeng.dao.UserMapper;
import com.xiaopeng.model.User;
import com.xiaopeng.mybatisutil.MyBatisUtil;

public class MyTest {

	@Test
	public void testDemo01(){
		SqlSession sqlSession=null;
		try {
			sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
			UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
			User user = userMapper.getUser(1L);
			System.out.println(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
9.執行結果


相關文章