MyBatis-HelloWorld

  • 1.导包项目环境配置

conf

  • 2.创建数据库mybatisedu以及表t_employees:

在这里插入图片描述

  • 3.Employee 以及相应 Dao的interface注意这里是interface:
package com.edu.bean;

public class Employee {
	
	private Integer id;
	private String empName;
	private String email;
	private Integer gender;
	
	public Employee() {
		super();
	}
	public Employee(Integer id, String empName, String email, Integer gender) {
		super();
		this.id = id;
		this.empName = empName;
		this.email = email;
		this.gender = gender;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getEmpName() {
		return empName;
	}
	public void setEmpName(String empName) {
		this.empName = empName;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Integer getGender() {
		return gender;
	}
	public void setGender(Integer gender) {
		this.gender = gender;
	}
	@Override
	public String toString() {
		return "Employee [id=" + id + ", empName=" + empName + ", email=" + email + ", gender=" + gender + "]";
	}
}

package com.edu.dao;

import com.edu.bean.Employee;

public interface EmployeeDao {
	public Employee getEmpById(Integer id);
}
  • 4.employeeDao配置文件:
<?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">
  
  <!-- namespace:名称空间:写接口的全类名,告诉MyBatis这个配置文件是用来实现哪个接口的 -->
<mapper namespace="com.edu.dao.EmployeeDao">
  <!-- 
  select 标签用来定义一个查询操作  
  id 方法名
  resultType 返回类型(全类名)
  #{参数名} 代表取出传递过来的某个参数的值
  -->
  <select id="getEmpById" resultType="com.edu.bean.Employee">
    select * from t_employees where id = #{id}
  </select>
</mapper>
  • 5.mybatis的全局配置文件:
<?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">
<!-- 这是第一个全局配置文件,指导myBatis如何正确运行,比如连接哪个数据库 -->
<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/mybatisedu"/>
        <property name="username" value="root"/>
        <property name="password" value="C0c0splaying!"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
  	<!-- 注册我们编写的每一个接口的实现文件,这里我们的文件直接在类路径(classPath)下 -->
    <mapper resource="mybatis/EmployeeDao.xml"/>
  </mappers>
</configuration>
  • 6.最终Test:
package com.edu.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.edu.bean.Employee;
import com.edu.dao.EmployeeDao;

public class MyBatisTest {
	
	@Test
	public void test01(){
		//1.根据全局配置文件穿件出一个SqlSessionFactory
		//创建的SqlSessionFactory负责创建SqlSession对象,
		//SqllSessiion,代表和数据库的一次会话
		String resource = "mybatis-config.xml";
		Employee employee = null;
		InputStream inputStream = null;
		try {
			inputStream = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			//2.获取和数据库的一次会话
			SqlSession openSession = sqlSessionFactory.openSession();
			try {
				//3.使用SqlSession操作数据库
				EmployeeDao employeeDao = openSession.getMapper(EmployeeDao.class);
				employee = employeeDao.getEmpById(1);
			}finally {
				openSession.close();
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		System.out.println(employee);
	}
}	

  • 7.成功查询:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值