zoukankan      html  css  js  c++  java
  • 大分享-hibernate,springmvc,easyui简要介绍

      近期公司一直在做项目,主要用到了springMVC,eseayui,hibernate几大框架。近一个月的时间,个人就目前自我知识给予分享。

    很多公司使用mybatis产品,综合所述其最大优点是全SQL操作,灵活方便。

    个人认为hibernate框架其优势比较突出。其一是因为它作为纯ORM产品,使用注解或是配置文件做好与数据表映射之后,操作实体就相当于操作数据表。而对于普通业务,多数是基于增删改操作的,较为复杂的可能就是查询操作了。实体映射关系太过于复杂容易引发牵一动百的效果,而业务逻辑中必须要这么做。那么,SQL就是最好的选择了。

      hibernate 中提供了org.hibernate.Query 接口。由 org.springframework.orm.hibernate3.support.HibernateDaoSupport.getSession() 方法返回的Sesson创建一个 

    org.hibernate.Session.createSQLQuery(String arg0) throws HibernateException 方法,该方法完全以SQL方式操作数据库表,抛开了实体bean的映射。
    完整代码如下:
    //查找ID
    public
    String findTypeIdByTitle(String title) throws Exception { String sql = "select pk_id from decoration_type_info where title =:title"; Query query = this.getSession().createSQLQuery(sql); query.setString("title", title); String pkId = (String) query.uniqueResult(); return pkId; }
    //更新方法
    public void updateTypeById(DecorationTypeInfo typeInfo) throws Exception{
            String sql = "update decoration_type_info set title =:title,REMARK =:remark,UPDATE_BY =:updateBy,"
                    + " UPDATE_DATE = to_timestamp(to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'), 'yyyy-mm-dd hh24:mi:ss:ff') "
                    + " where pk_id =:pkId";
            Query query = this.getSession().createSQLQuery(sql);
            query.setString("title", typeInfo.getTitle());
            query.setString("remark", typeInfo.getRemark());
            query.setString("updateBy", typeInfo.getUpdateBy());
            query.setString("pkId", typeInfo.getPkId());
            query.executeUpdate();
        }
    //删除方法
    public void delTypeById(String pkId)throws Exception {
            String sql = " delete from unit_decoration_type_info where fk_decoration_type_id=?";
            Query query = this.getSession().createSQLQuery(sql);
            query.setParameter(0, pkId);
            query.executeUpdate();
        }
    //新增方法
    public void addType(DecorationTypeInfo typeInfo,String themeId)throws Exception{
            if (StringUtils.isBlank(typeInfo.getPkId())) {
                String id = GeneratePKID.getPKID();
                typeInfo.setPkId(id);
            }
    
            String sql = "insert into decoration_type_info (PK_ID,TITLE,PRICE,REMARK,FK_THEME_ID,CREATE_BY,CREATE_DATE,UPDATE_BY,UPDATE_DATE) "
                    + " values (?,?,?,?,?,?,to_timestamp(to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'), 'yyyy-mm-dd hh24:mi:ss:ff'),"
                    + " ?,NULL)";
            Query query = this.getSession().createSQLQuery(sql);
            query.setParameter(0, typeInfo.getPkId());
            query.setParameter(1, typeInfo.getTitle());
            query.setParameter(2, typeInfo.getPrice());
            query.setParameter(3, typeInfo.getRemark());
            query.setParameter(4, themeId);
            query.setParameter(5, typeInfo.getCreateBy());
            query.setParameter(6, typeInfo.getUpdateBy());
            
            query.executeUpdate();
        }
    //所有导入的Hibernate包
    
    import org.apache.commons.lang.StringUtils;
    import org.hibernate.Query;
    import org.hibernate.transform.Transformers;
    import org.springframework.stereotype.Repository;

                                             注明:以上只作为个人学习观点,不涉及任何商业或法律。

     再来说说springMVC。

    个人目前理解为 :

      controller中的@Controller()和@RequestMapping("/xx/xx")注解作为action请求的唯一标志,不容许一个项目中有重复出现。

      service中的@Service("xxx1")注解作为全局唯一的service标识。

      dao中的xxxImpl类中@Repository("xx1")注解作为全局某个DAO的唯一标识。

    在service服务层注入引用DAO需要@Autowired和@Qualifier("xx1")配合使用,这样便于更准确的找到引用的DAO。

    controller引用service同样使用@Autowired和@Qualifier("xxx1")作为查找的条件 。

    其优势总结为:AOP切面编程可以随意中任何一个类中引用其它包的资源,只要加上@Autowired自动装配注解和@Qualifier("")筛选注解即可达到一次编写,到处使用的目的。

        这样一来,我们只要编写一个通用业务处理的方法,那么中项目的任何地方都可以调用。很大程度上减轻了工作量和代码冗余量 。

    //Controller段代码
    
    @Controller()
    @RequestMapping("/xx/xxx")
    public class TypeController {
    
        @Autowired
        @Qualifier("typeservice")
        private TypeService typeService;
        
        private static Log log = LogFactory.getLog(TypeController.class);
    
        @RequestMapping("/list")
        public ModelAndView bookCycleInfoList(HttpServletRequest request,
                HttpServletResponse response) {
            ModelAndView view = new ModelAndView();
            try {
                view.setViewName("/bookCycleInfo/bookCycleInfo_list");
            } catch (Exception e) {
                log.error("TypeController.list : " + e.getMessage());
            }
            return view;
        }
    }
    //Service代码
    
    @Service("typeservice")
    public class TypeService {
    
        @Autowired
        @Qualifier("typedao")
        private TypeDao type;
    
        public List<TreeList> queryRoomTree(String estateId) throws Exception{
            try {
                return bookCycleInfoDao.queryRoomTree(estateId);
            } catch (Exception e) {
                throw e;
            }
        }
    //DAO中只有接口定义,本代码不做演示
    
    @Repository("typedao")
    public class TypeDaoImpl  {
    
        
        @Override
        public List findTypeList() throws Exception{
    
            try {
                            //代码体
                return null;
            } catch (Exception e) {
                throw e;
            }
        }
    }
            

                                          注明:作为初学者,该共享知识仅作为参考,不能保证完全准确性 。

     

    下面说说easyUI框架 。首先,easyUI框架是完全基于JS的,个人认为它是jquery和ajax的综合体 。

    其中包含很多界面控件和JS代码控制。常用的控件有datagrid,treegrid,Dialog,Menu,TextBox,NumberBox,DateTimeBox等等,ajax也非常简单,如下代码所示

    <script type="text/javascript">
    $(function() {
                var ids ="${editId}";
                $.ajax({
                            type : "post",
                            async : false,
                            url : "${pageContext.request.contextPath}/xx/xxx/findTypeById.action",
                            data : {
                                pkId : ids
                            },
                            dataType : "json",
                            success : function(obj) {
                                $("#pkId").textbox('setValue', obj[0].PKID);
                                $("#title").textbox('setValue', obj[0].TITLE);
                                $("#content").textbox('setValue', obj[0].CONTENT);
                            }
                        });
            });
    </script>

        更多easyUI知识,请看官网 http://www.jeasyui.com/ 和中文网 http://www.jeasyui.net/ 。

    个人感触:

         个人虽然已经有一年多的开发经验,但做的项目和写的代码为之甚少。进新公司一个月了快,虽然累,但学到了很多可用实用的东西。

    所以非常开心,工作之余分享下自己的成果,既是对自我的检验,也是对雏步难行阶段学者的帮助 。

  • 相关阅读:
    实验四 数据库安全设计
    对订单数据库进行查询等操作
    vue学习笔记7 -组件之 父子组件之间的访问
    vue学习笔记6 父子组件的通信之 子组件修改由父组件传递过来的值 [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed pro
    vue学习笔记5 组件
    vue学习笔记4 v-mode
    vue学习笔记3 购物车 实例
    vue学习笔记2 实例学习
    vue学习笔记1 《Vue.js 前端开发 快速入门与专业应用》
    postman学习:如何写断言
  • 原文地址:https://www.cnblogs.com/rick168/p/4759031.html
Copyright © 2011-2022 走看看