JdbcTemplate需要的jar包

1、Spring核心必须依赖的库:commons-logging-1.1.1.jar
2、Spring IoC部分核心库:

  • spring-beans-4.3.9.RELEASE.jar
  • spring-context-4.3.9.RELEASE.jar
  • spring-context-support-4.3.9.RELEASE.jar
  • spring-core-4.3.9.RELEASE.jar
  • spring-expression-4.3.9.RELEASE.jar
  • spring-web-4.3.9.RELEASE.jar      ------> 支持在Web环境中使用Spring IoC容器

3、Spring AOP部分核心库:

  • spring-aop-4.3.9.RELEASE.jar
  • spring-aspects-4.3.9.RELEASE.jar

4、Spring AOP需要依赖于aspectj库:

  • aspectjrt.jar
  • aspectjweaver.jar

5、Spring JDBC部分核心库:

  • spring-jdbc-4.3.9.RELEASE.jar
  • spring-tx-4.3.9.RELEASE.jar

6、数据库对应的驱动包:mysql-connector-java-5.1.40-bin.jar

Spring Jdbc 的使用

1、Spring通过抽象JDBC访问并一致的API来简化JDBC编程的工作量。我们只需要声明SQL、调用合适的SpringJDBC框架API、处理结果集即可。事务由Spring管理,并将JDBC受查异常转换为Spring一致的非受查异常,从而简化开发。

2、XML配置(AOP)事务控制

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:p="http://www.springframework.org/schema/p"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
  9. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
  10. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
  11.  
  12. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
  13. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  14. <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf8" />
  15. <property name="username" value="root" />
  16. <property name="password" value="123456" />
  17. </bean>
  18.  
  19. <!-- 配置平台事务管理器 -->
  20. <bean id="platformTransactionManager"
  21. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  22. <property name="dataSource" ref="dataSource" />
  23. </bean>
  24.  
  25. <!-- 提供对事务的配置 ( Advice ) Advice不需要由我们完成只要提供配置就好了,会根据配置生成相应的事物配置的方法-->
  26. <!-- no-rollback-for 设置不回滚 isolation 隔离级别 -->
  27. <tx:advice id="transactionAdvice" transaction-manager="platformTransactionManager">
  28. <tx:attributes>
  29. <tx:method name="persist*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="false" />
  30. <tx:method name="save*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="false" />
  31. <tx:method name="update*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="false" />
  32. <tx:method name="delete*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="false" />
  33. <tx:method name="remove*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="false" />
  34.  
  35. <tx:method name="load*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="true" />
  36. <tx:method name="get*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="true" />
  37. <tx:method name="find*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="true" />
  38. <tx:method name="query*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="true" />
  39. </tx:attributes>
  40. </tx:advice>
  41.  
  42. <!-- 使用 aop:config 实现将 Advice 织入到 相应的 连接点中 -->
  43. <aop:config>
  44. <!-- 事务对应的切点应该选择到 Service 层次,这里 为了简化步骤 暂时 选择了 Dao 层次 -->
  45. <aop:pointcut id="tx-pointcut" expression="execution(* ecut.jdbc.dao.*.*(..))"/>
  46. <!-- 声明事务控制 切面 -->
  47. <aop:advisor pointcut-ref="tx-pointcut" advice-ref="transactionAdvice"/>
  48. </aop:config>
  49.  
  50. </beans>

propagation传播属性详解

  • REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
  • SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行。
  • MANDATORY:支持当前事务,如果当前没有事务,就抛出异常。
  • REQUIRES_NEW:新建事务,如果当前存在事务,把当前事务挂起。
  • NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
  • NEVER:以非事务方式执行,如果当前存在事务,则抛出异常。
  • NESTED:支持当前事务,如果当前事务存在,则执行一个嵌套事务,如果当前没有事务,就新建一个事务

