1. Explain a
RowMapper can encapsulate each row in the data into a user-defined class, in a database query, if the type returned is a user-defined type, it needs to be wrapped, if it is a Java custom type, such as: string is not required, Spring's newest class simplejdbctemplate is much simpler to use.
Here's an example of how to use Rowmapp to download code from the Web, and it's OK to explain the problem. In the use of the process we can make the inner class Pojo external class, as long as the implementation of the RowMapper interface. If the user wants to define ApplicationContext, be cautious. After all, the implementation of RowMapper interface needs to add a Maprow method to a class, so that the class can withstand more functions, not conducive to the analysis system
Implement the RowMapper interface by building an inline class internally
Package Hysteria.contact.dao.impl; Import Java.sql.ResultSet; Import java.sql.SQLException; Import Java.sql.Types; Import java.util.List; Import Org.springframework.jdbc.core.JdbcTemplate; Import Org.springframework.jdbc.core.RowMapper; Import Hysteria.contact.dao.ItemDAO; Import Hysteria.contact.domain.Item; public class Itemdaoimpl implements Itemdao {private JdbcTemplate jdbctemplate; public void Setjdbctemplate (JdbcTemplate jdbctemplate) {this.jdbctemplate = JdbcTemplate; } Public Item Insert (item item) {String sql = ' INSERT into items (user_id,name,phone,email) VALUES (? ,?,?,?)"; object[] params = new object[] {Item.getuserid (), Item.getname (), Item.getphone (), Item.getemail ()}; int[] types = new int[] {types.integer, Types.varchar, Types.char, Types.varchar}; Jdbctemplate.update (SQL, params, types); return item; Public Item update (item item) {String sql = "Update items SET name =?, phone =?, email =?"} WHERE id =? "; object[] params = new object[] {item.getname (), Item.getphone (), Item.getemail (), Item.getid ()}; int[] types = new int[] {Types.varchar, Types.char, Types.varchar, Types.varchar, Types.intege R}; Jdbctemplate.update (SQL, params, types); return item; } public void Delete (item item) {String sql = ' Delete from items WHERE id =? '; object[] params = new object[] {item.getid ()}; int[] types = new int[] {Types.integer}; Jdbctemplate.update (SQL, params, types); Public Item FindByID (int id) {String sql = ' SELECT * from items WHERE id =? '; object[] params = new object[] {ID}; int[] types = new int[] {Types.integer}; LIST items = jdbctemplate.query (sql, params, types, New Itemmapper ()); if (Items.isempty ()) {return null; } return (Item) items.get (0); } public list<item> FindAll () {String sql = ' SELECT * from items '; return jdbctemplate.query (SQL, New Itemmapper ()); } public list<item> findallbyuser (int user_id) {String sql = ' SELECT * from Items WHERE user_ id =? "; object[] params = new object[] {user_id}; int[] types = new int[] {Types.integer}; List items = jdbctemplate.query (sql, params, types, New Itemmapper ()); return items; } protected class Itemmapper implements RowMapper {public Object Maprow (ResultSet rs, int rownu m) throws SQLException {Item item = new Item (); Item.setid (Rs.getint ("id")); Item.setuserid (Rs.getint ("user_id")); Item.setname (rs.getstring ("name")); Item.setphone (rs.getstring ("Phone")); Item.setemail (rs.getstring ("email")); return item; } } }
2. Explanation of the second
The RowMapper in sping can encapsulate each row of data in the data into a user-defined class.
In database queries, if the type returned is a user-defined type (in fact, most of the classes we return in a database query are custom-defined), we need to wrap it if it is a Java-custom type, such as a string.
If Sping is combined with hibernate, it is basically not available, and most of them are used when spring is used alone.
The RowMapper interface can be implemented by establishing an internal class, and there is a Maprow method in the RowMapper, so the RowMapper interface must implement the Maprow method, and the wrapper of the custom class is implemented in the Maprow method.
Here is a simple example:
public class Testdao {private JdbcTemplate JT; public void Setjt (JdbcTemplate JT) { THIS.JT = JT; } public list<tnpc> GetAll () { String sql = ' select * from T_NPC '; Use List list = Jt.query (sql, New Npcrowmapper ()); return list; }/** * Define inner class implementation RowMapper interface */public class Npcrowmapper implements rowmapper{ //Implement Maprow method public Object m Aprow (ResultSet rs, int num) throws SQLException { //classes are encapsulated TNpc NPC = new TNpc (); Npc.setid (Rs.getlong ("id")); Npc.setname (rs.getstring ("name")); return NPC; } }}