SpringBoot使用JDBC實現增刪改查

1、項目分層java

2、鏈接數據庫mysql

https://blog.csdn.net/saytime/article/details/78963121 步驟1、引入鏈接數據庫的相關依賴spring

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.1.14</version>
</dependency>

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>8.0.15</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <version>1.18.6</version>
   <scope>provided</scope>
</dependency>

步驟2、配置數據庫鏈接池相關信息sql

步驟3、初始化數據庫鏈接數據庫

@Configuration
@PropertySource(value = "classpath:application.properties")
public class DataSourceConfiguration {

    @Bean(destroyMethod = "close", initMethod = "init")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        return druidDataSource;
    }

}

啓動報錯:app

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

解決方法:https://blog.csdn.net/anaini1314/article/details/71157791ide

步驟4、增刪改查spring-boot

http://www.javashuo.com/article/p-ypbkoqma-bw.htmlui

一、新增spa

/**
 * 新增用戶
 * @param user 用戶信息
 * @return
 */
@Override
public int add(User user) {
    String sql = "insert into sys_user (user_name,password,email,telephone) values(?,?,?,?)";
    int result = jdbcTemplate.update(sql, user.getUserName(),user.getPassword(),user.getEmail(),user.getTelephone());
    return result;
}

二、修改

/**
 * 修改用戶 用戶信息
 * @param user
 * @return
 */
@Override
public int update(User user) {
    String sql = "update sys_user set user_name =?,password=?,email=?,telephone=? where id = ?";
    int result = jdbcTemplate.update(sql, user.getUserName(),user.getPassword(),user.getEmail(),user.getTelephone(),user.getId());
    return result;
}

三、刪除

/**
 * 刪除用戶
 * @param id 用戶id
 * @return
 */
@Override
public int deleteById(String id) {
    String sql = "delete from sys_user where id = ?";
    int result = jdbcTemplate.update(sql, id);
    return result;
}

四、根據用戶id獲取用戶

/**
 * 根據用戶id獲取用戶
 * @param id 用戶id
 * @return
 */
@Override
public User getUserById(String id) {
    String sql="select * from sys_user where id=?";
    return jdbcTemplate.queryForObject(sql, new RowMapper<User>() {
        @Override
        public User mapRow(ResultSet rs, int rowNum) throws SQLException {
            return User.builder()
                    .id(rs.getInt(1))
                    .userName(rs.getString(2))
                    .password(rs.getString(3))
                    .email(rs.getString(4))
                    .telephone(rs.getString(5))
                    .build();
        }
    }, id);
}

注意: 一、接收參數的兩種方式 (1)參數在路徑上

(2)問號傳參

相關文章
相關標籤/搜索