JdbcTemplate实现增删改查操作

你的名字 2023-05-29 03:09 92阅读 0赞

JdbcTemplate介绍

为了使 JDBC 更加易于使用,Spring 在 JDBCAPI 上定义了一个抽象层, 以此建立一个JDBC存取框架,Spring Boot Spring Data-JPA。

作为 SpringJDBC 框架的核心, JDBC 模板的设计目的是为不同类型的JDBC操作提供模板方法. 每个模板方法都能控制整个过程,并允许覆盖过程中的特定任务。

通过这种方式,可以在尽可能保留灵活性的情况下,将 数据库存取的工作量降到最低。

JdbcTemplate方法介绍

JdbcTemplate主要提供以下五类方法:

1、execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句;

Execute、executeQuery、executeUpdate

2、update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句 SQL SERVCER(GO SQL语句 GO) ;

3、query方法及queryForXXX方法:用于执行查询相关语句;

4、call方法:用于执行存储过程、函数相关语句。

JdbcTemplate实现增删改查

JdbcTemplate添加数据

1. 使用配置实现
1.1 创建实体类

www.wityx.com www.wityx.com

  1. public class Account {
  2. private Integer accountid;
  3. private String accountname;
  4. private Double balance;
  5. public Integer getAccountid() {
  6. return accountid;
  7. }
  8. public void setAccountid(Integer accountid) {
  9. this.accountid = accountid;
  10. }
  11. public String getAccountname() {
  12. return accountname;
  13. }
  14. public void setAccountname(String accountname) {
  15. this.accountname = accountname;
  16. }
  17. public Double getBalance() {
  18. return balance;
  19. }
  20. public void setBalance(Double balance) {
  21. this.balance = balance;
  22. }
  23. }

实体类

1.2 创建Dao层

www.wityx.com www.wityx.com

  1. //查询所有所有账户
  2. public List<Account> getAllAccounts();

查询方法

.3 创建Dao层实现类并继承JdbcDaoSupport接口

www.wityx.com www.wityx.com

  1. public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
  2. @Override
  3. public List<Account> getAllAccounts() {
  4. JdbcTemplate jdbcTemplate = getJdbcTemplate();
  5. String sql = "select * from accounts";
  6. //RowMapper:接口 封装了记录的行映射关系
  7. List<Account> lists = jdbcTemplate.query(sql, new RowMapper<Account>() {
  8. @Override
  9. public Account mapRow(ResultSet resultSet, int i) throws SQLException {
  10. //创建Account对象
  11. Account account = new Account();
  12. //从ResultSet中解数据保到Accounts对象中
  13. account.setAccountid(resultSet.getInt("accountid"));
  14. account.setAccountname(resultSet.getString("accountname"));
  15. account.setBalance(resultSet.getDouble("balance"));
  16. return account;
  17. }
  18. });
  19. return account;
  20. }

实现查询方法

1.4创建Service层

www.wityx.com www.wityx.com

  1. //查询所有所有账户
  2. public List<Account> getAllAccounts();

查询方法

1.5创建Service层实现类

www.wityx.com www.wityx.com

  1. AccountDao accountDao;
  2. @Override
  3. public List<Account> getAllAccounts() {
  4. List<Account> allAccounts = accountDao.getAllAccounts();
  5. return allAccounts;
  6. }

实现查询方法

1.6 编写applicationContext.xml文件

www.wityx.com www.wityx.com

  1. <!--识别到配置文件-->
  2. <context:property-placeholder location="classpath:jdbc.properties"/>
  3. <!--配置数据源-->
  4. <!--spring内置的数据源:提供连接的,不负责管理,使用连接池-->
  5. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  6. <property name="driverClassName" value="${jdbc.driver}"></property>
  7. <property name="url" value="${jdbc.url}"></property>
  8. <property name="username" value="${jdbc.username}"></property>
  9. <property name="password" value="${jdbc.password}"></property>
  10. </bean>
  11. <!--构建jdbcTemplate-->
  12. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  13. <property name="dataSource" ref="dataSource"></property>
  14. </bean>
  15. <bean id="accountDao" class="cn.spring.accounttest.dao.impl.AccountDaoImpl">
  16. <property name="jdbcTemplate" ref="jdbcTemplate"></property>
  17. </bean>
  18. <!--Service-->
  19. <bean id="accountService" class="cn.spring.accounttest.service.impl.AccountServiceImpl">
  20. <property name="accountDao" ref="accountDao"></property>
  21. </bean>

applicationContext.xml

1.7编写测试类

www.wityx.com www.wityx.com

  1. @Test
  2. public void getAllAccount(){
  3. ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
  4. //从spring容器中获取Service对象
  5. AccountService accountService = (AccountService)context.getBean("accountService");
  6. List<Account> allAccounts = accountService.getAllAccounts();
  7. for (Account account:allAccounts) {
  8. System.out.println("账户名:"+account.getAccountname()+",余额为:"+account.getBalance());
  9. }
  10. }

