MyBatis中如何通过继承SqlSessionDaoSupport来编写DAO(一)

packagecom.abc.dao.base;importorg.mybatis.spring.SqlSessionTemplate;importorg.mybatis.spring.support.SqlSessionDaoSupport;importorg.springframework.beans.factory.annotation.Autowired;publicclassBaseDaoextendsSqlSessionDaoSupport{@AutowiredpublicvoidsetSqlSessionTemplate(SqlSessionTemplatesqlSessionTemplate){super.setSqlSessionTemplate(sqlSessionTemplate);}}packagecom.abc.mapper;importcom.abc.domain.Student;publicinterfaceStudentMapper{//根据id查找学生publicStudentgetById(intid);//添加一名学生publicintadd(Studentstudent);//修改学生publicintupdate(Studentstudent);//删除学生publicintdelete(intid);}privateStudentMapperstudentMapper=this.getSqlSession().getMapper(StudentMapper.class);packagecom.abc.dao;importorg.springframework.stereotype.Repository;importcom.abc.dao.base.BaseDao;importcom.abc.domain.Student;importcom.abc.mapper.StudentMapper;@RepositorypublicclassStudentDaoextendsBaseDao{privateStudentMapperstudentMapper;publicStudentgetById(intid){returnthis.studentMapper.getById(id);}publicvoiddeleteById(intid){intcount=this.studentMapper.delete(id);System.out.println(“删除了”+count+”行数据。”);}publicvoidupdate(Studentstudent){intcount=this.studentMapper.update(student);System.out.println(“修改了”+count+”行数据。”);}publicvoidadd(Studentstudent){//TODOAuto-generatedmethodstubintcount=this.studentMapper.add(student);System.out.println(“添加了”+count+”行数据。”);}//对studentMapper进行初始化的方法publicvoidinit(){System.out.println(“初始化studentMapper…”);this.studentMapper=this.getSqlSession().getMapper(StudentMapper.class);}}packagecom.abc.dao.base;importorg.mybatis.spring.SqlSessionTemplate;importorg.mybatis.spring.support.SqlSessionDaoSupport;importorg.springframework.beans.factory.annotation.Autowired;publicabstractclassBaseDaoextendsSqlSessionDaoSupport{@AutowiredpublicvoidsetSqlSessionTemplate(SqlSessionTemplatesqlSessionTemplate){super.setSqlSessionTemplate(sqlSessionTemplate);}//抽象方法publicabstractvoidinit();}

StudentDao的代码不用改动,而bean后处理器的代码如下:

packagecom.abc.post;importorg.springframework.beans.BeansException;importorg.springframework.beans.factory.config.BeanPostProcessor;importcom.abc.dao.base.BaseDao;publicclassDaoPostProcessorimplementsBeanPostProcessor{@OverridepublicObjectpostProcessAfterInitialization(Objectbean,StringbeanName)throwsBeansException{//TODOAuto-generatedmethodstub//只处理BaseDao的子类的对象if(bean.getClass().getSuperclass()==BaseDao.class){BaseDaodao=(BaseDao)bean;dao.init();}//返回原bean实例returnbean;}@OverridepublicObjectpostProcessBeforeInitialization(Objectbean,StringbeanName)throwsBeansException{//TODOAuto-generatedmethodstub//直接返回原bean实例,不做任何处理returnbean;}}<beanclass=”com.abc.post.DaoPostProcessor”/>

对象并调用了相关的方法,具体请参考StudentService、StudentDao的代码,以及Spring中context:component-scan的相关配置。本示例完整源代码与数据库脚本下载地址:):

packagecom.demo;importorg.springframework.context.ApplicationContext;importcom.abc.service.StudentService;importcom.abc.domain.Student;importorg.springframework.context.support.ClassPathXmlApplicationContext;publicclassTestDaoSupport{privatestaticApplicationContextctx;static{//在类路径下寻找spring主配置文件,启动spring容器ctx=newClassPathXmlApplicationContext(“classpath:/applicationContext.xml”);}publicstaticvoidmain(String[]args){//从Spring容器中请求服务组件StudentServicestudentService=(StudentService)ctx.getBean(“studentService”);Studentstudent=studentService.getById(13);System.out.println(student.getName());}}

(本文示例完整源代码与数据库脚本下载地址:)

MyBatis技术交流群:188972810,,或扫描二维码:

本文出自 “肖凡的专栏” 博客,请务必保留此出处

梦想,并不奢侈,只要勇敢地迈出第一步。

MyBatis中如何通过继承SqlSessionDaoSupport来编写DAO(一)

相关文章:

你感兴趣的文章:

标签云: