• SpringJdbc之最强版本,附带十个开源案例!!!!


    导入依赖

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0</modelVersion>
    5. <groupId>com.xxx</groupId>
    6. <artifactId>SpringJdbc</artifactId>
    7. <version>1.0-SNAPSHOT</version>
    8. <name>SpringJdbc</name>
    9. <!-- FIXME change it to the project's website -->
    10. <url>http://www.example.com</url>
    11. <properties>
    12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    13. <maven.compiler.source>1.8</maven.compiler.source>
    14. <maven.compiler.target>1.8</maven.compiler.target>
    15. </properties>
    16. <dependencies>
    17. <dependency>
    18. <groupId>junit</groupId>
    19. <artifactId>junit</artifactId>
    20. <version>4.12</version>
    21. <scope>test</scope>
    22. </dependency>
    23. <!-- //引入jdbc的包-->
    24. <dependency>
    25. <groupId>org.springframework</groupId>
    26. <artifactId>spring-jdbc</artifactId>
    27. <version>5.2.4.RELEASE</version>
    28. </dependency>
    29. <!-- //引入mysql数据库驱动包-->
    30. <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    31. <dependency>
    32. <groupId>mysql</groupId>
    33. <artifactId>mysql-connector-java</artifactId>
    34. <version>8.0.19</version>
    35. </dependency>
    36. <dependency>
    37. <groupId>org.springframework</groupId>
    38. <artifactId>spring-context</artifactId>
    39. <version>5.2.4.RELEASE</version>
    40. </dependency>
    41. <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
    42. <dependency>
    43. <groupId>org.springframework</groupId>
    44. <artifactId>spring-test</artifactId>
    45. <version>5.2.4.RELEASE</version>
    46. <scope>test</scope>
    47. </dependency>
    48. <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    49. <dependency>
    50. <groupId>org.aspectj</groupId>
    51. <artifactId>aspectjweaver</artifactId>
    52. <version>1.8.9</version>
    53. <scope>runtime</scope>
    54. </dependency>
    55. <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
    56. <dependency>
    57. <groupId>org.springframework</groupId>
    58. <artifactId>spring-tx</artifactId>
    59. <version>5.3.21</version>
    60. </dependency>
    61. <dependency>
    62. <groupId>com.mchange</groupId>
    63. <artifactId>c3p0</artifactId>
    64. <version>0.9.5.2</version>
    65. </dependency>
    66. <dependency>
    67. <groupId>commons-lang</groupId>
    68. <artifactId>commons-lang</artifactId>
    69. <version>2.6</version>
    70. </dependency>
    71. </dependencies>
    72. <build>
    73. </build>
    74. </project>

    实体类

    1. package com.xxx.po;
    2. import java.util.Date;
    3. /**
    4. * @Author 陈平安
    5. * @Date 2022/6/20 16:58
    6. * @PackageName:com.xxx.po
    7. * @ClassName: Account
    8. * @Description: TODO
    9. * @Version 1.0
    10. */
    11. public class Account {
    12. Integer accountid;//账户id
    13. String AccountName;//账户名称
    14. String accountType;//账户 类别
    15. Double money;//
    16. Date createDate;//
    17. Date updateDate;//
    18. Integer userID;//
    19. public Account() {
    20. }
    21. public Account(String accountName, String accountType, Double money, Integer userID) {
    22. AccountName = accountName;
    23. this.accountType = accountType;
    24. this.money = money;
    25. this.userID = userID;
    26. }
    27. @Override
    28. public String toString() {
    29. return "Account{" +
    30. "accountid=" + accountid +
    31. ", AccountName='" + AccountName + '\'' +
    32. ", accountType='" + accountType + '\'' +
    33. ", money=" + money +
    34. ", createDate=" + createDate +
    35. ", updateDate=" + updateDate +
    36. ", userID=" + userID +
    37. '}';
    38. }
    39. public Integer getAccountid() {
    40. return accountid;
    41. }
    42. public void setAccountid(Integer accountid) {
    43. this.accountid = accountid;
    44. }
    45. public String getAccountName() {
    46. return AccountName;
    47. }
    48. public void setAccountName(String accountName) {
    49. AccountName = accountName;
    50. }
    51. public String getAccountType() {
    52. return accountType;
    53. }
    54. public void setAccountType(String accountType) {
    55. this.accountType = accountType;
    56. }
    57. public Double getMoney() {
    58. return money;
    59. }
    60. public void setMoney(Double money) {
    61. this.money = money;
    62. }
    63. public Date getCreateDate() {
    64. return createDate;
    65. }
    66. public void setCreateDate(Date createDate) {
    67. this.createDate = createDate;
    68. }
    69. public Date getUpdateDate() {
    70. return updateDate;
    71. }
    72. public void setUpdateDate(Date updateDate) {
    73. this.updateDate = updateDate;
    74. }
    75. public Integer getUserID() {
    76. return userID;
    77. }
    78. public void setUserID(Integer userID) {
    79. this.userID = userID;
    80. }
    81. }

    db.properties

    1. jdbc.driver=com.mysql.cj.jdbc.Driver
    2. jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8
    3. jdbc.user=root
    4. jdbc.password=cjd

    xml配置信息

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:aop="http://www.springframework.org/schema/aop"
    6. xmlns:tx="http://www.springframework.org/schema/tx"
    7. xmlns:task="http://www.springframework.org/schema/task"
    8. xsi:schemaLocation="http://www.springframework.org/schema/beans
    9. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    10. http://www.springframework.org/schema/context
    11. http://www.springframework.org/schema/context/spring-context-3.1.xsd
    12. http://www.springframework.org/schema/task
    13. http://www.springframework.org/schema/tx
    14. http://www.springframework.org/schema/aop
    15. http://www.springframework.org/schema/aop/spring-aop.xsd
    16. http://www.springframework.org/schema/tx/spring-tx.xsd
    17. http://www.springframework.org/schema/task/spring-task-4.1.xsd">
    18. <!-- http://www.springframework.org/schema/tx-->
    19. <context:component-scan base-package="com.xxx"></context:component-scan>
    20. <!-- <task:annotation-driven></task:annotation-driven>-->
    21. <context:property-placeholder location="jdbc.properties"></context:property-placeholder>
    22. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    23. <property name="driverClass" value="${jdbc.driver}"></property>
    24. <property name="jdbcUrl" value="${jdbc.url}"></property>
    25. <property name="user" value="${jdbc.user}"></property>
    26. <property name="password" value="${jdbc.password}"></property>
    27. </bean>
    28. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    29. <property name="dataSource" ref="dataSource"></property>
    30. </bean>
    31. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    32. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    33. <!--注入数据源-->
    34. <property name="dataSource" ref="dataSource"></property>
    35. </bean>
    36. <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
    37. <tx:advice id="txAdvice" transaction-manager="transactionManager">
    38. <tx:attributes>
    39. <tx:method name="add" propagation="REQUIRED"/>
    40. <tx:method name="update" propagation="REQUIRED"/>
    41. <tx:method name="delete" propagation="REQUIRED"/>
    42. <tx:method name="query" read-only="true"/>
    43. </tx:attributes>
    44. </tx:advice>
    45. <aop:config>
    46. <aop:pointcut id="cut" expression="execution(* com.xxx.service..*.*(..))"/>
    47. </aop:config>
    48. </beans>

    10个方法接口:

    1. package com.xxx.Dao;
    2. import com.xxx.po.Account;
    3. import java.util.List;
    4. /**
    5. * @Author 陈平安
    6. * @Date 2022/6/20 17:02
    7. * @PackageName:com.xxx.Dao
    8. * @ClassName: IAaccount
    9. * @Description: TODO
    10. * @Version 1.0
    11. */
    12. public interface IAaccount {
    13. public int addAccount(Account account);
    14. public int addAccountHasKey(Account account);
    15. public int addAccountBath(List<Account> accounts);
    16. public int queryAccount(int userID);
    17. public Account queryAccountID(int accountid);
    18. public List<Account> queryAccountParmy(Integer userID, String accountName, String
    19. accountType);
    20. public int UpdateAccount(Account account);
    21. public int updateAccountBath(List<Account> accounts);
    22. public int deleteAccount(int accountid);
    23. public int deleteAccountBatch(Integer[] ids);
    24. public int outAccount(Integer accountId, Double money);
    25. public int inAccount(Integer accountId, Double money);
    26. }
    1. package com.xxx.Dao.impl;
    2. import com.xxx.Dao.IAaccount;
    3. import com.xxx.po.Account;
    4. import org.apache.commons.lang.StringUtils;
    5. import org.springframework.jdbc.core.BatchPreparedStatementSetter;
    6. import org.springframework.jdbc.core.JdbcTemplate;
    7. import org.springframework.jdbc.core.PreparedStatementCreator;
    8. import org.springframework.jdbc.support.GeneratedKeyHolder;
    9. import org.springframework.jdbc.support.KeyHolder;
    10. import org.springframework.stereotype.Repository;
    11. import javax.annotation.Resource;
    12. import java.sql.*;
    13. import java.util.ArrayList;
    14. import java.util.List;
    15. /**
    16. * @Author 陈平安
    17. * @Date 2022/6/20 17:24
    18. * @PackageName:com.xxx.impl
    19. * @ClassName: ImpleAccount
    20. * @Description: TODO
    21. * @Version 1.0
    22. */
    23. @Repository
    24. public class ImpleAccount implements IAaccount {
    25. @Resource
    26. private JdbcTemplate jdbcTemplate;
    27. @Override
    28. public int addAccount(Account account) {
    29. String sql = "insert into tb_account (account_name,account_type,money,user_id) values (?,?,?,?)";
    30. Object[] objects = {account.getAccountName(), account.getAccountType(),
    31. account.getMoney(), account.getUserID()};
    32. int nows = jdbcTemplate.update(sql, objects);
    33. return nows;
    34. }
    35. @Override
    36. public int addAccountHasKey(Account account) {
    37. String sql = "insert into tb_account (account_name,account_type,money,user_id) values (?,?,?,?)";
    38. KeyHolder keyHolder = new GeneratedKeyHolder();
    39. jdbcTemplate.update(connection -> {
    40. PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
    41. ps.setString(1, account.getAccountName());
    42. ps.setString(2, account.getAccountType());
    43. ps.setDouble(3, account.getMoney());
    44. ps.setInt(4, account.getUserID());
    45. return ps;
    46. }, keyHolder);
    47. int key = keyHolder.getKey().intValue();
    48. return key;
    49. }
    50. @Override
    51. public int addAccountBath(List<Account> accounts) {
    52. String sql = "insert into tb_account (account_name,account_type,money,user_id) values (?,?,?,?)";
    53. int rows = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
    54. @Override
    55. public void setValues(PreparedStatement ps, int i) throws SQLException {
    56. Account account = accounts.get(i);
    57. ps.setString(1, account.getAccountName());
    58. ps.setString(2, account.getAccountType());
    59. ps.setDouble(3, account.getMoney());
    60. ps.setInt(4, account.getUserID());
    61. }
    62. @Override
    63. public int getBatchSize() {
    64. return accounts.size();
    65. }
    66. }).length;
    67. return rows;
    68. }
    69. @Override
    70. public int queryAccount(int userID) {
    71. String sql = "select count(1) from tb_account where user_id = ?";
    72. int count = jdbcTemplate.queryForObject(sql, Integer.class, userID);
    73. return count;
    74. }
    75. @Override
    76. public Account queryAccountID(int accountid) {
    77. String sql = "select * from tb_account where account_id =?";
    78. Account account = jdbcTemplate.queryForObject(sql, (resultSet, i) -> {
    79. Account account1 = new Account();
    80. account1.setAccountid(accountid);
    81. account1.setAccountName(resultSet.getString("account_name"));
    82. account1.setAccountType(resultSet.getString("account_type"));
    83. account1.setMoney(resultSet.getDouble("money"));
    84. account1.setUserID(resultSet.getInt("user_id"));
    85. return account1;
    86. }, accountid);
    87. return account;
    88. }
    89. @Override
    90. public List<Account> queryAccountParmy(Integer userID, String accountName,
    91. String accountType) {
    92. String sql = "select * from tb_account where user_id = ?";
    93. List<Object> list = new ArrayList<>();
    94. list.add(userID);
    95. if (StringUtils.isNotBlank(accountName)) {
    96. sql += " and account_name like concat('%',?,'%')";
    97. list.add(accountName);
    98. }
    99. if (StringUtils.isNotBlank(accountType)) {
    100. sql += " and account_type = ?";
    101. list.add(accountType);
    102. }
    103. Object[] objects = list.toArray();
    104. List<Account> accountList = jdbcTemplate.query(sql, objects, (ResultSet resultSet, int i) -> {
    105. Account account1 = new Account();
    106. account1.setAccountid(resultSet.getInt("user_id"));
    107. account1.setAccountName(resultSet.getString("account_name"));
    108. account1.setAccountType(resultSet.getString("account_type"));
    109. account1.setMoney(resultSet.getDouble("money"));
    110. account1.setUserID(resultSet.getInt("user_id"));
    111. return account1;
    112. });
    113. return accountList;
    114. }
    115. @Override
    116. public int UpdateAccount(Account account) {
    117. String sql = "update tb_account set account_name = ? ,account_type = ?,money = ? ";
    118. Object[] objects = {account.getAccountName(), account.getAccountType(), account.getMoney()};
    119. int rows = jdbcTemplate.update(sql, objects);
    120. return rows;
    121. }
    122. /**
    123. * @param accounts
    124. * @return
    125. */
    126. @Override
    127. public int updateAccountBath(List<Account> accounts) {
    128. String sql = "update tb_account set account_name = ? ,account_type = ?,money = ? ";
    129. int rows = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
    130. @Override
    131. public void setValues(PreparedStatement ps, int i) throws SQLException {
    132. Account account = accounts.get(i);
    133. ps.setString(1, account.getAccountName());
    134. ps.setString(2, account.getAccountType());
    135. ps.setDouble(3, account.getMoney());
    136. }
    137. @Override
    138. public int getBatchSize() {
    139. return accounts.size();
    140. }
    141. }).length;
    142. return rows;
    143. }
    144. @Override
    145. public int deleteAccount(int accountid) {
    146. String sql = "delete from tb_account where account_id = ?";
    147. int row = jdbcTemplate.update(sql, accountid);
    148. return row;
    149. }
    150. @Override
    151. public int deleteAccountBatch(Integer[] ids) {
    152. String sql = "delete from tb_account where account_id = ?";
    153. int rows = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
    154. @Override
    155. public void setValues(PreparedStatement ps, int i) throws SQLException {
    156. ps.setInt(1, ids[i]);
    157. }
    158. @Override
    159. public int getBatchSize() {
    160. return ids.length;
    161. }
    162. }).length;
    163. return rows;
    164. }
    165. @Override
    166. public int outAccount(Integer accountId, Double money) {
    167. String sql = "update tb_account set money = money - ? where account_id = ?";
    168. Object[] objects = {money,accountId};
    169. return jdbcTemplate.update(sql,objects);
    170. }
    171. @Override
    172. public int inAccount(Integer accountId, Double money) {
    173. String sql = "update tb_account set money = money + ? where account_id = ?";
    174. Object[] objects = {money,accountId};
    175. return jdbcTemplate.update(sql,objects);
    176. }
    177. }

    测试:

    1. package com.xxx.Test;
    2. import org.junit.Test;
    3. import org.springframework.context.ApplicationContext;
    4. import org.springframework.context.support.ClassPathXmlApplicationContext;
    5. import org.springframework.jdbc.core.JdbcTemplate;
    6. /**
    7. * @Author 陈平安
    8. * @Date 2022/6/20 16:46
    9. * @PackageName:com.xxx.Test
    10. * @ClassName: test01
    11. * @Description: TODO
    12. * @Version 1.0
    13. */
    14. public class test01 {
    15. @Test
    16. public void testjdbc(){
    17. ApplicationContext ac =new ClassPathXmlApplicationContext("spring.xml");
    18. JdbcTemplate jdbcTemplate = (JdbcTemplate) ac.getBean("jdbcTemplate");
    19. String sql = "select count(1) from tb_account";
    20. Integer integer = jdbcTemplate.queryForObject(sql,Integer.class);
    21. System.out.println(integer);
    22. }
    23. }

    结果: 

     测试案例的进一步封装:

    1. package com.xxx.Test;
    2. import org.junit.Before;
    3. import org.junit.Test;
    4. import org.springframework.context.ApplicationContext;
    5. import org.springframework.context.support.ClassPathXmlApplicationContext;
    6. import org.springframework.jdbc.core.JdbcTemplate;
    7. /**
    8. * @Author 陈平安
    9. * @Date 2022/6/20 16:46
    10. * @PackageName:com.xxx.Test
    11. * @ClassName: test01
    12. * @Description: TODO
    13. * @Version 1.0
    14. */
    15. public class test02 {
    16. private JdbcTemplate jdbcTemplate;
    17. @Before
    18. public void init() {
    19. ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
    20. jdbcTemplate = (JdbcTemplate) ac.getBean("jdbcTemplate");
    21. }
    22. @Test
    23. public void testjdbc() {
    24. String sql = "select count(1) from tb_account";
    25. Integer integer = jdbcTemplate.queryForObject(sql, Integer.class);
    26. System.out.println(integer);
    27. }
    28. }

    测试案例的再一步封装:

    1. package com.xxx.Test;
    2. import com.xxx.Dao.IAaccount;
    3. import com.xxx.po.Account;
    4. import org.junit.Before;
    5. import org.junit.Test;
    6. import org.junit.runner.RunWith;
    7. import org.springframework.context.ApplicationContext;
    8. import org.springframework.context.support.ClassPathXmlApplicationContext;
    9. import org.springframework.jdbc.core.JdbcTemplate;
    10. import org.springframework.test.context.ContextConfiguration;
    11. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    12. import javax.annotation.Resource;
    13. /**
    14. * @Author 陈平安
    15. * @Date 2022/6/20 16:46
    16. * @PackageName:com.xxx.Test
    17. * @ClassName: test01
    18. * @Description: TODO
    19. * @Version 1.0
    20. */
    21. public class test03 extends BaseTest{
    22. @Resource
    23. private JdbcTemplate jdbcTemplate;
    24. @Resource
    25. private IAaccount iAaccount;
    26. @Before
    27. public void init() {
    28. ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
    29. jdbcTemplate = (JdbcTemplate) ac.getBean("jdbcTemplate");
    30. }
    31. @Test
    32. public void testjdbc() {
    33. String sql = "select count(1) from tb_account";
    34. Integer integer = jdbcTemplate.queryForObject(sql, Integer.class);
    35. System.out.println(integer);
    36. }
    37. @Test
    38. public void test() {
    39. Account account = new Account("account03", "jianhang03", 3000.0, 3);
    40. int row = iAaccount.addAccount(account);
    41. System.out.println(row);
    42. }
    43. }
    1. package com.xxx.Test;
    2. import org.junit.runner.RunWith;
    3. import org.springframework.test.context.ContextConfiguration;
    4. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    5. /**
    6. * @Author 陈平安
    7. * @Date 2022/6/20 16:55
    8. * @PackageName:com.xxx.Test
    9. * @ClassName: BaseTest
    10. * @Description: TODO
    11. * @Version 1.0
    12. */
    13. @RunWith(SpringJUnit4ClassRunner.class)
    14. @ContextConfiguration(locations = {"classpath:spring.xml"})
    15. public class BaseTest {
    16. }

     运行结果:

     

    以下是10个方法的测试案例:

    1. package com.xxx.Test;
    2. import com.xxx.Dao.IAaccount;
    3. import com.xxx.po.Account;
    4. import com.xxx.service.AccountService;
    5. import org.junit.Test;
    6. import javax.annotation.Resource;
    7. import java.util.ArrayList;
    8. import java.util.List;
    9. /**
    10. * @Author 陈平安
    11. * @Date 2022/6/21 10:36
    12. * @PackageName:com.xxx.Test
    13. * @ClassName: AddTest01
    14. * @Description: TODO
    15. * @Version 1.0
    16. */
    17. public class AddTest01 extends BaseTest {
    18. @Resource
    19. private IAaccount iAaccount;
    20. @Test
    21. public void test() {
    22. Account account = new Account("account03", "jianhang03", 3000.0, 3);
    23. int row = iAaccount.addAccount(account);
    24. System.out.println(row);
    25. }
    26. @Test
    27. public void test01() {
    28. Account account = new Account("account05", "jianhang04", 30001.0, 4);
    29. int key = iAaccount.addAccountHasKey(account);
    30. System.out.println(key);
    31. }
    32. @Test
    33. public void test02() {
    34. Account account = new Account("account05", "jianhang04", 30001.0, 4);
    35. Account account2 = new Account("account06", "jianhang06", 30006.0, 6);
    36. Account account3 = new Account("account07", "jianhang07", 30007.0, 7);
    37. List<Account> list = new ArrayList<>();
    38. list.add(account);
    39. list.add(account2);
    40. list.add(account3);
    41. int key = iAaccount.addAccountBath(list);
    42. System.out.println("数值======" + key);
    43. }
    44. @Test
    45. public void test03() {
    46. // Account account = new Account("account05", "jianhang04", 30001.0, 4);
    47. int key = iAaccount.queryAccount(0);
    48. System.out.println("数值1======" + key);
    49. }
    50. @Test
    51. public void test04() {
    52. Account account = iAaccount.queryAccountID(1);
    53. // int key = iAaccount.queryAccount(0);
    54. System.out.println("数值1======" + account.toString());
    55. }
    56. @Test
    57. public void test05() {
    58. List<Account> accountList = iAaccount.queryAccountParmy(1, "account01", null);
    59. System.out.println(accountList.toString());
    60. // int key = iAaccount.queryAccount(0);
    61. //System.out.println("数值1======"+account.toString());
    62. }
    63. @Test
    64. public void test06() {
    65. Account account = new Account("account05000", "jianhang04", 30001.0, 4);
    66. account.setAccountid(1);
    67. int key = iAaccount.UpdateAccount(account);
    68. System.out.println("数值1======" + key);
    69. }
    70. @Test
    71. public void test07() {
    72. Account account = new Account("account05", "jianhang04", 30001.0, 4);
    73. account.setAccountid(1);
    74. Account account2 = new Account("account06", "jianhang06", 30006.0, 6);
    75. account.setAccountid(2);
    76. Account account3 = new Account("account07", "jianhang07", 30007.0, 7);
    77. account.setAccountid(3);
    78. List<Account> list = new ArrayList<>();
    79. list.add(account);
    80. list.add(account2);
    81. list.add(account3);
    82. int key = iAaccount.updateAccountBath(list);
    83. System.out.println("数值1======" + key);
    84. }
    85. @Test
    86. public void test08() {
    87. Integer[] id = {2, 3, 4};
    88. int key = iAaccount.deleteAccountBatch(id);
    89. System.out.println("数值1======" + key);
    90. }
    91. @Resource
    92. private AccountService accountService;
    93. @Test
    94. public void test09() {
    95. int code = accountService.updateAccontByTranfer(10, 11, 100.0);
    96. if (code == 1){
    97. System.out.println("success");
    98. }else {
    99. System.out.println("fail");
    100. }
    101. }
    102. }

    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    在此更正一i下配置文件xml文件

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:aop="http://www.springframework.org/schema/aop"
    6. xmlns:tx="http://www.springframework.org/schema/tx"
    7. xmlns:task="http://www.springframework.org/schema/task"
    8. xsi:schemaLocation="http://www.springframework.org/schema/beans
    9. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    10. http://www.springframework.org/schema/context
    11. http://www.springframework.org/schema/context/spring-context-3.1.xsd
    12. http://www.springframework.org/schema/task
    13. http://www.springframework.org/schema/tx
    14. http://www.springframework.org/schema/aop
    15. http://www.springframework.org/schema/aop/spring-aop.xsd
    16. http://www.springframework.org/schema/tx/spring-tx.xsd
    17. http://www.springframework.org/schema/task/spring-task-4.1.xsd">
    18. <!-- http://www.springframework.org/schema/tx-->
    19. <context:component-scan base-package="com.xxx"></context:component-scan>
    20. <!-- <task:annotation-driven></task:annotation-driven>-->
    21. <context:property-placeholder location="jdbc.properties"></context:property-placeholder>
    22. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    23. <property name="driverClass" value="${jdbc.driver}"></property>
    24. <property name="jdbcUrl" value="${jdbc.url}"></property>
    25. <property name="user" value="${jdbc.user}"></property>
    26. <property name="password" value="${jdbc.password}"></property>
    27. </bean>
    28. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    29. <property name="dataSource" ref="dataSource"></property>
    30. </bean>
    31. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    32. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    33. <!--注入数据源-->
    34. <property name="dataSource" ref="dataSource"></property>
    35. </bean>
    36. <!--<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>-->
    37. <!-- <tx:advice id="txAdvice" transaction-manager="transactionManager">-->
    38. <!-- <tx:attributes>-->
    39. <!-- <tx:method name="add" propagation="REQUIRED"/>-->
    40. <!-- <tx:method name="update" propagation="REQUIRED"/>-->
    41. <!-- <tx:method name="delete" propagation="REQUIRED"/>-->
    42. <!-- <tx:method name="query" read-only="true"/>-->
    43. <!-- </tx:attributes>-->
    44. <!-- </tx:advice>-->
    45. <aop:config>
    46. <aop:pointcut id="cut" expression="execution(* com.xxx.service..*.*(..))"/>
    47. </aop:config>
    48. </beans>

    数据库表:

     

     

  • 相关阅读:
    (最优化理论与方法)第二章最优化所需基础知识-第八节:次梯度
    Linux操作系统——进程控制【一】
    Linux代码初试__进度条
    DHCP服务初探
    pytest+allure 详细版
    qt+ffmpeg视频播放器实现音视频倍速功能
    线程的深度剖析
    ITSS认证现场的评估过程基本步骤
    【仿真】carsim、prescan、simulink联合仿真教程、笔记、操作
    助推科技强国高质量发展《科创超级训练营》系列活动正式拉开帷幕
  • 原文地址:https://blog.csdn.net/qq_63772260/article/details/125439997