Skeleton 之 Spring Boot 整合 JDBC Templete
作者:互联网
Spring Boot 整合 JDBC Templete
<!-- jdbc templete 启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
然后在 Dao 层添加 @Repository
注解,搞定!!!
打印执行的 SQL 语句
application.yml 文件增加一行配置
spring:
jpa:
show-sql: true
持久层编写
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
}
新增
@Override
public int add(User user) {
return jdbcTemplate.update("insert into tb_user(username, password, ctime) values(?, ?, ?)",
user.getUsername(), user.getPassword(), new Date());
}
更新
@Override
public int update(User user) {
return jdbcTemplate.update("update tb_user SET username = ? , password = ? WHERE id=?",
user.getUsername(), user.getPassword(), user.getId());
}
删除
@Override
public int delete(Long id) {
return jdbcTemplate.update("delete from tb_user where id = ? ", id);
}
单查询
@Override
public User getUserById(Long id) {
List<User> list = jdbcTemplate.query("select * from tb_user where id = ?", new Object[]{id}, new BeanPropertyRowMapper(User.class));
if (list != null && list.size() > 0) {
return list.get(0);
} else {
return null;
}
}
查询列表
@Override
public List<User> getUserList() {
List<User> list = jdbcTemplate.query("select * from tb_user", new Object[]{}, new BeanPropertyRowMapper(User.class));
if (list != null && list.size() > 0) {
return list;
} else {
return null;
}
}
标签:Templete,JDBC,return,Skeleton,list,public,user,id,jdbcTemplate 来源: https://blog.csdn.net/qq_38762237/article/details/100580831