isolation隔离级别属性详解

  • DEFAULT:使用后端数据库默认的隔离级别(spring中的默认选择项)。
  • READ_UNCOMMITED:允许你读取还未提交的改变了的数据。可能导致脏、幻、不可重复读。
  • READ_COMMITTED:允许在并发事务已经提交后读取。可防止脏读,但幻读和 不可重复读仍可发生。
  • REPEATABLE_READ:对相同字段的多次读取是一致的,除非数据被事务本身改变。可防止脏、不可重复读,但幻读仍可能发生。
  • SERIALIZABLE:完全服从ACID的隔离级别,确保不发生脏、幻、不可重复读。这在所有的隔离级别中是最慢的,它是典型的通过完全锁定在事务中涉及的数据表来完成的。

name属性详解

  • 与事务属性关联的方法名。通配符(*)可以用来指定一批关联到相同的事务属性的方法。

平台事务管理器,包含事务的提交回滚等这些信息,并以ref的方式为平台事务管理器注入dataSource的引用。提供对事务的配置 Advice ,并以方法为单位,指定方法应用什么事务属性 isolation:隔离级别 propagation:传播行为 read-only:是否只读。使用 aop:config 实现将事务的配置 Advice 织入到 相应的 连接点中。

3、JdbcTemplate使用的基本步骤

  • 引用jar包
  • 创建数据库和表
    1. DROP TABLE IF EXISTS t_customer ;
    2.  
    3. CREATE TABLE t_customer (
    4. id INT(5) PRIMARY KEY ,
    5. email VARCHAR(60) UNIQUE NOT NULL,
    6. password VARCHAR(32) NOT NULL ,
    7. nickname VARCHAR(150) ,
    8. gender VARCHAR(3) ,
    9. birthdate DATE ,
    10. married CHAR(1)
    11. );
  • 创建类

    Customer类

    1. package ecut.jdbc.entity;
    2.  
    3. import java.util.Date;
    4.  
    5. public class Customer {
    6.  
    7. private Integer id;
    8. private String email;
    9. private String password;
    10. private String nickname;
    11. private char gender;
    12. private Date birthdate;
    13. private boolean married;
    14.  
    15. public Integer getId() {
    16. return id;
    17. }
    18.  
    19. public void setId(Integer id) {
    20. this.id = id;
    21. }
    22.  
    23. public String getEmail() {
    24. return email;
    25. }
    26.  
    27. public void setEmail(String email) {
    28. this.email = email;
    29. }
    30.  
    31. public String getPassword() {
    32. return password;
    33. }
    34.  
    35. public void setPassword(String password) {
    36. this.password = password;
    37. }
    38.  
    39. public String getNickname() {
    40. return nickname;
    41. }
    42.  
    43. public void setNickname(String nickname) {
    44. this.nickname = nickname;
    45. }
    46.  
    47. public char getGender() {
    48. return gender;
    49. }
    50.  
    51. public void setGender(char gender) {
    52. this.gender = gender;
    53. }
    54.  
    55. public Date getBirthdate() {
    56. return birthdate;
    57. }
    58.  
    59. public void setBirthdate(Date birthdate) {
    60. this.birthdate = birthdate;
    61. }
    62.  
    63. public boolean isMarried() {
    64. return married;
    65. }
    66.  
    67. public void setMarried(boolean married) {
    68. this.married = married;
    69. }
    70.  
    71. }

    CustomerController类

    1. package ecut.jdbc.controller;
    2.  
    3. import ecut.jdbc.service.CustomerService;
    4.  
    5. public class CustomerController {
    6.  
    7. private CustomerService customerService ;
    8.  
    9. public CustomerService getCustomerService() {
    10. return customerService;
    11. }
    12.  
    13. public void setCustomerService(CustomerService customerService) {
    14. this.customerService = customerService;
    15. }
    16.  
    17. }

    CustomerService类

    1. package ecut.jdbc.service;
    2.  
    3. import ecut.jdbc.dao.CustomerDao;
    4.  
    5. public class CustomerService {
    6.  
    7. private CustomerDao customerDao ;
    8.  
    9. public CustomerDao getCustomerDao() {
    10. return customerDao;
    11. }
    12.  
    13. public void setCustomerDao(CustomerDao customerDao) {
    14. this.customerDao = customerDao;
    15. }
    16.  
    17. }

    配置文件

    1. <?xml version="1.0" encoding="UTF-8"?>
    2.  
    3. <beans xmlns="http://www.springframework.org/schema/beans"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:aop="http://www.springframework.org/schema/aop"
    6. xmlns:p="http://www.springframework.org/schema/p"
    7. xmlns:tx="http://www.springframework.org/schema/tx"
    8. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    9. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    10. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    11.  
    12. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
    13. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    14. <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf8" />
    15. <property name="username" value="root" />
    16. <property name="password" value="123456" />
    17. </bean>
    18.  
    19. <!-- 配置平台事务管理器 -->
    20. <bean id="platformTransactionManager"
    21. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    22. <property name="dataSource" ref="dataSource" />
    23. </bean>
    24.  
    25. <!-- 提供对事务的配置 ( Advice ) Advice不需要由我们完成只要提供配置就好了,会根据配置生成相应的事物配置的方法-->
    26. <!-- no-rollback-for 设置不回滚 isolation 隔离级别 tx:method 与事物的那些方法-->
    27. <tx:advice id="transactionAdvice" transaction-manager="platformTransactionManager">
    28. <tx:attributes>
    29. <tx:method name="persist*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="false" />
    30. <tx:method name="save*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="false" />
    31. <tx:method name="update*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="false" />
    32. <tx:method name="delete*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="false" />
    33. <tx:method name="remove*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="false" />
    34.  
    35. <tx:method name="load*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="true" />
    36. <tx:method name="get*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="true" />
    37. <tx:method name="find*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="true" />
    38. <tx:method name="query*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="true" />
    39. </tx:attributes>
    40. </tx:advice>
    41.  
    42. <!-- 使用 aop:config 实现将 Advice 织入到 相应的 连接点中 -->
    43. <aop:config>
    44. <!-- 事务对应的切点应该选择到 Service 层次,这里 为了简化步骤 暂时 选择了 Dao 层次 -->
    45. <aop:pointcut id="tx-pointcut" expression="execution(* ecut.jdbc.dao.*.*(..))"/>
    46. <!-- 声明事务控制 切面 -->
    47. <aop:advisor pointcut-ref="tx-pointcut" advice-ref="transactionAdvice"/>
    48. </aop:config>
    49.  
    50. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    51. <!-- 数据源 -->
    52. <property name="dataSource" ref="dataSource"/>
    53. </bean>
    54.  
    55. <bean id="customerDao" class="ecut.jdbc.dao.CustomerDao" >
    56. <property name="jdbcTemplate" ref="jdbcTemplate" />
    57. </bean>
    58.  
    59. <bean id="customerService" class="ecut.jdbc.service.CustomerService" >
    60. <property name="customerDao" ref="customerDao" />
    61. </bean>
    62.  
    63. <bean id="customerController" class="ecut.jdbc.controller.CustomerController" >
    64. <property name="customerService" ref="customerService" />
    65. </bean>
    66.  
    67. </beans>

    controller中添加了CustomerService的对象,因此需要将CustomerService以ref的方式引入到controller中。service中添加了CustomerDao的对象,因此需要将CustomerDao以ref的方式引入到service中。dao中添加了jdbcTemplate的对象,因此需要将jdbcTemplate以ref的方式引入到dao中。而Template依赖与DataSource,以ref的方式为JdbcTemplate注入引用。DataSource的属性可以通过注入数据库的一些配置属性添加。

    CustomerDao类

    1. package ecut.jdbc.dao;
    2.  
    3. import java.sql.Connection;
    4. import java.sql.PreparedStatement;
    5. import java.sql.ResultSet;
    6. import java.sql.SQLException;
    7. import java.util.List;
    8.  
    9. import ecut.jdbc.entity.Customer;
    10. import org.springframework.dao.DataAccessException;
    11. import org.springframework.jdbc.core.JdbcTemplate;
    12. import org.springframework.jdbc.core.PreparedStatementCallback;
    13. import org.springframework.jdbc.core.PreparedStatementCreator;
    14. import org.springframework.jdbc.core.RowMapper;
    15.  
    16. public class CustomerDao {
    17.  
    18. private JdbcTemplate jdbcTemplate;
    19. //在表名前后加空格避免空格缺失
    20. private static final String TABLE = " t_customer " ;
    21.  
    22. public boolean persist( Customer c ) {
    23.  
    24. final Integer id = jdbcTemplate.queryForObject( "SELECT max(id) FROM " + TABLE , Integer.class ) ;
    25.  
    26. System.out.println( "id : " + id );
    27.  
    28. final String SQL = "INSERT INTO " + TABLE +
    29. " ( id , email , password , nickname , gender , birthdate , married ) " +
    30. " VALUES ( ? , ? , ? , ? , ? , ? , ? ) " ;
    31. //是一个接口用匿名内部类的方法去实现
    32. PreparedStatementCallback<Integer> action = new PreparedStatementCallback<Integer>(){
    33. @Override
    34. public Integer doInPreparedStatement( PreparedStatement ps )
    35. throws SQLException, DataAccessException {
    36.  
    37. ps.setInt( 1 , id + 1 );
    38. ps.setString( 2 , c.getEmail() );
    39. ps.setString( 3 , c.getPassword() );
    40. ps.setString( 4 , c.getNickname() );
    41. ps.setString( 5 , c.getGender() + "" );
    42. // c.getBirthdate().getTime() 获得毫秒数
    43. java.sql.Date date = new java.sql.Date( c.getBirthdate().getTime() ) ;
    44. ps.setDate( 6 , date );
    45. ps.setString( 7 , c.isMarried() ? "Y" : "N" );
    46.  
    47. int count = ps.executeUpdate() ;
    48.  
    49. return count;
    50. }
    51. };
    52. //doInPreparedStatement返回类型则jdbcTemplate.execute( SQL , action );也是返回什么类型
    53. Integer count = jdbcTemplate.execute( SQL , action );
    54.  
    55. // return count != null && count > 0 ;
    56.  
    57. if( count != null && count > 0 ) {
    58. return true ;
    59. } else {
    60. return false ;
    61. }
    62.  
    63. }
    64.  
    65. public boolean update( Customer c , Integer id ) {
    66. final String SQL = "UPDATE " + TABLE +
    67. " SET email =?, password = ? , nickname = ? ,gender =? , birthdate = ? , married = ? where id = ?";
    68. PreparedStatementCallback<Integer> action = new PreparedStatementCallback<Integer>(){
    69. @Override
    70. public Integer doInPreparedStatement( PreparedStatement ps )
    71. throws SQLException, DataAccessException {
    72.  
    73. ps.setString( 1 , c.getEmail() );
    74. ps.setString( 2 , c.getPassword() );
    75. ps.setString( 3 , c.getNickname() );
    76. ps.setString( 4 , c.getGender() + "" );
    77. // c.getBirthdate().getTime() 获得毫秒数
    78. java.sql.Date date = new java.sql.Date( c.getBirthdate().getTime() ) ;
    79. ps.setDate( 5 , date );
    80. ps.setString( 6 , c.isMarried() ? "Y" : "N" );
    81. ps.setInt( 7 , id );
    82.  
    83. int count = ps.executeUpdate() ;
    84.  
    85. return count;
    86. }
    87. };
    88. //doInPreparedStatement返回类型则jdbcTemplate.execute( SQL , action );也是返回什么类型
    89. Integer count = jdbcTemplate.execute( SQL , action );
    90.  
    91. // return count != null && count > 0 ;
    92.  
    93. if( count != null && count > 0 ) {
    94. return true ;
    95. } else {
    96. return false ;
    97. }
    98.  
    99. }
    100.  
    101. public boolean delete( Integer id ) {
    102.  
    103. final String SQL = "DELETE FROM " + TABLE +"WHERE id = ?";
    104. PreparedStatementCallback<Integer> action = new PreparedStatementCallback<Integer>() {
    105.  
    106. @Override
    107. public Integer doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
    108. ps.setInt(1, id);
    109. int count = ps.executeUpdate();
    110. return count;
    111. }
    112.  
    113. };
    114. Integer count = jdbcTemplate.execute( SQL , action );
    115.  
    116. // return count != null && count > 0 ;
    117.  
    118. if( count != null && count > 0 ) {
    119. return true ;
    120. } else {
    121. return false ;
    122. }
    123. }
    124.  
    125. public Customer load( Integer id ) {
    126.  
    127. final String SQL = "SELECT id , email , password , nickname , gender , birthdate , married FROM " + TABLE + " WHERE id = ? " ;
    128.  
    129. PreparedStatementCreator psc = new PreparedStatementCreator(){
    130.  
    131. @Override
    132. public PreparedStatement createPreparedStatement( Connection conn ) throws SQLException {
    133.  
    134. PreparedStatement ps = conn.prepareStatement( SQL );
    135.  
    136. ps.setInt( 1 , id );
    137.  
    138. return ps ;
    139. }
    140.  
    141. };
    142.  
    143. final RowMapper<Customer> rowMapper = new RowMapper<Customer>() {
    144. @Override
    145. public Customer mapRow( ResultSet rs, int count ) throws SQLException {
    146.  
    147. Customer c = new Customer();
    148. //Integer id = rs.getInt( "id" );可读性好效率低
    149. Integer id = rs.getInt( 1 );
    150. c.setId( id );
    151.  
    152. String email = rs.getString( 2 );
    153. c.setEmail( email );
    154.  
    155. String password = rs.getString( 3 );
    156. c.setPassword(password);
    157.  
    158. String nickname = rs.getString( 4 );
    159. c.setNickname(nickname);
    160.  
    161. String gender = rs.getString( 5 );
    162. if( gender != null && gender.length() > 0 ) {
    163. c.setGender( gender.charAt( 0 ) );
    164. }
    165.  
    166. java.sql.Date birthdate = rs.getDate( 6 );
    167. c.setBirthdate( birthdate );
    168.  
    169. String married = rs.getString( 7 );
    170. c.setMarried( "Y".equals( married ) ? true : false );
    171.  
    172. return c;
    173. }
    174. } ;
    175.  
    176. List<Customer> list = jdbcTemplate.query( psc , rowMapper);
    177.  
    178. if( list != null && list.size() > 0 ) {
    179. Customer c = list.get( 0 );
    180. return c ;
    181. } else {
    182. return null ;
    183. }
    184.  
    185. }
    186.  
    187. public List<Customer> loadAll() {
    188.  
    189. final String SQL = "SELECT id , email , password , nickname , gender , birthdate , married FROM " + TABLE ;
    190. //吧查询后的结果拼装起来返回的对象,将行记录包装成一个个的对象
    191. final RowMapper<Customer> rowMapper = new RowMapper<Customer>() {
    192. @Override
    193. public Customer mapRow( ResultSet rs, int count ) throws SQLException {
    194.  
    195. Customer c = new Customer();
    196.  
    197. Integer id = rs.getInt( 1 );
    198. c.setId( id );
    199.  
    200. String email = rs.getString( 2 );
    201. c.setEmail( email );
    202.  
    203. String password = rs.getString( 3 );
    204. c.setPassword(password);
    205.  
    206. String nickname = rs.getString( 4 );
    207. c.setNickname(nickname);
    208.  
    209. String gender = rs.getString( 5 );
    210. if( gender != null && gender.length() > 0 ) {
    211. c.setGender( gender.charAt( 0 ) );
    212. }
    213.  
    214. java.sql.Date birthdate = rs.getDate( 6 );
    215. c.setBirthdate( birthdate );
    216.  
    217. String married = rs.getString( 7 );
    218. c.setMarried( "Y".equals( married ) ? true : false );
    219.  
    220. return c;
    221. }
    222. } ;
    223.  
    224. List<Customer> list = jdbcTemplate.query( SQL , rowMapper );
    225.  
    226. return list ;
    227. }
    228.  
    229. public JdbcTemplate getJdbcTemplate() {
    230. return jdbcTemplate;
    231. }
    232.  
    233. public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    234. this.jdbcTemplate = jdbcTemplate;
    235. }
    236.  
    237. }

    测试类

    1. package ecut.jdbc;
    2.  
    3. import java.util.Date;
    4. import java.util.List;
    5.  
    6. import org.junit.AfterClass;
    7. import org.junit.BeforeClass;
    8. import org.junit.Test;
    9. import ecut.jdbc.dao.CustomerDao;
    10. import ecut.jdbc.entity.Customer;
    11. import org.springframework.context.support.AbstractApplicationContext;
    12. import org.springframework.context.support.ClassPathXmlApplicationContext;
    13.  
    14. public class TestCustomerDao {
    15.  
    16. private static AbstractApplicationContext container;
    17.  
    18. public @BeforeClass static void init() {
    19. String configLocations = "classpath:ecut/**/jdbc/beans.xml";
    20. container = new ClassPathXmlApplicationContext(configLocations);
    21. }
    22.  
    23. public @Test void testSaveCustomer() {
    24.  
    25. Customer c = new Customer();
    26.  
    27. c.setEmail("Amy@ecut.edu.cn");
    28. c.setPassword("hello2017");
    29.  
    30. Date birthdate = new Date();
    31. c.setBirthdate(birthdate);
    32. c.setGender('女');
    33.  
    34. c.setNickname("Saber");
    35. c.setMarried(false);
    36.  
    37. CustomerDao customerDao = container.getBean("customerDao", CustomerDao.class);
    38.  
    39. customerDao.persist(c);
    40.  
    41. }
    42.  
    43. public @Test void testLoadCustomer() {
    44.  
    45. CustomerDao customerDao = container.getBean("customerDao", CustomerDao.class);
    46.  
    47. Customer c = customerDao.load(1);
    48.  
    49. System.out.println(c.getEmail());
    50.  
    51. }
    52.  
    53. public @Test void testLoadAllCustomer() {
    54.  
    55. CustomerDao customerDao = container.getBean("customerDao", CustomerDao.class);
    56.  
    57. List<Customer> list = customerDao.loadAll();
    58.  
    59. for (Customer c : list) {
    60. System.out.println(c.getEmail() + " : " + c.getNickname());
    61. }
    62.  
    63. }
    64.  
    65. public @Test void testUpdateCustomer() {
    66.  
    67. Customer c = new Customer();
    68.  
    69. c.setEmail("Saber@ecut.edu.cn");
    70. c.setPassword("hello2017");
    71.  
    72. Date birthdate = new Date();
    73. c.setBirthdate(birthdate);
    74. c.setGender('女');
    75.  
    76. c.setNickname("Amy");
    77. c.setMarried(false);
    78.  
    79. CustomerDao customerDao = container.getBean("customerDao", CustomerDao.class);
    80.  
    81. customerDao.update(c, 2);
    82.  
    83. }
    84.  
    85. public @Test void testDeleteCustomer() {
    86.  
    87. CustomerDao customerDao = container.getBean("customerDao", CustomerDao.class);
    88.  
    89. boolean flag = customerDao.delete(1);
    90.  
    91. System.out.println(flag);
    92. }
    93.  
    94. public @AfterClass static void destory() {
    95. container.close();
    96. }
    97.  
    98. }

