初尝Mybatis

Mybatis与Hibernate的比较

1.Mybatis是半自动化,SQL需要开发人员编写,可以定制编写复杂的SQL

2.Mybatis可以针对数据或者特殊的场景做优化,提高应用的响应速度

3.Mybatis更轻量,更容易学习,Hibernate要支持定制化的SQL还需要HQL,更难掌握

4.Mybatis不需要bean对所有表的字段做映射,特别在开发电信系统时,一个表可能有30到40个字段,如果用Hibernate会比较麻烦

更详细的对比可以参考:【持久化框架】Mybatis与Hibernate的详细对比

编写一个Mybatis步骤:

英文比较好的可以参考:Getting started

具体步骤如下:

 * 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);
}

 

转载于:https://my.oschina.net/u/3049601/blog/1609008

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值