首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

(转)Spring集成MyBatis开展项目开发(二)

2013-12-29 
(转)Spring集成MyBatis进行项目开发(二)原文出处:http://wangjie2013.iteye.com/blog/1996151上一章Spring

(转)Spring集成MyBatis进行项目开发(二)

原文出处:http://wangjie2013.iteye.com/blog/1996151

上一章Spring集成MyBatis进行项目开发(一)已经介绍了spring和mybatis整合的配置,接下来是一个项目里面的部分代码:

? ? Application.java是下面会用到的一个实体bean:

?

Java代码??(转)Spring集成MyBatis开展项目开发(二)
  1. public?class?Application?{??
  2. ??
  3. ????public?static?final?int?APP_DISABLE?=?0;??
  4. ????public?static?final?int?APP_ENABLE?=?1;??
  5. ????private?Integer?id;??
  6. ????private?String?appAccount;//每个app对应一个账户标识;对应生成的数据表??
  7. ????private?String?appName;??
  8. ????private?String?appICON;??
  9. ????private?String?appDesc;??
  10. ????private?String?appURL;??
  11. ????private?Date?createTime;??
  12. ????private?int?isDisable;//'是否前台显示:0显示,1不显示'???
  13. }??

? ? getter 和setter略。

?

?

? ? 首先我们要编写一个与mapper.xml文件映射的接口文件,mybatis会将这个接口文件和对应的mapper文件中的sql语句关联,自动实现这个接口文件。在之后的开发中我们直接调用这个接口文件就可以了,因为内存中已经有接口相对应的实例了。

ApplicationsMapper.xml文件:

?

