Nutz Use of Custom SQL

Source: Internet
Author: User

Although the CND is provided, it is a bit inconvenient to use, and then the SQL statement is written directly. Nutz provides a number of methods.

The solution to the custom SQL section of Nutz.dao is:
// deprecated users can hardcode SQL statements, such as SQL SQL = Sqls.create ("DELETE from t_abc WHERE name= ' Peter '");

//Yes. Anti-injection. But too many strings, to splice. supports the notation of placeholders, such as SQL SQL = Sqls.create ("DELETE from $table WHERE [email protected]"), Sql.vars (). Set ("Table", "T_ABC") ; Sql.params (). Set ("Name", "Peter"), $table will be replaced with T_abc@name will be replaced with, to create PreparedStatement

//This method is good, the code looks good, and the convenience of CV operation The user can store all SQL statements in one or more files, and the interval of the statements can be commented, for example:/* Delete.data */delete from $table Where name like @name/* Update.data */update from $table SET [email protected] where [email protected] in your Java code: SQL SQL = Dao.sqls (). Create ("Delete.data"); You can customize the callback for your SQL, which will be explained in detail later

Based on Nutz's documentation, it is easier to manage code by using a method that is managed by placing SQL statements in multiple files.

Use the following method, when initializing, let DAO load the. sqls file. There are two ways.

The first, when constructed, is loaded. Path you can also use folders, which will load all the. sqls files under the folder

New Nutdao (DataSource,new Filesqlmanager ("Demo/sqls/all.sqls")); System.out.println (Dao.sqls (). Count ());

or write it in dao.js, and load it with the IOC.

var ioc = {        conf: {            ...        },        dataSource: {            ...        },        sqlsource:{            type: "Org.nutz.dao.impl.FileSqlManager",            args:["SQL"]        },        DAO: {            " Org.nutz.dao.impl.NutDao ",            args: [{refer:" DataSource "},{refer:" Sqlsource "}]        }};

Second, after the construction of the load

New Nutdao (DataSource);((Nutdao) dao). Setsqlmanager (new Filesqlmanager ("Demo/sqls/all.sqls") )); System.out.println (Dao.sqls (). Count ());

Ps:.sqls file must be UTF-8 encoded
--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------

Well, the pre-work is done. It's time to get the work done. First of all, write your own. sqls file.

1. Through Dao.sqls (). Create (""); Load the corresponding SQL statement.

2. Replace the corresponding placeholder.

3. Set the callback function, in the callback function, through the Invoke function you do the following.

4. Execute dao.execute (SQL). Here is the synchronization, which waits for the end until it fails, or the callback function executes before it continues.

@At @Ok ("Jsp:jsp.test.getMenuDi")     Public voidGETMENUBYIOC (HttpSession session) {SQL SQL= Dao.sqls (). Create ("Getmenu.data"); Sql.params (). Set ("id", 1); Sql.setcallback (NewSqlcallback () { PublicObject Invoke (Connection conn, ResultSet RS, SQL SQL)throwsSQLException {... while(Rs.next ()) {.... m.setname (rs.getstring ("Menu_name"));                ....                } System.out.println ("Callback End"); returnMyMenu;                }        });        Dao.execute (SQL); System.out.println ("Execute End"); MyMenu MyMenu= Sql.getobject (MyMenu.class); ...    }

Then, you can begin to write the code again.

Nutz Use of Custom SQL

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.