转载请于明显处标明出处:

https://www.cnblogs.com/AmyZheng/p/9281810.html

Spring学习(九)的更多相关文章

  1. spring学习九 spring aop详解

    本文来自于:https://www.cnblogs.com/jingzhishen/p/4980551.html AOP(Aspect-Oriented Programming,面向方面编程),可以说 ...

  2. Spring学习(九)-----Spring bean配置继承

    在 Spring,继承是用为支持bean设置一个 bean 来分享共同的值,属性或配置. 一个子 bean 或继承的bean可以继承其父 bean 的配置,属性和一些属性.另外,子 Bean 允许覆盖 ...

  3. Spring学习九----------Bean的配置之Bean的定义及作用域的注解实现

    © 版权声明:本文为博主原创文章,转载请注明出处 Spring Bean常用注解 @Component:通常注解,可用于任何Bean @Repository:通常用于注解DAO层,即持久层 @Serv ...

  4. Spring学习九 Servlet相关

    servlet作用: 它驻留在 Web 服务器上,处理新来的请求和输出的响应.它与表示无关,实际上也不它应该与表示有关. 作为一名专业编程人员,您碰到的大多数 Java servlet 都是为响应 W ...

  5. Spring学习(十一)-----Spring使用@Required注解依赖检查

    Spring学习(九)-----Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种 ...

  6. Spring学习(九)Spring 和数据库编程【了解】

    一.传统 JDBC 回顾 用一个大佬的demo来简单看一下 /** * 使用jdbc,根据id查询单个Student的信息 */ public class JdbcManage { public St ...

  7. Spring Boot2(九):整合Jpa的基本使用

    一.前言 今天早上看到一篇微信文章,说的是国内普遍用的Mybatis,而国外确普遍用的是Jpa.我之前也看了jpa,发现入门相当容易.jpa对于简单的CRUD支持非常好,开发效率也会比Mybatis高 ...

  8. Spring学习之——手写Spring源码V2.0(实现IOC、D、MVC、AOP)

    前言 在上一篇<Spring学习之——手写Spring源码(V1.0)>中,我实现了一个Mini版本的Spring框架,在这几天,博主又看了不少关于Spring源码解析的视频,受益匪浅,也 ...

  9. spring 学习之 bean 的注入方式 property和constructor-arg的使用方式

    spring 学习之 bean 的注入方式 property和constructor-arg的使用方式. bean的注入方式: property 注入是: 通过setxx方法注入. construct ...

随机推荐

  1. 题解【CJOJ2608】[JZOJ 100043]第k小数

    P2608 - [JZOJ 100043]第k小数 Description 有两个非负整数数列,元素个数分别为N和M.从两个数列中分别任取一个数相乘,这样一共可以得到N*M个数,询问这N*M个数中第K ...

  2. JS 数组克隆方法总结

    ES5 方法总结 1.slice let arr = [2,4,434,43] let arr1= arr.slice() arr[0] = 'a' console.log(arr,arr1) // ...

  3. Iframe 高度自适应,js控制Iframe 高度自适应

     Iframe 高度自适应, js控制Iframe 高度自适应, iframe自适应高度 ================================ ©Copyright 蕃薯耀 2019年12 ...

  4. SQL Server 用户定义表类型

    用户定义表类型: CREATE TYPE [dbo].[TVP_Location] AS TABLE( [Location] [varchar](50) NOT NULL, [Address] [va ...

  5. JSP技术(二)

    参考网址:https://blog.csdn.net/king_cannon_fodder/article/details/79835463 (1)JSP隐式对象(9个内置对象) Servlet容器会 ...

  6. 【Python】使用socketserver建立一个异步TCP服务器

    概述 这篇文章是讲解如何使用socketserver建立一个异步TCP服务器,其中Python版本为3.5.1. socketserver主要的类 socketserver模块中的类主要有以下几个:1 ...

  7. BufferedInputStream 介绍

    BufferedInputStream 介绍 BufferedInputStream 是缓冲输入流.它继承于FilterInputStream.BufferedInputStream 的作用是为另一个 ...

  8. ASP.NET Core搭建多层网站架构【0-前言】

    2020/01/26, ASP.NET Core 3.1, VS2019 摘要:基于ASP.NET Core 3.1 WebApi搭建后端多层网站架构 目录 0-前言 1-项目结构分层建立 2-公共基 ...

  9. Python - 用python实现split函数

    # pattern支持字符或者字符串 def my_split(string, pattern): ret = [] len_pattern = len(pattern) while True: in ...

  10. 日期格式在ios中的兼容性

    在IOS中支持 2017/3/2 这种格式的日期 不支持2017-3-2日期 /** * 返回兼容ios.android的日期时间格式 * @param dateTime String * @retu ...