面向接口式的数据库操作
在mybatis中可以动态为我们生成dao接口的实现类
mybatis当中实际上使用了代理模式,在内存中生成dao接口的代理类,然后创建代理类的实例
使用mybatis的这种代理机制的前提:SqlMapper.xml文件中namespace必须是dao接口的全限定名称,id必须是dao接口中的方法名,mybatis-config中的mapper标签改为package标签,并指明mapper接口的包;mapper.xml文件需要放在同mapper接口相同的包路径下,文件名要与接口名相同
mybatis-config配置文件
<mappers>
<!-- 指定mapper接口的包 -->
<package name="com.atli.mapper"/>
</mappers>
工具类
public class SqlSessionUtil {
// 全局唯一的工厂对象
private static SqlSessionFactory sqlSessionFactory;
// 全局唯一的线程资源管理
private static ThreadLocal<SqlSession> local = new ThreadLocal<SqlSession>();
// 工厂对象是全局唯一的,在类加载时创建刚好满足需求
static{
String resource = "mybatis-config.xml";
try {
// 通过流读取配置文件并使用build方法创造工厂对象
sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader(resource));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static SqlSession getSqlSession(){
// 获取SqlSession时,若当前线程SqlSession为空就new一个并绑定在当前线程
if(local.get() == null){
local.set(sqlSessionFactory.openSession());
}
return local.get();
}
public static void closeSqlSession(){
if(local.get() != null){
local.get().close();
local.remove();
}
}
}
mapper层
public interface StudentMapper {
int addStudent(Student student);
int updateStudent(Student student);
int deleteStudent(int id);
Student getStudentById(int id);
List<Student> getAllStudent();
}
mapper配置文件
<!-- 放在resources/com/atli/mapper目录下,且命名为StudentMapper -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 命名空间是使用的mapper接口的全类名 -->
<mapper namespace="com.atli.mapper.StudentMapper">
<!-- 方法id要与mapper接口中方法名相同 -->
<insert id="addStudent" parameterType="com.atli.pojo.Student">
<!-- 所插入的变量名要与mapper接口中变量名相同 -->
insert into student (stu_id, stu_name, stu_age, classes_id) value (#{stuId},#{stuName},#{stuAge},#{classesId})
</insert>
<update id="updateStudent" parameterType="com.atli.pojo.Student">
update student set stu_name=#{stuName}, stu_age=#{stuAge}, classes_id=#{classesId}
</update>
<delete id="deleteStudent">
delete from student where stu_id=#{stuId}
</delete>
<select id="getStudentById" resultType="com.atli.pojo.Student">
select * from student where stu_id=#{stuId}
</select>
<select id="getAllStudent" resultType="com.atli.pojo.Student">
select * from student
</select>
</mapper>
service实现类
public class StudentServiceImpl implements StudentService {
@Override
public int addStudent(Student student) {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
// SqlSession的getMapper提供了对于Mapper的一个代理,可以直接调用配置文件中sql语句
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
return mapper.addStudent(student);
}
@Override
public int updateStudent(Student student) {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
return mapper.updateStudent(student);
}
@Override
public int deleteStudent(int id) {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
return mapper.deleteStudent(id);
}
@Override
public Student getStudentById(int id) {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
return mapper.getStudentById(id);
}
@Override
public List<Student> getAllStudent() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
return mapper.getAllStudent();
}
}