SPRINGMVC + Oracle Stored procedure architecture High Performance Java Web architecture

Source: Internet
Author: User
Tags eval stmt blank page java web

MVC makes simple business more complex

Do not know if you are using the MVC pattern development project, whether it is very troublesome, a simple delete and change check action but to involve multiple file operations.

An example of an increase in data is illustrated.


Suppose we use hibernate and the DAO layer is already encapsulated.

As you can see from the diagram, if we write an additional feature in the system that needs to be written by ourselves, at least JSP, action, Service,servicesimpl four layers.

If it's a complex addition, then we're probably going to define the DAO layer's interfaces and methods, which is the 6-tier operation.

If you use MyBatis to write at least 4 layers, it's also common to write 6 layers, because we need to write some SQL statements ourselves,

It's about writing mapper.xml and Mapper.java, so because it's 6 floors.

A simple action to write 4 or 6 layers I always think it's such a troublesome thing, and it's easy to make mistakes.

If one day this new action needs to add a field, then who is in the current is an egg pain action, add a field enough to make you sick half a day.

Let's see how many places we need to change.

1. Database

2, Pojo

3, DAO's configuration file

4, interface

You may also want to modify the action and other places. And the configuration file is prone to bugs ...

Anyway, I'm fed up with the days like this ...



Two-tier streamlined architecture

Later, after entering Hunan province, after the company framework, I reformed a 2-tier architecture using SPRINGMVC and Oracle stored procedures to implement the Web architecture, from which the gospel came

Let's look at a simplified flowchart.


We implement all of the business logic through the Oracle process, and then we just need to get the results of the process processed directly on the page by invoking the JavaScript library we've written.

So most of the business we have only 2 processing 2 places on it, the first write process in the Write interface call. Is it convenient? And the process data is much faster than MVC.

As for the interface display is also, the HTML interface reading speed is far greater than the JSP page, so performance is a great improvement.

And we don't have pojo here. So if you modify the field, you don't need to change anything, change the stored procedures and pages. The workload is one-fold less than that of MVC. That is to say, productivity has increased twice times.



The principle is that the diagram shows the following code to illustrate (with a functional management as an example):


Database Tables

--Create table
CREATE TABLE Ex_sys_power
(
ID INTEGER NOT NULL,
Icon_path VARCHAR2 (500),
Function_path VARCHAR2 (1000),
MARK VARCHAR2 (4000),
State INTEGER,
GRADE INTEGER,
parent_id INTEGER,
Yxbz INTEGER,
NAME VARCHAR2 (500)
)

  
Stored Procedures


CREATE OR REPLACE PACKAGE body A_es_power is


--============= Query by ID
PROCEDURE Find_power_byid (p_id INTEGER,
P_result out Plat_constant. RESULTSET) is
BEGIN
OPEN P_result for
SELECT *
From Ex_sys_power
WHERE ID = p_id
and YXBZ = 1;

End;

End;

  
HTML interface

<! DOCTYPE html>
<script type= "Text/javascript" src= ". /.. /public/hui/lib/jquery/1.9.1/jquery.min.js "></script>
<script type= "Text/javascript" src= ". /.. /public/exctscript/ajaxproxy.js "></script>
<title> Blank Page </title>
<body>
<a onclick= "Test ()" href= "javascript:void (0);" class= ' btn btn-success ' > Test Query </a>
<a onclick= "test2 ()" href= "javascript:void (0);" > Field Test </a>
<script type= "Text/javascript" >
function Test () {
var loj=new ajaxproxy ();
Loj.addparm (1, "1");
Loj.invoke ("SCOTT.") A_es_power. Find_power_byid ", function (Result) {
Alert (loj.getstring ("P_result", 1, "Icon_path"));
Alert (Loj.getrowcount ("P_result")); });
}

</script>
</body>

Ajaxprox.js

/**
* Author: Kang Youyao
*/

function Getrootpath () {
var curwwwpath = Window.document.location.href;
var pathName = Window.document.location.pathname;
Return curwwwpath.substring (0, Curwwwpath.indexof (pathName))
+ pathname.substring (0, Pathname.substr (1). IndexOf ('/') + 1);
}

