1.dao
Public Interface Mapperselecteallempdao { public list<emp> all ();}
2. Implementation class
Public class extends Implements Mapperselecteallempdao { @Override public list<emp> All () { = " SELECT * from emp "; ListThisnew emprowmapper ()); return list; }}
3. Inherit the RowMapper interface and return the custom type
Public class Implements Rowmapper<emp> { @Override publicintthrows SQLException { New Emp (); Emp.setid (Rs.getint ("id")); Emp.setname (rs.getstring ("name")); Emp.setage (Rs.getint ("age")); return emp; }}
4. Testing
@Test // Spring uses RowMapper to query all records in a table Public void Rowmapperall () { new classpathxmlapplicationcontext ( " Applicationcontext.xml "); = (Mapperselecteallempdao) AC . Getbean ("Mapperall"); List<Emp> emplist = mapperselectall.all (); for (Emp emp:emplist) { System.out.println (Emp.getname ()); } }
Spring uses RowMapper to encapsulate each row in the data into a user-defined class