在入门学习中已经创建好maven项目,并导入相关依赖,接下来就可以利用mybatis对数据库进行操作
数据库包含两个表:
在pojo层创建一个Employee实体类
package com.neuedu.pojo; import java.util.Date; public class Employee { private Integer id; private String loginName; private String password; private String name; private Date hiredate; private String email; private Integer status; private String photoPath; private Integer deptId; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } public String getPhotoPath() { return photoPath; } public void setPhotoPath(String photoPath) { this.photoPath = photoPath; } public Integer getDeptId() { return deptId; } public void setDeptId(Integer deptId) { this.deptId = deptId; } @Override public String toString() { return "Employee [id=" + id + ", loginName=" + loginName + ", password=" + password + ", name=" + name + ", hiredate=" + hiredate + ", email=" + email + "]"; } }
可以直接在resources下创建xml映射文件,EmpMapper.xml
如果idea识别不出xml文件,可以看IDEA创建Mapper.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=""> </mapper>
根标签mapper的namespace属性现在可以随意给值 ,但当使用mapper代理方式开发时,有特定的取值。
sql语句
<select id="findEmpById" parameterType="int" resultType="com.neuedu.pojo.Employee"> <!-- id就是这条语句的唯一标识,parameterType是员工id的属性,resultType是返回类型,要把实体类的路径写完整 --> select * from tb_emp where id = #{value} <!-- 占位符要使用#{} parameterType的类型如果为 简单类型(基本类型和String),#{}中的值任意。--> </select>
测试
创建一个测试类
package com.neuedu.test; 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.neuedu.pojo.Employee; public class TestMybatis { @Test public void testFindById() throws Exception{ InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml"); sf = new SqlSessionFactoryBuilder().build(in); SqlSession session = sf.openSession(); Employee emp = session.selectOne("findEmpById", 3); System.out.println(emp.getName()); session.close(); } }
查询实质上调用的还是session,session调用完成后要关闭
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml"); 该语句引入了映射文件,还需要由映射文件去寻找sql语句所在的文件EmpMapper.xml 所以要在SqlMapConfig.xml中引入EmpMapper.xml
测试结果
对比数据库
成功!