< 16 >jdbc_ using dbutils to write generic DAO

Source: Internet
Author: User
Tags stub

Interface: Dao<t>.java

Import java.sql.Connection;
Import java.sql.SQLException;
Import java.util.List;

/*
* DAO interface for accessing data.
* @param types of entity classes processed by T:dao
* */
Public interface Dao<t> {

/*
* Method of batch Processing
* @param con: Database connection
* @param sql:sql statement
* @param args: Variable parameters for object[] types filled with placeholders
* @return
* */
void Batch (Connection con,string Sql,object[]...args);

/*
* Returns a specific value, eg. total number of people, average salary ...
* @param con: Database connection
* @param sql:sql statement
* @param args: Variable parameters for fill placeholders
* @return
* */
<E> E getforvalue (Connection con,string Sql,object...args);

/*
* Returns a collection of T
* @param con: Database connection
* @param sql:sql statement
* @param args: Variable parameters for fill placeholders
* @return
* */
List<t> getforlist (Connection con,string Sql,object...args);

/*
* Returns an object of T
* @param con: Database connection
* @param sql:sql statement
* @param args: Variable parameters for fill placeholders
* @return
* */
T Get (Connection con,string Sql,object...args) throws SQLException;

/*
* INSERT, UPDATE, delete
* @param con: Database connection
* @param sql:sql statement
* @param args: Variable parameters for fill placeholders
* */
void Update (Connection con,string Sql,object...args);
}

DAO interface Implementation class: Jdbcdaoimpl.java

Import java.sql.Connection;
Import java.sql.SQLException;
Import java.util.List;
Import Org.apache.commons.dbutils.QueryRunner;
Import Org.apache.commons.dbutils.handlers.BeanHandler;
Import Com.kk.jdbc.ReflectionUtils;

/*
* Use Queryrunner to provide its specific implementation
* @param <t>: Subclasses need to pass in a generic type
* */
public class Jdbcdaoimpl<t> implements dao<t> {

Private Queryrunner Qr=null;
Private class<t> type;

Public Jdbcdaoimpl () {

Qr=new Queryrunner ();
Type=reflectionutils.getsupergenerictype (GetClass ());
}

@Override
public void Batch (Connection con, String sql, object[] ... args) {
TODO auto-generated Method Stub

}

@Override
Public <E> E getforvalue (Connection con, String sql, Object ... args) {
TODO auto-generated Method Stub
return null;
}

@Override
Public list<t> getforlist (Connection con, String sql, Object ... args) {
TODO auto-generated Method Stub
return null;
}

@Override
Public T get (Connection con, String sql, Object ... args) throws SQLException {

Return qr.query (Con, SQL, New beanhandler<> (type), args);
}

@Override
public void update (Connection con, String sql, Object ... args) {
TODO auto-generated Method Stub
}
}

Sub-class of Jdbcdaoimpl Customerdao.java

public class Customerdao extends Jdbcdaoimpl<customer> {

}

Test class: Customerdaotest.java

Import static org.junit.assert.*;
Import java.sql.Connection;
Import Org.junit.Test;
Import Com.kk.jdbc.JDBCTools;

public class Customerdaotest {

Customerdao customerdao=new Customerdao ();

@Test
public void Testbatch () {
Fail ("not yet implemented");
}

@Test
public void Testgetforvalue () {
Fail ("not yet implemented");
}

@Test
public void Testgetforlist () {
Fail ("not yet implemented");
}

@Test
public void Testget () {

Connection con = null;
try {

Con=jdbctools.getconnection ();
String sql = "Select Id,name,email from Customers where id=?";
Customer customer=customerdao.get (Con, SQL, 6);
SYSTEM.OUT.PRINTLN (customer);
} catch (Exception e) {
E.printstacktrace ();
}finally{
Jdbctools.release (null, NULL, con);
}
}

@Test
public void Testupdate () {
Fail ("not yet implemented");
}
}

< 16 >jdbc_ using dbutils to write generic DAO

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.