How to use JdbcTemplate for springBoot
This article mainly introduces how springBoot uses JdbcTemplate, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
SpringBoot uses JdbcTemplate
If jdbcTemplate is automatically injected through spring, application.properties can be used directly in other classes.
If you come out through new JdbcTemplate (), you need to configure DataSource yourself.
Automatic injection is as follows
Application.properties file
Spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTCspring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.type=com.alibaba.druid.pool.DruidDataSourceC3P0Adapter
UserDao
Package com.example.demo.dao;import com.example.demo.pojo.UserInfo;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.PreparedStatementCreator;import org.springframework.jdbc.core.RowMapper;import org.springframework.jdbc.support.GeneratedKeyHolder;import org.springframework.jdbc.support.KeyHolder;import org.springframework.stereotype.Repository;import javax.annotation.Resource;import java.sql.*;import java.util.ArrayList;import java.util.List;@Repositorypublic class UserDao {@ Resource private JdbcTemplate jdbcTemplate Public UserInfo createUser (UserInfo u) {String sql = "insert into user (name,address) values"; KeyHolder holder=new GeneratedKeyHolder (); jdbcTemplate.update (new PreparedStatementCreator () {public PreparedStatement createPreparedStatement (Connection conn) throws SQLException {PreparedStatement ps=conn.prepareStatement (sql, Statement.RETURN_GENERATED_KEYS); ps.setString (1, u.getName ()); ps.setString (2, u.getAddress ()); return ps;}}, holder); int insertId=holder.getKey (). IntValue (); u.setId (insertId); return u } public void createUserList () {String sql= "insert into user (name,address) values"; List batchArgs=new ArrayList (); batchArgs.add (new Object [] {"caoyc", "Beijing"}); batchArgs.add (new Object [] {"zhh", "Chongqing"}); batchArgs.add (new Object [] {"cjx", "Tianjin"}); jdbcTemplate.batchUpdate (sql, batchArgs);} public void deleteUser (int id) {String sql= "delete from user where id=?" JdbcTemplate.update (sql, new Object [] {id}, new int [] {java.sql.Types.INTEGER});} public void updateUser (UserInfo u) {String sql= "update user set name=? Where id=? "; jdbcTemplate.update (sql,new Object [] {u.getName (), u.getId ()});} public List queryUser (int id) {String sql=" select * from user where id=? "; / / RowMapper rowMapper = new BeanPropertyRowMapper (UserInfo.class); return jdbcTemplate.query (sql,new Object [] {id}, new UserRowMapper ());} class UserRowMapper implements RowMapper {public UserInfo mapRow (ResultSet res, int arg1) throws SQLException {UserInfo u=new UserInfo (); u.setId (res.getInt (" id ")) U.setName (res.getString ("name")); u.setAddress (res.getString ("address")); return u;}
Manual configuration is as follows
DriverManagerDataSource dataSource=new DriverManagerDataSource (); dataSource.setDriverClassName ("org.postgresql.Driver"); dataSource.setUrl ("jdbc:postgresql://127.0.0.1:5432/postgres"); dataSource.setUsername ("postgres"); dataSource.setPassword ("332578"); JdbcTemplate jdbcTemplate = new JdbcTemplate (dataSource)
Thank you for reading this article carefully. I hope the article "how to use JdbcTemplate with springBoot" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!