How to apply JDBC in Spring Framework
How to carry out the JDBC application in Spring framework, I believe that many inexperienced people do not know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
First, look at the jdbctemplate configuration.
Needless to say, this dataSource uses the same data source as Hibernate.
DAO layer writing method
Service layer writing method
Spring configuration is that simple.
Of course, in DAO layer programs, we usually use an object wrapper.
Import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.support.JdbcDaoSupport; import com.fruitking.dao.jdbcdao.IAccessAnalyserDao; import com.fruitking.entity.AccessAnalyser; public class AccessAnalyserDaoImpl extends JdbcDaoSupport implements IAccessAnalyserDao {public List countGroupByYear () {String sql = "select to_char (t.createddate, 'yyyy') as cyear, count (*) as yearcount from accessanalyser t group by to_char (t.createddate,' yyyy')" List accessAnalyserList = this.getJdbcTemplate (). Query (sql, new AccessAnalyserRowMapper ()); return accessAnalyserList;} class AccessAnalyserRowMapper implements RowMapper {public Object mapRow (ResultSet rs, int rowNum) throws SQLException {AccessAnalyser accessAnalyser = new AccessAnalyser (); accessAnalyser.setYearName (rs.getString ("cyear")); accessAnalyser.setClickTimes (rs.getLong ("yearcount")); return accessAnalyser;}
In this way, you can use JDBC in the Spring framework like Hibernate, but you can use JDBC things at will.
After reading the above, have you mastered how to apply JDBC in the Spring framework? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!