var basepath = Getrootpath ();
document.write (' <script type= ' text/javascript ' src= ' + basepath
+ '/public/hui/lib/layer/1.9.3/layer.js ' ></script> ');

function Ajaxproxy (async) {

if (async!= null) {
This.async = async;
} else {
This.async = true;
}
This.gs_parameter = {};
This. state_success = ' 1 ';
This. State_err = ' 0 ';
This.result = null;
}

AjaxProxy.prototype.invoke = function (procedure, callback) {
var url = basepath + "/center.do?p_id=" + procedure;
var $this = this;
Layer.msg (' in load ', {
Icon:16
});
if (This.async) {//asynchronous
$.ajax ({
Type: "Post",
Url:url,
Data:this.gs_parameter,
Async:true,
Success:function (callBackData) {
var result = eval (callbackdata);
if (Result.requeststatus = = 0) {
Layer.closeall (' dialog ');
Layer.msg (Result.errmsg, {
Icon:2
});
Return
} else {
$this. result = result;
Layer.closeall (' dialog ');
Callback ();
}
}
});

else {//sync
$.ajax ({
Type: "Post",
Url:url,
Data:this.gs_parameter,
Async:false,
Success:function (callBackData) {
var result = eval (callbackdata);
if (Result.requeststatus = = 0) {
Layer.closeall (' dialog ');
Layer.msg (Result.errmsg, {
Icon:2
});
Return
} else {
$this. result = result;
Layer.closeall (' dialog ');
Callback ();
}
}
});

}

};

AjaxProxy.prototype.addParm = function (index, value) {
if (value!= null) {
Value + = "";
Value = encodeURIComponent (value);
this.gs_parameter["PARAM_" + index] = value;
}
};

AjaxProxy.prototype.getRowCount = function (mapname) {
Return eval ("This.result." + mapname + "[' Row_count ']");
};

AjaxProxy.prototype.getValue = function (key) {
Return eval ("This.result." + key);
};

AjaxProxy.prototype.getString = function (mapname, index, key) {
Return eval ("This.result." + Mapname + ". row_value[" + index + "[]" + Key
+ "']");
};



Java invoke Oracle Stored procedures

Steps:
1. Write Oracle Stored Procedures
2. Write Database get Connection tool class
3. Write simple application Call stored procedure


Realize:


1. Oracle Stored procedures:

/* Test Form * *
CREATE TABLE Test (
ID VARCHAR2 (32),
Name VARCHAR2 (32)
);

/* Stored Procedure Insert Data * *
CREATE OR REPLACE PROCEDURE insert_procedure (
PARA1 in VARCHAR2,
PARA2 in VARCHAR2
) as
BEGIN
INSERT into Test (ID, name) VALUES (PARA1, PARA2);
End Insert_procedure;

/* Stored procedure return result set * *
CREATE OR REPLACE PROCEDURE select_procedure (
para_id in VARCHAR2,
Name out sys_refcursor/* This sys_refcursor type is in Sys.standard package * *
) as
BEGIN
OPEN name for
SELECT * FROM Test WHERE id = para_id;
End;

2. JDBC Tool class

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;

public class Dbutil {
public static final String DRIVER = "Oracle.jdbc.driver.OracleDriver";
public static final String URL = "Jdbc:oracle:thin: @localhost: 1521/ORCL";
public static final String USERNAME = "PFM";
public static final String PASSWORD = "PFM";

/**
* Register database driver via static code block
*/
static {
try {
Class.forName (DRIVER);
catch (ClassNotFoundException e) {
E.printstacktrace ();
}
}

/**
* Get Connection
*
* @return
*/
public static Connection getconnection () {
Connection conn = null;
try {
conn = Drivermanager.getconnection (URL, USERNAME, PASSWORD);
catch (SQLException e) {
E.printstacktrace ();
}
Return conn;
}

/**
* Get Statement
*
* @return
*/
public static Statement Getstatement () {
Statement st = null;
try {
st = getconnection (). createstatement ();
catch (SQLException e) {
E.printstacktrace ();
}
Return St;
}

/**
* Close ResultSet
*
* @param RS
*/
public static void Closeresultset (ResultSet rs) {
if (Rs!= null) {
try {
Rs.close ();
catch (SQLException e) {
E.printstacktrace ();
}
}
}

/**
* Close Statement
*
* @param St
*/
public static void Closestatement (Statement st) {
if (St!= null) {
try {
St.close ();
catch (SQLException e) {
E.printstacktrace ();
}
}
}

/**
* Close Connection
*
* @param Conn
*/
public static void CloseConnection (Connection conn) {
IF (conn!= null) {
try {
Conn.close ();
catch (SQLException e) {
E.printstacktrace ();
}
}
}

/**
* Close All
*
* @param RS
* @param STA
* @param Conn
*/
public static void CloseAll (ResultSet rs, Statement STA, Connection conn) {
Closeresultset (RS);
Closestatement (STA);
CloseConnection (conn);
}

}

3. Call the stored procedure:

Import java.sql.CallableStatement;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import Oracle.jdbc.driver.OracleTypes;

/**
* Test Call stored Procedure
*
*/
public class Storedtest {
public static void Main (string[] args) {
Insert_call ();
Select_call ();
}

/**
* Execute stored procedure insert data
*/
public static void Insert_call () {
Connection conn = Dbutil.getconnection ();
PreparedStatement PST = NULL;

callablestatement proc = null; To create an object that executes a stored procedure
try {
proc = Conn.preparecall ("{Call Insert_procedure (?,?)}");
Proc.setstring (1, "1"); Set first input parameter
Proc.setstring (2, "Hello call"); Set first input parameter
Proc.execute ()//Execute

catch (SQLException e) {
E.printstacktrace ();
finally {
try {
Close IO Stream
Proc.close ();
Dbutil.closeall (NULL, PST, conn);
catch (Exception e) {
E.printstacktrace ();
}
}
}

/**
* Execute stored procedure query data
*/
public static void Select_call () {
Connection conn = Dbutil.getconnection ();

CallableStatement stmt;
try {
stmt = Conn.preparecall ("{Call Select_procedure (?,?)}"); Multiple-line syntax cannot be implemented with this calling method
Stmt.setstring (1, "1");
Stmt.registeroutparameter (2, oracletypes.cursor);
Stmt.execute ();
ResultSet rs = (ResultSet) stmt.getobject (2);
while (Rs.next ()) {
System.out.println (rs.getstring ("name"));
}
catch (SQLException e) {
E.printstacktrace ();
finally {
Dbutil.closeconnection (conn);
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.