In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.