Xml代码??(转)Spring集成MyBatis开展项目开发(二)
  1. <?xml?version="1.0"?encoding="UTF-8"??>???
  2. <!DOCTYPE?mapper?PUBLIC?"-//mybatis.org//DTD?Mapper?3.0//EN"???
  3. ????"http://mybatis.org/dtd/mybatis-3-mapper.dtd">??
  4. ??
  5. <mapper?namespace="com.pinche.statistic.mapper.ApplicationsMapper">??
  6. ??
  7. ????<insert?id="add"?parameterType="Application"?useGeneratedKeys="true"??
  8. ????????keyProperty="id">??
  9. ????????INSERT?INTO?applications??
  10. ????????(appName,appAccount,appICON,appDesc,appURL,createTime)??
  11. ????????VALUES??
  12. ????????(#{appName},#{appAccount},#{appICON},#{appDesc},#{appURL},#{createTime})??
  13. ????</insert>??
  14. ??
  15. ????<delete?id="delete"?parameterType="String">??
  16. ????????DELETE?FROM?applications?WHERE??
  17. ????????appAccount?=?#{appAccount}??
  18. ????</delete>??
  19. ??
  20. ????<update?id="update"?parameterType="Application">??
  21. ????????UPDATE?applications??
  22. ????????<set>??
  23. ????????????<if?test="?appName?!=?''?and?appName?!=?null?">??
  24. ????????????????appName?=?#{appName},??
  25. ????????????</if>??
  26. ????????????<if?test="?appICON?!=?''?and?appICON?!=?null?">??
  27. ????????????????appICON?=?#{appICON},??
  28. ????????????</if>??
  29. ????????????<if?test="?appDesc?!=?''?and?appDesc?!=?null?">??
  30. ????????????????appDesc?=?#{appDesc},??
  31. ????????????</if>??
  32. ????????????<if?test="?appURL?!=?''?and?appURL?!=?null?">??
  33. ????????????????appURL?=?#{appURL},??
  34. ????????????</if>??
  35. ????????????<if?test="?isDisable?!=?-1?">??
  36. ????????????????isDisable?=?#{isDisable}??
  37. ????????????</if>??
  38. ????????</set>??
  39. ????????WHERE?appAccount?=?#{appAccount}??
  40. ????</update>??
  41. ??
  42. ????<select?id="findByAppAccount"?resultType="Application"??
  43. ????????parameterType="String">??
  44. ????????select?*?from?applications?where?appAccount?=??
  45. ????????#{appAccount}??
  46. ????</select>??
  47. ??
  48. ????<select?id="findAll"?resultType="Application">??
  49. ????????select?*?from?applications??
  50. ????</select>??
  51. </mapper>???

? ? 对ApplicationsMapper.xml文件的配置必须要注意的是它的命名空间是必须的,而且是对应接口文件的全名!并且每个sql语句的id属性和接口文件中的方法名一致!!

?

?

? ? 下面是ApplicationsMapper.java文件,也就是对应的接口文件:

?

Java代码??(转)Spring集成MyBatis开展项目开发(二)
  1. package?com.pinche.statistic.mapper;??
  2. ??
  3. import?java.util.List;??
  4. ??
  5. import?com.pinche.statistic.domain.Application;??
  6. ??
  7. public?interface?ApplicationsMapper?{??
  8. ??
  9. ????void?add(Application?app);??
  10. ??
  11. ????void?delete(String?appAccount);??
  12. ??
  13. ????void?update(Application?app);??
  14. ??
  15. ????Application?findByAppAccount(String?appAccount);??
  16. ??
  17. ????List<Application>?findAll();??
  18. }??

?

?

? ? 通过以上的的配置,大家可以在test中测试一下自己的代码了:

Java代码??(转)Spring集成MyBatis开展项目开发(二)
  1. @Test??
  2. public?void?testCreateTable()?{??
  3. ????ApplicationContext?aContext?=?new?FileSystemXmlApplicationContext("src/main/webapp/WEB-INF/applicationContext.xml");??
  4. ????ApplicationsMapper?mapper?=?(ApplicationsMapper)?aContext.getBean(ApplicationsMapper.class);??
  5. ??????
  6. ????Application?app?=?new?Application();??
  7. ????app.setAppAccount("androidApp");??
  8. ??????
  9. ????mapper.add(app);??
  10. }??

?

? ? 以上测试了add方法,其他的测试方法大家可以照着写一写。如果插入成功恭喜,你的环境已经搭好了。

?

?

? ??接下来是将继续这个样例系统的Dao层,service层和controller层。

? ??AppDao.java

?

Java代码??(转)Spring集成MyBatis开展项目开发(二)
  1. package?com.pinche.statistic.dao;??
  2. ??
  3. import?java.util.List;??
  4. ??
  5. import?com.pinche.statistic.domain.Application;??
  6. ??
  7. public?interface?AppDao?{??
  8. ????boolean?add(Application?app);??
  9. ??
  10. ????boolean?delete(String?appAccount);??
  11. ??
  12. ????boolean?update(Application?app);??
  13. ??
  14. ????Application?findByAppAccount(String?appAccount);??
  15. ??????
  16. ????List<Application>?findAll();??
  17. }??

?

? ? ?AppDaoImpl.java

?

?

Java代码??(转)Spring集成MyBatis开展项目开发(二)
  1. package?com.pinche.statistic.dao.impl;??
  2. ??
  3. import?java.util.List;??
  4. ??
  5. import?org.springframework.beans.factory.annotation.Autowired;??
  6. import?org.springframework.dao.DataAccessException;??
  7. import?org.springframework.stereotype.Repository;??
  8. ??
  9. import?com.pinche.statistic.dao.AppDao;??
  10. import?com.pinche.statistic.domain.Application;??
  11. import?com.pinche.statistic.mapper.ApplicationsMapper;??
  12. ??
  13. @Repository??
  14. public?class?AppDaoImpl?implements?AppDao?{??
  15. ??
  16. ????@Autowired??
  17. ????private?ApplicationsMapper?mapper;??
  18. ??????
  19. ????@Override??
  20. ????public?boolean?add(Application?app)?{??
  21. ????????try?{??
  22. ????????????mapper.add(app);??
  23. ????????????return?true;??
  24. ????????}?catch?(DataAccessException?e)?{??
  25. ????????????e.printStackTrace();??
  26. ????????}??
  27. ????????return?false;??
  28. ????}??
  29. ??
  30. ????@Override??
  31. ????public?boolean?delete(String?appAccount)?{??
  32. ????????try?{??
  33. ????????????mapper.delete(appAccount);??
  34. ????????????return?true;??
  35. ????????}?catch?(DataAccessException?e)?{??
  36. ????????????e.printStackTrace();??
  37. ????????}??
  38. ????????return?false;??
  39. ????}??
  40. ??
  41. ????@Override??
  42. ????public?boolean?update(Application?app)?{??
  43. ????????try?{??
  44. ????????????mapper.update(app);??
  45. ????????????return?true;??
  46. ????????}?catch?(DataAccessException?e)?{??
  47. ????????????e.printStackTrace();??
  48. ????????}??
  49. ????????return?false;??
  50. ????}??
  51. ??
  52. ????@Override??
  53. ????public?Application?findByAppAccount(String?appAccount)?{??
  54. ????????try?{??
  55. ????????????Application?findByAppAccount?=?mapper.findByAppAccount(appAccount);??
  56. ????????????return?findByAppAccount;??
  57. ????????}?catch?(DataAccessException?e)?{??
  58. ????????????e.printStackTrace();??
  59. ????????}??
  60. ????????return?null;??
  61. ????}??
  62. ??
  63. ????@Override??
  64. ????public?List<Application>?findAll()?{??
  65. ????????try?{??
  66. ????????????return?mapper.findAll();??
  67. ????????}?catch?(DataAccessException?e)?{??
  68. ????????????e.printStackTrace();??
  69. ????????}??
  70. ????????return?null;??
  71. ????}??
  72. }??

?

?

? ? 自行设计的DAO层对象容器(在DAO对象很多时,如果在service层要调用对应的DAO还得手动注入,通过引用这个DAO层对象容器,可以实现在需要使用DAO时迅速找需要的DAO,省去了繁杂的手动注入,而且spring默认的bean都是单例的,无论在何处注入一个实体bean其实都是同一个。这样做更方便):

?

Java代码??(转)Spring集成MyBatis开展项目开发(二)
  1. package?com.pinche.statistic.dao;??
  2. ??
  3. import?java.lang.reflect.Field;??
  4. ??
  5. import?javax.annotation.PostConstruct;??
  6. ??
  7. import?org.slf4j.Logger;??
  8. import?org.slf4j.LoggerFactory;??
  9. import?org.springframework.beans.factory.annotation.Autowired;??
  10. import?org.springframework.stereotype.Repository;??
  11. ??
  12. @Repository??
  13. public?class?BaseDAL?{??
  14. ??
  15. ????private?static?final?Logger?logger?=?LoggerFactory.getLogger(BaseDAL.class);??
  16. ??????
  17. ????@Autowired??
  18. ????private?AppDao?_appDao;??
  19. ????public?static?AppDao?appDao;??
  20. ??
  21. ????@Autowired??
  22. ????private?MetaDataDao?_metaDataDao;??
  23. ????public?static?MetaDataDao?metaDataDao;??
  24. ??
  25. ????@Autowired??
  26. ????private?DDLManager?_DDLManager;??
  27. ????public?static?DDLManager?DDLManager;??
  28. ??????
  29. ????@Autowired??
  30. ????private?AnalyzeDao?_analyzeDao;??
  31. ????public?static?AnalyzeDao?analyzeDao;??
  32. ??????
  33. ????@Autowired??
  34. ????private?DialstatisticsDao?_dialstatisticsDao;??
  35. ????public?static?DialstatisticsDao?dialstatisticsDao;??
  36. ??
  37. ????@PostConstruct??
  38. ????public?void?init()?{??
  39. ????????long?start?=?System.currentTimeMillis();??
  40. ????????logger.debug("start?init?BaseDAL?...");??
  41. ????????try?{??
  42. ??
  43. ????????????Field[]?fields?=?this.getClass().getDeclaredFields();??
  44. ??
  45. ????????????for?(int?i?=?0;?i?<?fields.length;?i++)?{??
  46. ????????????????String?fieldname?=?fields[i].getName();??
  47. ????????????????if?(fieldname.startsWith("_"))?{??
  48. ????????????????????String?sfieldname?=?fieldname.substring(1);??
  49. ????????????????????Field?sfield?=?this.getClass().getDeclaredField(sfieldname);??
  50. ????????????????????sfield.setAccessible(true);??
  51. ????????????????????sfield.set(this,?fields[i].get(this));??
  52. ????????????????}??
  53. ????????????}??
  54. ????????????logger.debug("init?BaseDAL?OVER,?consume?=?{}ms",??
  55. ????????????????????System.currentTimeMillis()?-?start);??
  56. ????????}?catch?(IllegalArgumentException?e)?{??
  57. ????????????e.printStackTrace();??
  58. ????????}?catch?(NoSuchFieldException?e)?{??
  59. ????????????e.printStackTrace();??
  60. ????????}?catch?(SecurityException?e)?{??
  61. ????????????e.printStackTrace();??
  62. ????????}?catch?(IllegalAccessException?e)?{??
  63. ????????????e.printStackTrace();??
  64. ????????}??
  65. ????}??
  66. }??

?

?

? ? 如果使用了以上的层管理容器,如果要在容器中添加一个DAO(例如:DemoDao),只需在这个容器中添加一个这样的声明:

?

Java代码??(转)Spring集成MyBatis开展项目开发(二)
  1. @Autowired??
  2. private?DemoDao?_demoDao;??
  3. public?static?DemoDao?demoDao;??

?

?

? ? 好了下面是Service层定义的接口:

? ??AppService.java

?

Java代码??(转)Spring集成MyBatis开展项目开发(二)
  1. package?com.pinche.statistic.service;??
  2. ??
  3. import?java.util.List;??
  4. ??
  5. import?com.pinche.statistic.domain.Application;??
  6. ??
  7. /**?
  8. ?*?@author?JACKWANG?
  9. ?*?@since?Dec?23,?2013?
  10. ?*/??
  11. public?interface?AppService?{??
  12. ??????
  13. ????Application?find(String?appAccount);??
  14. ??????
  15. ????boolean?update(Application?app);??
  16. ??????
  17. ????boolean?setDisable(String?appAccount);??
  18. ??????
  19. ????boolean?setEnable(String?appAccount);??
  20. ??????
  21. ????List<Application>?findAll();??
  22. ??????
  23. }??

?

?

? ??AppServiceImpl.java?: AppService的实现类:

?

Java代码??(转)Spring集成MyBatis开展项目开发(二)
  1. package?com.pinche.statistic.service.impl;??
  2. ??
  3. import?java.util.List;??
  4. ??
  5. import?org.slf4j.Logger;??
  6. import?org.slf4j.LoggerFactory;??
  7. import?org.springframework.stereotype.Service;??
  8. ??
  9. import?com.pinche.statistic.dao.BaseDAL;??
  10. import?com.pinche.statistic.domain.Application;??
  11. import?com.pinche.statistic.service.AppService;??
  12. import?com.pinche.statistic.utils.SystemUtils;??
  13. ??
  14. /**?
  15. ?*?@author?JACKWANG?
  16. ?*?@since?Dec?23,?2013?
  17. ?*/??
  18. @Service??
  19. public?class?AppServiceImpl?implements?AppService?{??
  20. ??
  21. ????private?static?final?Logger?logger?=?LoggerFactory??
  22. ????????????.getLogger(AppServiceImpl.class);??
  23. ??
  24. ????@Override??
  25. ????public?Application?find(String?appAccount)?{??
  26. ????????return?BaseDAL.appDao.findByAppAccount(appAccount);??
  27. ????}??
  28. ??
  29. ??????
  30. ????@Override??
  31. ????public?boolean?update(Application?app)?{??
  32. ????????String?appAccount?=?app.getAppAccount();??
  33. ????????if?(appAccount?==?null?&&?"".equals(appAccount))?{??
  34. ????????????return?true;??
  35. ????????}??
  36. ????????return?BaseDAL.appDao.update(app);??
  37. ????}??
  38. ??
  39. ????@Override??
  40. ????public?boolean?setDisable(String?appAccount)?{??
  41. ????????Application?app?=?new?Application();??
  42. ????????app.setAppAccount(appAccount);??
  43. ????????app.setIsDisable(Application.APP_DISABLE);??
  44. ????????return?BaseDAL.appDao.update(app);??
  45. ????}??
  46. ??
  47. ????@Override??
  48. ????public?boolean?setEnable(String?appAccount)?{??
  49. ????????Application?app?=?new?Application();??
  50. ????????app.setAppAccount(appAccount);??
  51. ????????app.setIsDisable(Application.APP_ENABLE);??
  52. ????????return?BaseDAL.appDao.update(app);??
  53. ????}??
  54. ??
  55. ????@Override??
  56. ????public?List<Application>?findAll()?{??
  57. ????????return?BaseDAL.appDao.findAll();??
  58. ????}??
  59. }??

? ?

? ? ?哈哈,使用层对象管理容器是不是很方便。通过一个引用就能获得所有的DAO支持。所以我在service层也构建了一个service层对象管理容器BaseBLL:

?

?

? ? BaseBLL.java:

?

Java代码??(转)Spring集成MyBatis开展项目开发(二)
  1. package?com.pinche.statistic.service;??
  2. ??
  3. import?java.lang.reflect.Field;??
  4. ??
  5. import?javax.annotation.PostConstruct;??
  6. ??
  7. import?org.slf4j.Logger;??
  8. import?org.slf4j.LoggerFactory;??
  9. import?org.springframework.beans.factory.annotation.Autowired;??
  10. import?org.springframework.stereotype.Service;??
  11. ??
  12. /**?
  13. ?*?@author?JACKWANG?
  14. ?*?@since?Dec?23,?2013?
  15. ?*/??
  16. @Service??
  17. public?class?BaseBLL?{??
  18. ??????
  19. ????private?static?final?Logger?logger?=?LoggerFactory.getLogger(BaseBLL.class);??
  20. ??????
  21. ????@Autowired??
  22. ????private?AnalyzeService?_analyzeService;??
  23. ????public?static?AnalyzeService?analyzeService;??
  24. ??
  25. ????@Autowired??
  26. ????private?AppService?_appService;??
  27. ????public?static?AppService?appService;??
  28. ??
  29. ????@Autowired??
  30. ????private?MetaDataService?_metaDataService;??
  31. ????public?static?MetaDataService?metaDataService;??
  32. ??
  33. ????@PostConstruct??
  34. ????public?void?init()?{??
  35. ????????long?start?=?System.currentTimeMillis();??
  36. ????????logger.debug("start?init?BaseBLL?...");??
  37. ????????try?{??
  38. ??
  39. ????????????Field[]?fields?=?this.getClass().getDeclaredFields();??
  40. ??
  41. ????????????for?(int?i?=?0;?i?<?fields.length;?i++)?{??
  42. ????????????????String?fieldname?=?fields[i].getName();??
  43. ????????????????if?(fieldname.startsWith("_"))?{??
  44. ????????????????????String?sfieldname?=?fieldname.substring(1);??
  45. ????????????????????Field?sfield?=?this.getClass().getDeclaredField(sfieldname);??
  46. ????????????????????sfield.setAccessible(true);??
  47. ????????????????????sfield.set(this,?fields[i].get(this));??
  48. ????????????????}??
  49. ????????????}??
  50. ????????????logger.debug("init?BaseBLL?OVER,?consume?=?{}ms",??
  51. ????????????????????System.currentTimeMillis()?-?start);??
  52. ????????}?catch?(IllegalArgumentException?e)?{??
  53. ????????????e.printStackTrace();??
  54. ????????}?catch?(NoSuchFieldException?e)?{??
  55. ????????????e.printStackTrace();??
  56. ????????}?catch?(SecurityException?e)?{??
  57. ????????????e.printStackTrace();??
  58. ????????}?catch?(IllegalAccessException?e)?{??
  59. ????????????e.printStackTrace();??
  60. ????????}??
  61. ????}??
  62. ??
  63. }??

? ?

?

?

? ? 好了下面应该是controller层的编写了,但是由于笔者以上的代码只是摘录了系统中的部分,而在controller中涉及到其他的内容,如果直接摘录可能和以上的代码衔接不上。所以这里就不进行了controller层的具体介绍了。本系统controller层使用的是SpringMVC,开发效率一级赞。

热点排行