初嘗Mybatis

Mybatis與Hibernate的比較html

1.Mybatis是半自動化,SQL須要開發人員編寫,能夠定製編寫複雜的SQLjava

2.Mybatis能夠針對數據或者特殊的場景作優化,提升應用的響應速度mysql

3.Mybatis更輕量,更容易學習,Hibernate要支持定製化的SQL還須要HQL,更難掌握sql

4.Mybatis不須要bean對全部表的字段作映射,特別在開發電信系統時,一個表可能有30到40個字段,若是用Hibernate會比較麻煩數據庫

更詳細的對比能夠參考:【持久化框架】Mybatis與Hibernate的詳細對比apache

編寫一個Mybatis步驟:session

英文比較好的能夠參考:Getting startedmybatis

具體步驟以下:app

 * 1.下載mybatis包
 * 2.在類路徑下配置全局配置文件
 * 3.得到SqlSeessionFactory
 * 4.得到SqlSeesion
 * 5.配置SQL文件
 * 6.編寫DAO接口框架

package com.jv.test;

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.Test;

import com.jv.bean.Employee;
import com.jv.dao.EmployeeMapper;

/*
 * 1.下載mybatis包
 * 2.在類路徑下配置全局配置文件
 * 3.得到SqlSeessionFactory
 * 4.得到SqlSeesion
 * 5.配置SQL文件
 * 6.編寫DAO接口
 */

public class TestMybatis {
	@Test
	public void test2() throws IOException {
		// 1.得到SqlSessionFactory
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		// 2.得到SqlSeession
		SqlSession session = sqlSessionFactory.openSession();
		// 3.使用SqlSession獲取數據庫記錄
		try {
			Employee employee = session.selectOne("com.jv.dao.EmployeeMapper.getEmployeeById", 1);
			System.out.println(employee);
		} finally {
			session.close();
		}
	}
	@Test
	public void test1() throws IOException {
		//1.得到SqlSessionFactory
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		//2.得到SqlSeession
		SqlSession session = sqlSessionFactory.openSession();
		// 3.使用SqlSession獲取數據庫記錄
		try {
			/*這種方式相比老的有以下優勢:
			 * 1.代碼更簡潔
			 * 2.能夠校驗參數,由於方法對參數有嚴格限制
			 * 3.更易閱讀
			 * 4.配置文件更獨立
			 */
			EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
			Employee employee = mapper.getEmployeeById(1);
			System.out.println(employee);
		} finally {
			session.close();
		}
	}
}
<?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/study?serverTimezone=UTC"/>
        <property name="username" value="root"/>
        <property name="password" value="12340101"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="EmployeeMapper.xml"/>
  </mappers>
</configuration>

jdbc:mysql://localhost:3306/study?serverTimezone=UTC

MySQL新驅動程序要求時區匹配,因此在上面的URL中添加serverTimezone=UTC

<?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.jv.dao.EmployeeMapper">
  <select id="getEmployeeById" resultType="com.jv.bean.Employee">
    select employee_id,first_name,last_name,gendor,birthday from employee where employee_id=#{id}
  </select>
</mapper>
package com.jv.bean;

import java.sql.Date;

public class Employee {
	private Integer employee_id;
	private String first_name;
	private String last_name;
	private Integer gendor;
	private Date birthday;
	public Integer getEmployee_id() {
		return employee_id;
	}
	public void setEmployee_id(Integer employee_id) {
		this.employee_id = employee_id;
	}
	public String getFirst_name() {
		return first_name;
	}
	public void setFirst_name(String first_name) {
		this.first_name = first_name;
	}
	public String getLast_name() {
		return last_name;
	}
	public void setLast_name(String last_name) {
		this.last_name = last_name;
	}
	public Integer getGendor() {
		return gendor;
	}
	public void setGendor(Integer gendor) {
		this.gendor = gendor;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	@Override
	public String toString() {
		return "Employee [employee_id=" + employee_id + ", first_name=" + first_name + ", last_name=" + last_name
				+ ", gendor=" + gendor + ", birthday=" + birthday + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((birthday == null) ? 0 : birthday.hashCode());
		result = prime * result + ((employee_id == null) ? 0 : employee_id.hashCode());
		result = prime * result + ((first_name == null) ? 0 : first_name.hashCode());
		result = prime * result + ((gendor == null) ? 0 : gendor.hashCode());
		result = prime * result + ((last_name == null) ? 0 : last_name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Employee other = (Employee) obj;
		if (birthday == null) {
			if (other.birthday != null)
				return false;
		} else if (!birthday.equals(other.birthday))
			return false;
		if (employee_id == null) {
			if (other.employee_id != null)
				return false;
		} else if (!employee_id.equals(other.employee_id))
			return false;
		if (first_name == null) {
			if (other.first_name != null)
				return false;
		} else if (!first_name.equals(other.first_name))
			return false;
		if (gendor == null) {
			if (other.gendor != null)
				return false;
		} else if (!gendor.equals(other.gendor))
			return false;
		if (last_name == null) {
			if (other.last_name != null)
				return false;
		} else if (!last_name.equals(other.last_name))
			return false;
		return true;
	}
	
}
package com.jv.dao;

import com.jv.bean.Employee;

public interface EmployeeMapper {
	public Employee getEmployeeById(Integer id);
}
相關文章
相關標籤/搜索