Spring JDBC Custom orm--sql Query corresponding Java data object

Source: Internet
Author: User

Target: Implement the following, passing SQL through Spring JDBC, querying the Java data Objects that meet the criteria:

Pojo:

 Public classZmtest { Publiczmtest () {//TODO auto-generated Constructor stub    }    PrivateInteger ID; PrivateString name;  PublicInteger getId () {returnID; }     Public voidsetId (Integer id) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }    }
zmtest

SQL:

Create Table  Number (3varchar2());
Table Structure
SELECT  from WHERE = ?
Query

Step:

One, custom mapping query

 PackageSpringjdbc;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Types;ImportJavax.sql.DataSource;ImportOrg.springframework.jdbc.core.SqlParameter;ImportOrg.springframework.jdbc.object.MappingSqlQuery; Public classCustomermappingqueryextendsMappingsqlquery { PublicCustomermappingquery (DataSource ds) {Super(DS, "SELECT ID, name from zm_test_sjdbc WHERE id =?");//WHERE id =?        Super. Declareparameter (NewSqlParameter ("id", Types.integer));    Compile (); }     PublicZmtest Maprow (ResultSet RS,intRowNumber)throwsSQLException {zmtest zmtest=Newzmtest (); Zmtest.setid (integer.valueof (Rs.getobject ("id"). toString ())); Zmtest.setname (Rs.getstring ("Name")); returnzmtest; }    }
Customermappingquery

Second, the test class

Importjava.util.ArrayList;Importjava.util.List;ImportJavax.sql.DataSource;ImportOrg.springframework.jdbc.datasource.DriverManagerDataSource; Public classGet_cmd_log { PublicGet_cmd_log () {} Public Static voidMain (string[] args) {Get_cmd_log GCL=NewGet_cmd_log (); Gcl.getdata (1); }        //Get Datasource     PublicDataSource Getdatasource () {drivermanagerdatasource DS=NewDrivermanagerdatasource (); Ds.setdriverclassname ("Oracle.jdbc.driver.OracleDriver"); Ds.seturl ("Jdbc:oracle:thin: @ip:p ort:name"); Ds.setusername ("*******"); Ds.setpassword ("*******"); returnds; } @SuppressWarnings ("Unchecked")     PublicList<zmtest>GetData (Integer id) {Get_cmd_log GCL=NewGet_cmd_log (); DataSource Dsource=Gcl.getdatasource (); Customermappingquery Query=Newcustomermappingquery (Dsource); Object[] Params=NewObject[1]; params[0] =ID; List Reszm=Query.execute (params); Zmtest zTest=Newzmtest (); ZTest= (zmtest) (Reszm.get (0));        System.out.println (Ztest.getname ()); returnReszm; }}
Get_cmd_log

Test result: Gets the name of the corresponding ID.

Principle:

The bottom layer is implemented through JdbcTemplate.

Application Scenarios:

Simple Object query: such as login.

Advantages:

No need to improve efficiency through third-party ORM frameworks.

Tips:

The Oracle number type corresponds to the Java type:

Http://www.cnblogs.com/codegod/p/6826133.html

Spring JDBC Custom orm--sql Query corresponding Java data object

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.