73、SpringおよびDAO操作update()

update()メソッドは、データの挿入、更新、および削除の操作を完了することができます。JdbcTemplateクラスには、一連のupdate()メソッドが用意されており、一般的に使用されるメソッドを次の表に示します。

方法 説明する
int update(String sql) このメソッドは、updateメソッドの最も単純なオーバーロードであり、着信SQLステートメントを直接実行し、影響を受ける行数を返します。
int update(PreparedStatementCreatorpsc) このメソッドは、PreparedStatementCreatorから返されたステートメントを実行してから、影響を受けた行の数を返します。
int update(String sql、PreparedStatementSetter pss) このメソッドは、PreparedStatementSetterを介してSQLステートメントのパラメーターを設定し、影響を受ける行の数を返します。
int update(Stringsql、Object ... args) このメソッドは、Object ...を使用してSQLステートメントにパラメーターを設定します。パラメーターをNULLにすることはできず、影響を受ける行数を返します。

フォアグラウンド接続

72のSpringおよびDAO操作execute()は、JDBCテンプレートオブジェクトをDaoレイヤーに挿入する実装クラスです。Springのトランザクション管理はAOPの適用であり、トランザクションは一面としてサービス層のビジネス手法に織り込まれています。SpringおよびJDBCテンプレートJDBCを直接使用することによってもたらされる複雑で長いコードを回避するために、Springは強力なテンプレートクラスを提供します--- JdbcTe ... https://blog.csdn.net/m0_54925305/article/details / 123149019? spm = 1001.2014.3001.5501

この場合、実行関数のプログラム関数を拡張します

DBの追加、削除、および変更は、update()メソッドによって実現されます。このメソッドには、一般的に使用される2つのオーバーロードメソッドがあります。

  • public int update(String sql)
  • public int update(String sql、Object…args)

        最初のパラメーターは実行されるsqlステートメントであり、2番目のパラメーターは実行されるsqlステートメントに含まれる動的パラメーターです。その戻り値は、影響を受けるレコードの数です。一般的にはそうではありません。

 

 

ケース操作

1.インターフェースを作成します

import java.util.List;

public interface AccountDao {
	// 添加
	public int addAccount(Account account);

	// 更新
	public int updateAccount(Account account);

	// 删除
	public int deleteAccount(int id);
}

2.実装クラスを作成します

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

public class AccountDaoImpl implements AccountDao {
	// 声明JdbcTemplate属性及其setter方法
	private JdbcTemplate jdbcTemplate;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	// 添加账户
	public int addAccount(Account account) {
		// 定义SQL
		String sql = "insert into account(username,balance) value(?,?)";
		// 定义数组来存放SQL语句中的参数
		Object[] obj = new Object[] { account.getUsername(), account.getBalance() };
		// 执行添加操作,返回的是受SQL语句影响的记录条数
		int num = this.jdbcTemplate.update(sql, obj);
		return num;
	}

	// 更新账户
	public int updateAccount(Account account) {
		// 定义SQL
		String sql = "update account set username=?,balance=? where id = ?";
		// 定义数组来存放SQL语句中的参数
		Object[] params = new Object[] { account.getUsername(), account.getBalance(), account.getId() };
		// 执行添加操作,返回的是受SQL语句影响的记录条数
		int num = this.jdbcTemplate.update(sql, params);
		return num;
	}

	// 删除账户
	public int deleteAccount(int id) {
		// 定义SQL
		String sql = "delete  from account where id = ? ";
		// 执行添加操作,返回的是受SQL语句影响的记录条数
		int num = this.jdbcTemplate.update(sql, id);
		return num;
	}
}

3.依存関係を追加します

	<!--定义id为accountDao的Bean-->
	<bean id="accountDao" class="com.Example.jdbc.AccountDaoImpl">
		<!-- 将jdbcTemplate注入到accountDao实例中 -->
		<property name="jdbcTemplate" ref="jdbcTemplate" />
	</bean>

第四に、テストクラスを作成します

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class JdbcTemplateTest_update {
	public static void main(String[] args) {
		// 加载配置文件
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		// 获取AccountDao实例
		AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
		// 创建Account对象,并向Account对象中添加数据
		Account account = new Account();
		account.setUsername("tom");
		account.setBalance(1000.00);
		// 执行addAccount()方法,并获取返回结果
		int num = accountDao.addAccount(account);
		if (num > 0) {
			System.out.println("成功插入了" + num + "条数据!");
		} else {
			System.out.println("插入操作执行失败!");
		}

		// 修改数据
		Account ud = new Account();
		ud.setId(1);
		ud.setUsername("tom");
		ud.setBalance(2000.00);
		// 执行updata方法并返回结果
		int sum = accountDao.updateAccount(ud);
		if (sum > 0) {
			System.out.println("成功修改了" + sum + "条数据!");
		} else {
			System.out.println("修改操作执行失败!");
		}
	}
	
		//执行findAccountById()方法
}

5.データベースを表示します

mysql> use spring;
Database changed
mysql> select * from account;
+----+-----------+---------+
| id | username  | balance |
+----+-----------+---------+
|  1 | 孙悟空    |     100 |
|  2 | 唐僧      |    1000 |
|  3 | 猪八戒    |    2000 |
|  4 | 沙僧      |    5000 |
+----+-----------+---------+
4 rows in set (0.04 sec)

6.プログラム操作

 7.データベースを再度確認します

mysql> select * from account;
+----+-----------+---------+
| id | username  | balance |
+----+-----------+---------+
|  1 | tom       |    2000 |
|  2 | 唐僧      |    1000 |
|  3 | 猪八戒    |    2000 |
|  4 | 沙僧      |    5000 |
|  5 | tom       |    1000 |
+----+-----------+---------+
5 rows in set (0.00 sec)

        データテーブルのアカウントの最初のデータが変更され、最後のデータが追加されました

おすすめ

転載: blog.csdn.net/m0_54925305/article/details/123169124