概述:关于spring-boot-starter-data-jdbc的看法
spring-boot-starter-data-jdbc使用起来很简单,就是简单的拼凑sql语句,通过JdbcTemplate 来调用。
第一步:引入依赖
依赖:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
<version>2.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.8.RELEASE</version>
</dependency>
第二步:配置文件信息
spring.datasource.url=jdbc:mysql://localhost:3306/cyl?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
第三步:编写操作代码
package com.cyl.TX2985.dao;
import com.cyl.TX2985.entity.MyUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
public JdbcTemplate jdbcTemplate;
@Override
public void addUser(MyUser user) {
String sql = "INSERT INTO user (uid, uname) VALUES (?, ?)";
jdbcTemplate.update(sql, user.getUid(), user.getUname());
}
@Override
public int delete(String text) {
jdbcTemplate.execute(text);
return 0;
}
public void save(String text,Object[] params ){
jdbcTemplate.update(text,params[0], params[1],params[2]);
System.out.println("what");
}
@Override
public List<MyUser> queryUsers() {
return null;
}
}