查询测试类

2. 使用注解方式实现
2.1 创建实体类

www.wityx.com 实体类

2.2 创建Dao层

www.wityx.com 查询方法

2.3 创建Dao层实现类

www.wityx.com www.wityx.com

  1. @Repository
  2. public class AccountDaoImpl implements AccountDao {
  3. @Autowired
  4. private JdbcTemplate jdbcTemplate;
  5. Account account = new Account();
  6. @Override
  7. public List<Account> getAllAccounts() {
  8. String sql = "select * from accounts";
  9. //自动映射
  10. RowMapper<Account> rowMapper = new BeanPropertyRowMapper<>(Account.class);
  11. List<Account> query = jdbcTemplate.query(sql, rowMapper);
  12. for (Account account : query) {
  13. System.out.println(account);
  14. }
  15. return query;
  16. }
  17. }

Dao实现类

2.4创建Service层

www.wityx.com 查询方法

2.5创建Service层实现类

www.wityx.com 实现查询方法

2.6编写applicationContext.xml文件

www.wityx.com www.wityx.com

  1. <!--扫描注解:包扫描器-->
  2. <context:component-scan base-package="cn.spring"/>
  3. <!--识别到配置文件-->
  4. <context:property-placeholder location="classpath:jdbc.properties"/>
  5. <!--配置数据源-->
  6. <!--spring内置的数据源:提供连接的,不负责管理,使用连接池-->
  7. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  8. <property name="driverClassName" value="${jdbc.driver}"></property>
  9. <property name="url" value="${jdbc.url}"></property>
  10. <property name="username" value="${jdbc.username}"></property>
  11. <property name="password" value="${jdbc.password}"></property>
  12. </bean>
  13. <!--构建jdbcTemplate-->
  14. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  15. <property name="dataSource" ref="dataSource"></property>
  16. </bean>

applicationContext.xml

2.7编写测试类

www.wityx.com 查询测试类

JdbcTemplate实现增删改操作

使用注解方式实现,配置式同添加操作

1.创建Dao层

www.wityx.com www.wityx.com

  1. //删除账户
  2. public int delAccount(int id);
  3. //添加用户
  4. public int addAccount(Account account);
  5. //修改账户
  6. public int updaAccount(Account account);

增删改方法

2.创建Dao曾实现类

www.wityx.com www.wityx.com

  1. @Override
  2. public int delAccount(int id) {
  3. String sql="delete from accounts where accountid=2";
  4. int count = jdbcTemplate.update(sql);
  5. return count;
  6. }
  7. @Override
  8. public int addAccount(Account account) {
  9. String sql="insert into Accounts(accountname,balance) values(?,?)";
  10. int count = jdbcTemplate.update(sql,account.getAccountname(),account.getBalance());
  11. return count;
  12. }
  13. @Override
  14. public int updaAccount(Account account) {
  15. String sql="update accounts set accountname=?,balance=? where accountid=?";
  16. int count = jdbcTemplate.update(sql, account.getAccountname(),account.getBalance(),account.getAccountid() );
  17. return count;
  18. }

增删改方法实现类

3. 创建Service层

www.wityx.com 增删改方法

4. 创建Service层实现类

www.wityx.com 增删改方法实现类

5. 编写applicationContext.xml文件

www.wityx.com applicationContext.xml

6. 编写测试类

www.wityx.com www.wityx.com

  1. @Test
  2. public void delAccount(){
  3. ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
  4. AccountService accountService =(AccountService) context.getBean("accountServiceImpl");
  5. int i = accountService.delAccount(2);
  6. if (i>0){
  7. System.out.println("删除成功");
  8. }
  9. }
  10. @Test
  11. public void addAccount(){
  12. ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
  13. AccountService accountServiceImpl = (AccountService) context.getBean("accountServiceImpl");
  14. Account account=new Account();
  15. account.setAccountname("刘磊");
  16. account.setBalance(Double.valueOf(784));
  17. int count = accountServiceImpl.addAccount(account);
  18. if (count>0){
  19. System.out.println("添加成功");
  20. }
  21. }
  22. @Test
  23. public void updaAcccount(){
  24. ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
  25. AccountService accountServiceImpl = (AccountService) context.getBean("accountServiceImpl");
  26. Account account=new Account();
  27. account.setAccountid(10);
  28. account.setAccountname("刘磊");
  29. account.setBalance(Double.valueOf(784));
  30. int count = accountServiceImpl.updaAccount(account);
  31. if (count>0){
  32. System.out.println("修改成功");
  33. }
  34. }

增删改测试类

发表评论

表情:
评论列表 (有 0 条评论,92人围观)

还没有评论,来说两句吧...

相关阅读