SpringBoot持久化层操作支持技巧

论坛 期权论坛 脚本     
niminba   2021-5-23 05:17   1568   0

SpringBoot的持久化层可以是Spring内置的轻量级JdbcTemplate、也可以是Hibernate或Mybatis等等,只需要在在工程pom.xml文件中添加对应的依赖就可以了。

新建工程我们能发现,SpringBoot对数据库操作的支持有以下几种:

可见SpringBoot对各种的支持还是挺多的。

入正题。看看对SQL的支持。主要选了比较传统/流行/有前景的4个进行操作:

均是采用mysql

所以应该添加对mysql操作的依赖:

<!--MySQL-->
<dependency>
  <groupid>mysql</groupid>
  mysql-connector-java</artifactid>
  <scope>runtime</scope>
</dependency>

同时,需要对web进行支持,添加web相关依赖

<!--web支持-->
<dependency>
  <groupid>org.springframework.boot</groupid>
  spring-boot-starter-web</artifactid>
</dependency>

需要在application.properties中配置mysql相关信息(也可以使用*.yml)。配置如下:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url= jdbc:mysql://localhost:3306/springboottest?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root

实际应用的时候,需要添加数据库连接池,为了方便,就暂时不添加了。

1. SpringBoot用JdbcTemplates访问Mysql

首先需要添加对JdbcTemplates的依赖,可以在新建工程中点击添加,或手动添加

<!--JDBC支持-->
<dependency>
  <groupid>org.springframework.boot</groupid>
  spring-boot-starter-jdbc</artifactid>
</dependency>

目录结构如上,代码如下:

@Data
public class Account {
  private int id ;
  private String name ;
  private double money;
}

这里是安装了一个插件,然后只需要添加如下依赖,添加@Data注解,就会帮我们自动生成getter和setter,也可以用@getter或@Setter注解进行标注。

<dependency>
  <groupid>org.projectlombok</groupid>
  lombok</artifactid>
</dependency>

Dao层:

public interface IAccountDao {
  int add(Account account);
  int update(Account account);
  int delete(int id);
  Account findAccountById(int id);
  Account selectAccountById(int id);
 
  List findAccountList();
}</account>

实现类:

@Repository
public class AccountDaoImpl implements IAccountDao{
 
  @Autowired private JdbcTemplate jdbcTemplate;
 
  @Override
  public int add(Account account) {
    return jdbcTemplate.update("INSERT INTO account(name,money) VALUES(?,?)",
        account.getName(),account.getMoney());
  }
 
  @Override
  public int update(Account account) {
    return jdbcTemplate.update("UPDATE account SET name=?,money=? WHERE id=?",
        account.getName(),account.getMoney(),account.getId());
  }
 
  @Override
  public int delete(int id) {
    return jdbcTemplate.update("DELETE FROM TABLE account WHERE id=?", id);
  }
 
  @Override
  public Account findAccountById(int id) {
    List list = jdbcTemplate.query("SELECT * FROM account WHERE id = ?",
        new Object[]{id}, new BeanPropertyRowMapper(Account.class));
    if(list!=null && list.size()>0){
      Account account = list.get(0);
      return account;
    }else{
      return null;
    }
  }
  @Override
  public Account selectAccountById(int id){
 
    return jdbcTemplate.queryForObject("SELECT * FROM account WHERE id = ?", new RowMapper() {
      @Override
      public Account mapRow(ResultSet resultSet, int i) throws SQLException {
        Account account = new Account();
        account.setId(resultSet.getInt("id"));
        account.setName(resultSet.getString("name"));
        account.setMoney(resultSet.getDouble("money"));
        return account;
      }
    },id);
  }
 
  @Override
  public List findAccountList() {
    List list = jdbcTemplate.query("SELECT * FROM account",
        new Object[]{}, new BeanPropertyRowMapper(Account.class));
    if(list!=null && list.size()>0) return list;
    else return null;
  }
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1060120
帖子:212021
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP