Get the App
SLTechnology News&Howtos  ›  Development  › 

How to apply JDBC in Spring Framework

Shulou Source: shulou.com Published: 2022-06-02 23:01:04 09月25日 Update

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!

Tags: Framework application content writing method more problem configuration helpless for this no needless to say things reasons but for this the object that is skills data. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Microsoft Linux macOS Huawei