java - How create BaseDAO+JDBC and use it like @Autowired variable? -
i used dao hibernate orm before , worked fine. need use dao jdbc , not understand how create single connection point.
i used this:
public interface basedao<e> { public e persist(e e); public e get(long id); e get(integer id); public list<e> getall(); e merge(e e); }
and realization
@repository @transactional(propagation = propagation.required) public abstract class basejpadao<e> implements basedao<e> { protected class<?> entityclass; @persistencecontext(unitname = "maindatabase") protected entitymanager entitymanager; public basejpadao(class<?> entityclass) { this.entityclass = entityclass; } @override public e persist(e e) { entitymanager.persist(e); return e; } @override public e get(long id) { return (e) entitymanager.find(entityclass, id); } @override public e get(integer id) { return (e) entitymanager.find(entityclass, id); } @override public list<e> getall(){ return getsession().createcriteria(entityclass).list(); } @override public e merge(e e){ checkfieldsfornull(e); return entitymanager.merge(e); }
but need use jdbc , use this:
public class basedao { private datasource datasource; private jdbctemplate jdbctemplate; public basedao(string servicename) throws namingexception { context envcontext = new initialcontext(); string jndiurl = messageformat.format("osgi:service/javax.sql.datasource/(osgi.jndi.service.name=jdbc/{0})", servicename); datasource = (datasource) envcontext.lookup(jndiurl); } public basedao(datasource datasource) { this.datasource = datasource; } }
and realization:
@repository public class subscriberdaoimpl extends basedao implements subscriberdao { public subscriberdaoimpl(string servicename) throws namingexception { super(servicename); } @override public list<subsdetail> getsubsdetils(long subsid, date startdate, date enddate) { return null; } }
and after want use dao class like:
public class subscriberprocessorimpl implements subscriberprocessor { @autowired subscriberdao subscriberdao; @override public string getsubscriberdetail(long id, date startdate, date enddate) { list<subsdetail> subsdetils = subscriberdao.getsubsdetils(id, startdate, enddate); return dataformatutil.me().prepareresponsejson(subsdetils, resultcode.success, ""); } }
but need pass parameter dao , not understand how it.
Comments
Post a Comment