• jdbc,hibernate,mybatis调用存储过程


    一,调用存储过程

      1 jdbc

    //jdbc 调用 有参的存储过程
        public static void doProcess(int id){
            System.out.println(conn);
            try {
                CallableStatement callableStatement= conn.prepareCall("{call querybyid(?)}");
                callableStatement.setInt(1, id);
                ResultSet rs=callableStatement.executeQuery();
                Book book=new Book();
                List<Book> lists=new ArrayList<Book>();
                while(rs.next()){
                    book.setId(rs.getInt("id"));
                    book.setImage(rs.getBlob("image"));
                    book.setName(rs.getString("name"));
                    book.setPage(rs.getInt("page"));
                    book.setTests(rs.getString("tests"));
                    lists.add(book);
                }
                System.out.println(lists.toString());
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

      2 hibernate

     <sql-query name="testProcess" callable="true">
            <return class="com.m01.bean.News">
                <return-property name="id" column="id"></return-property>
                <return-property name="title" column="title"></return-property>
                <return-property name="author" column="author"></return-property>
                <return-property name="date" column="date"></return-property>
                <return-property name="desc" column="desc"></return-property>
                <return-property name="price" column="price"></return-property>
            </return>
            {call queryNews()}
        </sql-query>
        @Test//测试存储过程
        public void doProcess(){
            Query query=session.getNamedQuery("testProcess");
            System.out.println(query.list());
        }

      3 mybatis

       在映射文件中编写 select语句 调用存储过程

    <!--  编写存储过程-->
         <select id="queryById" parameterMap="myMap" statementType="CALLABLE" resultType="com.m01.bean.User">
             call queryById(?)
         </select>
         <parameterMap type="java.util.Map" id="myMap">
             <parameter property="id_in" mode="IN" jdbcType="INTEGER"/>
         </parameterMap>
        @Test//测试存储过程
        public void testProcess(){
            Map map=new HashMap();
            map.put("id_in", 1);
            User user=session.selectOne("com.m01.bean.userMapper.queryById", map);
            System.out.println(user);
        }

    二,调用函数

      1 jdbc

    //jdbc 调用 有参的函数
            public static void doFunction(int id){
                System.out.println(conn);
                try {
                    CallableStatement callableStatement= conn.prepareCall("{?=call eById(?)}");
                    callableStatement.registerOutParameter(1, Types.VARCHAR);
                    callableStatement.setInt(2, id);
                    callableStatement.execute();
                    String name=callableStatement.getString(1);
                    System.out.println(name);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

      2 hibernate

        @Test//测试函数
        public void doFunction(){
        
            SQLQuery query= session.createSQLQuery("SELECT eById(?)");
            query.setParameter(0, 111);
            List list=query.list();
            System.out.println(list.toString());
        }

       3 mybatis

    <!-- 编写函数 -->
         <select id="queryPricebyId" statementType="CALLABLE"
         parameterType="java.util.Map">
             {#{var1,mode=OUT,javaType="double"}=call queryPricebyId(#{varid,mode=IN,javaType="java.lang.Integer"})}
         </select>
    @Test//测试自定义函数
        public void testFunction(){
            Map map=new HashMap();
            map.put("var1", null);
            map.put("varid", 1);
            session.selectOne("com.m01.bean.userMapper.queryPricebyId", map);
            System.out.println(map.get("var1"));
        }
  • 相关阅读:
    libevent源码学习之event
    游戏寻路A*算法
    游戏地图动态生成
    一个基于protocol buffer的RPC实现
    TCMalloc源码学习(四)(小内存块释放)
    TCMalloc源码学习(三)(小块内存分配)
    TCMalloc源码学习(二)
    第五十四篇 Linux相关——远程连接SSH
    第五十三篇 Linux相关——Web服务器
    第五十二篇 Linux相关——数据库服务MySQL
  • 原文地址:https://www.cnblogs.com/m01qiuping/p/6477083.html
Copyright © 2020-2023  润新知