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