• Spring基于注解开发案例


    文章目录
    环境搭建
    0.实体类
    1.业务层接口
    2.业务层实现类
    3.持久层接口
    4.持久层实现类
    5.配置文件:bean.xml
    6.测试类
    通过注解代替bean.xml配置文件
    @Component("account")
    @Value("zhangsan")
    @Service("accountDao")
    @Autowired
    @Repository("accountDao")
    @Configuration
    @ComponentScan
    @Bean
    测试
    @Import
    @PropertySource
    Spring整合Junit
    总结
    0.实体类
    1.业务层接口
    2.业务层实现类
    3.持久层接口
    4.持久层实现类
    5.SpringConfig配置主类
    6.jdbcConfig.properties
    7.JDBCConfig配置副类
    8.测试类
    环境搭建
    数据库:

    1. create table account(
    2.     id int primary key auto_increment,
    3.     name varchar(40),
    4.     money float
    5. )character set utf8 collate utf8_general_ci;
    6. insert into account(name,money) values('aaa',1000);
    7. insert into account(name,money) values('bbb',1000);
    8. insert into account(name,money) values('ccc',1000);

     项目的目录结构:

     pom.xml:

    1. <dependencies>
    2.        
    3.         <dependency>
    4.             <groupId>mysqlgroupId>
    5.             <artifactId>mysql-connector-javaartifactId>
    6.             <version>8.0.18version>
    7.         dependency>
    8.        
    9.         <dependency>
    10.             <groupId>org.springframeworkgroupId>
    11.             <artifactId>spring-webmvcartifactId>
    12.             <version>5.2.0.RELEASEversion>
    13.         dependency>
    14.        
    15.         <dependency>
    16.             <groupId>junitgroupId>
    17.             <artifactId>junitartifactId>
    18.             <version>4.12version>
    19.             <scope>testscope>
    20.         dependency>
    21.        
    22.         <dependency>
    23.             <groupId>c3p0groupId>
    24.             <artifactId>c3p0artifactId>
    25.             <version>0.9.1.2version>
    26.         dependency>
    27.        
    28.         <dependency>
    29.             <groupId>commons-dbutilsgroupId>
    30.             <artifactId>commons-dbutilsartifactId>
    31.             <version>1.7version>
    32.         dependency>
    33.        
    34.         <dependency>
    35.             <groupId>junitgroupId>
    36.             <artifactId>junitartifactId>
    37.             <version>4.12version>
    38.             <scope>testscope>
    39.         dependency>
    40.     dependencies>


    0.实体类

    1. public class Account {
    2.     private Integer id;
    3.     private String name;
    4.     private Float money;
    5.     public Integer getId() {
    6.         return id;
    7.     }
    8.     public void setId(Integer id) {
    9.         this.id = id;
    10.     }
    11.     public String getName() {
    12.         return name;
    13.     }
    14.     public void setName(String name) {
    15.         this.name = name;
    16.     }
    17.     public Float getMoney() {
    18.         return money;
    19.     }
    20.     public void setMoney(Float money) {
    21.         this.money = money;
    22.     }
    23.     @Override
    24.     public String toString() {
    25.         return "Account{" +
    26.                 "id=" + id +
    27.                 ", name='" + name + '\'' +
    28.                 ", money=" + money +
    29.                 '}';
    30.     }
    31. }


    1.业务层接口

    1. public interface AccountService {
    2.     //查询所有
    3.     List findAllAccount();
    4.     //查询一个
    5.     Account findAccountById(Integer id);
    6.     //保存
    7.     void saveAccount(Account account);
    8.     //更新
    9.     void updateAccount(Account account);
    10.     //删除
    11.     void deleteAccount(Integer id);
    12. }

    2.业务层实现类
     

    1. //业务层实现类,业务层调用持久层
    2. public class AccountServiceImpl implements AccountService{
    3.     private AccountDao accountDao;
    4.     public void setAccountDao(AccountDao accountDao) {
    5.         this.accountDao = accountDao;
    6.     }
    7.     public List findAllAccount() {
    8.         return accountDao.findAllAccount();
    9.     }
    10.     public Account findAccountById(Integer id) {
    11.         return accountDao.findAccountById(id);
    12.     }
    13.     public void saveAccount(Account account) {
    14.         accountDao.saveAccount(account);
    15.     }
    16.     public void updateAccount(Account account) {
    17.         accountDao.updateAccount(account);
    18.     }
    19.     public void deleteAccount(Integer id) {
    20.         accountDao.deleteAccount(id);
    21.     }
    22. }

    3.持久层接口

    1. //账户的持久层接口
    2. public interface AccountDao {
    3.     //查询所有
    4.     List findAllAccount();
    5.     //查询一个
    6.     Account findAccountById(Integer id);
    7.     //保存
    8.     void saveAccount(Account account);
    9.     //更新
    10.     void updateAccount(Account account);
    11.     //删除
    12.     void deleteAccount(Integer id);
    13. }

    4.持久层实现类

    1. //持久层实现类
    2. public class AccountDaoImpl implements AccountDao {
    3.     private QueryRunner runner;
    4.     public void setRunner(QueryRunner runner) {
    5.         this.runner = runner;
    6.     }
    7.     public List findAllAccount() {
    8.         try {
    9.             return runner.query("select * from account",new BeanListHandler(Account.class));
    10.         } catch (Exception e) {
    11.             throw new RuntimeException(e);
    12.         }
    13.     }
    14.     public Account findAccountById(Integer id) {
    15.         try {
    16.             return runner.query("select * from account where id=?",new BeanHandler(Account.class),id);
    17.         } catch (Exception e) {
    18.             throw new RuntimeException(e);
    19.         }
    20.     }
    21.     public void saveAccount(Account account) {
    22.         try {
    23.             runner.update("insert into account(name,money) values(?,?)",account.getName(),account.getMoney());
    24.         } catch (Exception e) {
    25.             throw new RuntimeException(e);
    26.         }
    27.     }
    28.     public void updateAccount(Account account) {
    29.         try {
    30.             runner.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
    31.         } catch (Exception e) {
    32.             throw new RuntimeException(e);
    33.         }
    34.     }
    35.     public void deleteAccount(Integer id) {
    36.         try {
    37.             runner.update("delete from account where id=?",id);
    38.         } catch (Exception e) {
    39.             throw new RuntimeException(e);
    40.         }
    41.     }
    42. }

    5.配置文件:bean.xml

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.        xsi:schemaLocation="http://www.springframework.org/schema/beans
    5.         http://www.springframework.org/schema/beans/spring-beans.xsd">
    6.         
    7.     
    8.     <context:component-scan base-package="com.hh"/>
    9.     
    10.     
    11.     <bean id="account" class="com.hh.domain.Account">
    12.         <property name="name" value="zhangsan"/>
    13.         <property name="money" value="2000"/>
    14.     bean>
    15.     
    16.    
    17.     <bean id="accountService" class="com.hh.service.AccountServiceImpl">
    18.        
    19.         <property name="accountDao" ref="accountDao"/>
    20.     bean>
    21.    
    22.     <bean id="accountDao" class="com.hh.dao.AccountDaoImpl">
    23.        
    24.         <property name="runner" ref="queryRunner"/>
    25.     bean>
    26.    
    27.     <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
    28.        
    29.         <constructor-arg name="ds" ref="dataSource"/>
    30.     bean>
    31.    
    32.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    33.        
    34.         <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    35.         <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"/>
    36.         <property name="user" value="root"/>
    37.         <property name="password" value="123"/>
    38.     bean>
    39. beans>

    6.测试类

    1. public class MyTest {
    2.     @Test
    3.     public void testFindAll(){
    4.         //获取Spring核心容器
    5.         ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
    6.         //得到业务层对象
    7.         AccountService service =
    8.                 context.getBean("accountService", AccountService.class);
    9.        List accounts =  service.findAllAccount();
    10.        for(Account account :accounts){
    11.            System.out.println(account);
    12.        }
    13.     }
    14.     @Test
    15.     public void testFindone(){
    16.         //获取Spring核心容器
    17.         ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
    18.         //得到业务层对象
    19.         AccountService service =
    20.                 context.getBean("accountService", AccountService.class);
    21.         Account account = service.findAccountById(3);
    22.         System.out.println(account);
    23.     }
    24.     @Test
    25.     public void testSave(){
    26.         //获取Spring核心容器
    27.         ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
    28.         //得到业务层对象
    29.         AccountService service =
    30.                 context.getBean("accountService", AccountService.class);
    31.         //获取Account对象
    32.         Account account = context.getBean("account",Account.class);
    33.         service.saveAccount(account);
    34.     }
    35.     @Test
    36.     public void testUpdate(){
    37.         //获取Spring核心容器
    38.         ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
    39.         //得到业务层对象
    40.         AccountService service =
    41.                 context.getBean("accountService", AccountService.class);
    42.         context.getBean("account");
    43.         //获取Account对象
    44.         Account account = service.findAccountById(4);
    45.         account.setMoney(3000f);
    46.         service.updateAccount(account);
    47.     }
    48.     @Test
    49.     public void testDelete(){
    50.         //获取Spring核心容器
    51.         ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
    52.         //得到业务层对象
    53.         AccountService service =
    54.                 context.getBean("accountService", AccountService.class);
    55.         context.getBean("account");
    56.         service.deleteAccount(4);
    57.     }
    58. }

    通过注解代替bean.xml配置文件

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.        xsi:schemaLocation="http://www.springframework.org/schema/beans
    5.         http://www.springframework.org/schema/beans/spring-beans.xsd">
    6.         
    7.     
    8.     <context:component-scan base-package="com.hh"/>
    9.     
    10.     
    11.     <bean id="account" class="com.hh.domain.Account">
    12.         <property name="name" value="zhangsan"/>
    13.         <property name="money" value="2000"/>
    14.     bean>
    15.     
    16.    
    17.     <bean id="accountService" class="com.hh.service.AccountServiceImpl">
    18.        
    19.         <property name="accountDao" ref="accountDao"/>
    20.     bean>
    21.  
    22.    
    23.     <bean id="accountDao" class="com.hh.dao.AccountDaoImpl">
    24.        
    25.         <property name="runner" ref="queryRunner"/>
    26.     bean>
    27.    
    28.     <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
    29.        
    30.         <constructor-arg name="ds" ref="dataSource"/>
    31.     bean>
    32.    
    33.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    34.        
    35.         <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    36.         <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"/>
    37.         <property name="user" value="root"/>
    38.         <property name="password" value="123"/>
    39.     bean>
    40. beans>


    既然是使用注解开发,那么思考一个问题,要通过哪些注解把上面的xml文件去掉,不再使用bean.xml呢?

    @Component(“account”)
    使用@Component来创建Account对象,将Account对象放进IOC容器

     代替bean.xml中下面的配置:

    1. <bean id="account" class="com.hh.domain.Account">
    2. bean>

    @Value(“zhangsan”)
    如果不想在测试类中调用setter方法初始化,可以使用注解依赖注入

     代替bean.xml中下面的配置:

    1. <bean id="account" class="com.hh.domain.Account">
    2.         <property name="name" value="zhangsan"/>
    3.         <property name="money" value="2000"/>
    4. bean>

    @Service(“accountDao”)
    使用@Service来创建accountService对象

     代替bean.xml中下面的配置:

    1. <bean id="accountService" class="com.hh.service.AccountServiceImpl">
    2.    
    3.     <property name="accountDao" ref="accountDao"/>
    4. bean>

    @Autowired
    由于AccountServiceImpl中有一个bean对象属性,因此可以通过@Autowired依赖注入,可以省略setter方法


    @Repository(“accountDao”)
    使用@Repository来创建accountDao对象

     

     代替bean.xml中下面的配置:

    1.     <bean id="accountDao" class="com.hh.dao.AccountDaoImpl">
    2.        
    3.         <property name="runner" ref="queryRunner"/>
    4.     bean>

    @Configuration
    作用:指定当前类是一个配置类,作用和bean.xml相同

    1. //该类是一个配置类,作用和bean.xml相同
    2. @Configuration
    3. public class SpringConfig {
    4. }

    @ComponentScan
    作用:用于通过注解指定Spring在创建容器中要扫描的包
    属性:value和bacePackages作用相同,都是指定要扫描的包
    作用等同于xml中的:


    使用@ComponentScan指定要扫描的包

     

     代替bean.xml中下面的配置:

    1.  
    2. <context:component-scan base-package="com.hh"/>


    @Bean
    作用:用于把当前方法的返回值作为bean对象存入Spring容器中
    属性:name:用于指定bean的id,不写时默认为当前方法的名称
    细节:当我们使用注解配置方法时,如果方法有参数,Spring框架回去容器中找有没有可用的bean对象,查找的方式和Autowired一样。

     代替bean.xml中下面的配置:

    1. <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
    2.      
    3.      <constructor-arg name="ds" ref="dataSource"/>
    4.  bean>
    5.  
    6.  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    7.      
    8.      <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    9.      <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"/>
    10.      <property name="user" value="root"/>
    11.      <property name="password" value="123"/>
    12.  bean>
    13.  <bean id="account" class="com.hh.domain.Account">
    14.      <property name="name" value="zhangsan"/>
    15.      <property name="money" value="2000"/>
    16.  bean>

    到目前为止,我们已经把配置文件中所有的配置都去掉了,目前就是下面这个样子,同时就可以吧这个bean.xml删除了:
    bean.xml:

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.        xmlns:context="http://www.springframework.org/schema/context"
    5.        xsi:schemaLocation="http://www.springframework.org/schema/beans
    6.         http://www.springframework.org/schema/beans/spring-beans.xsd
    7.         http://www.springframework.org/schema/context
    8.         http://www.springframework.org/schema/context/spring-context.xsd">
    9. beans>

    测试
    下面我们需要对注解改造过的项目进行测试,那么问题来了,之前我们都是通过配置文件bean.xml来获取Spring的IOC核心容器ApplicationContext,现在注解删了,还怎么获取?
    下面要通过ApplicationContext的另一个实现类:


     

    1. public class MyTest {
    2.        @Test
    3.     public void testSave(){
    4.         //获取Spring核心容器
    5.         ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
    6.         //得到业务层对象
    7.         AccountService service =
    8.                 context.getBean("accountService", AccountService.class);
    9.         //获取Account对象
    10.         Account account = context.getBean("account",Account.class);
    11.         service.saveAccount(account);
    12.     }

     @Import
    当配置类作为容器对象AnnotationConfigApplicationContext的参数时,@Configuration注解可以省略不写


    配置类JDBCConfig:

     

    1. //和Spring连接数据库相关的配置类
    2. public class JDBCConfig {
    3.     //用于创建一个QueryRunner对象
    4.     @Bean(name="queryRunner")
    5.     public QueryRunner createQueryRunner(DataSource dataSource){
    6.         return new QueryRunner(dataSource);
    7.     }
    8.     //创建一个DataSource对象
    9.     @Bean(name="dataSource")
    10.     public DataSource createDataSource() throws PropertyVetoException {
    11.         ComboPooledDataSource ds =  new ComboPooledDataSource();
    12.         ds.setDriverClass("com.mysql.jdbc.Driver");
    13.         ds.setJdbcUrl("jdbc:mysql://localhost:3306/spring");
    14.         ds.setUser("root");
    15.         ds.setPassword("123");
    16.         return ds;
    17.     }
    18. }


    如上图,重新在config包下面写一个配置类JDBCConfig,如果把这个配置类的注解@Configuration省略不写,就要把这个配置类JDBCConfig.class加到容器对象的参数中


    如果在JDBCConfig类上加了@Configuration注解,那么就不需要再容器对象参数中写入JDBCConfig.class

     

    问题来了,现在有两个配置类SpringConfig和JDBCConfig,如果我既不想在JDBCConfig类上加一个@Configuration注解,也不想在容器对象参数中加入JDBCConfig.class呢?这个时候就需要引入一个新注解@Import。把配置类JDBCConfig导入配置类SpringConfig中。

    @Import作用:用于导入其他的配置类
    使用Import注解后,有Import注解的类就是主配置类,导入的都是副配置类

     

    @PropertySource
    现在还有一个问题注意看SpringConfig配置类:

    1. //和Spring连接数据库相关的配置类
    2. public class JDBCConfig {
    3.     //用于创建一个QueryRunner对象
    4.     @Bean(name="queryRunner")
    5.     public QueryRunner createQueryRunner(DataSource dataSource){
    6.         return new QueryRunner(dataSource);
    7.     }
    8.     //创建一个DataSource对象
    9.     @Bean(name="dataSource")
    10.     public DataSource createDataSource() throws PropertyVetoException {
    11.         ComboPooledDataSource ds =  new ComboPooledDataSource();
    12.         ds.setDriverClass("com.mysql.jdbc.Driver");
    13.         ds.setJdbcUrl("jdbc:mysql://localhost:3306/spring");
    14.         ds.setUser("root");
    15.         ds.setPassword("123");
    16.         return ds;
    17.     }
    18. }

     写一个jdbcConfig.properties配置文件

    1. driver=com.mysql.jdbc.Driver
    2. url=jdbc:mysql://localhost:3306/spring
    3. username=root
    4. password=123

    在配置类JDBCConfig中写入这几个属性,并指定值:

     @PropertySource
    作用:用于指定properties文件的位置
    属性:value用于指定文件的名称和路径
    关键字:classPath:表示类路径下

     Spring整合Junit
    1.导入spring整合Junit的jar包

    1. <dependency>
    2.     <groupId>org.springframeworkgroupId>
    3.     <artifactId>spring-testartifactId>
    4.     <version>5.2.0.RELEASEversion>
    5.     <scope>testscope>
    6. dependency>

    2.使用Junit提供的一个注解把原有的main方法替换了,替换成spring提供的@Runwith
    3.告知spring的运行器,spring的ioc的创建是基于xml的还是基于注解的,并说明位置。
    @ContextConfigration
    locations:指定xml文件的位置,加上classpath关键字,表示在类路径下。
    classes:自定注解所在的位置
    总结
    0.实体类

    1. package com.hh.domain;
    2.  
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.beans.factory.annotation.Value;
    5. import org.springframework.stereotype.Component;
    6. //实体类
    7. @Component("account")
    8. public class Account {
    9.     private Integer id;
    10.     @Value("zhangsan")
    11.     private String name;
    12.     @Value("2000f")
    13.     private Float money;
    14.     public void setId(Integer id) {
    15.         this.id = id;
    16.     }
    17.     public void setName(String name) {
    18.         this.name = name;
    19.     }
    20.     public void setMoney(Float money) {
    21.         this.money = money;
    22.     }
    23.     public Integer getId() {
    24.         return id;
    25.     }
    26.     public String getName() {
    27.         return name;
    28.     }
    29.     public Float getMoney() {
    30.         return money;
    31.     }
    32.     @Override
    33.     public String toString() {
    34.         return "Account{" +
    35.                 "id=" + id +
    36.                 ", name='" + name + '\'' +
    37.                 ", money=" + money +
    38.                 '}';
    39.     }
    40. }


    1.业务层接口

    1. public interface AccountService {
    2.     //查询所有
    3.     List findAllAccount();
    4.     //查询一个
    5.     Account findAccountById(Integer id);
    6.     //保存
    7.     void saveAccount(Account account);
    8.     //更新
    9.     void updateAccount(Account account);
    10.     //删除
    11.     void deleteAccount(Integer id);
    12. }

    2.业务层实现类

    1. @Service("accountService")
    2. public class AccountServiceImpl implements AccountService{
    3.     @Autowired
    4.     private AccountDao accountDao;
    5.     public List findAllAccount() {
    6.         return accountDao.findAllAccount();
    7.     }
    8.     public Account findAccountById(Integer id) {
    9.         return accountDao.findAccountById(id);
    10.     }
    11.     public void saveAccount(Account account) {
    12.         accountDao.saveAccount(account);
    13.     }
    14.     public void updateAccount(Account account) {
    15.         accountDao.updateAccount(account);
    16.     }
    17.     public void deleteAccount(Integer id) {
    18.         accountDao.deleteAccount(id);
    19.     }
    20. }

    3.持久层接口

    1. //账户的持久层接口
    2. public interface AccountDao {
    3.     //查询所有
    4.     List findAllAccount();
    5.     //查询一个
    6.     Account findAccountById(Integer id);
    7.     //保存
    8.     void saveAccount(Account account);
    9.     //更新
    10.     void updateAccount(Account account);
    11.     //删除
    12.     void deleteAccount(Integer id);
    13. }

    4.持久层实现类

    1. //持久层实现类
    2. @Repository("accountDao")
    3. public class AccountDaoImpl implements AccountDao {
    4.     @Autowired
    5.     private QueryRunner runner;
    6.     public List findAllAccount() {
    7.         try {
    8.             return runner.query("select * from account",new BeanListHandler(Account.class));
    9.         } catch (Exception e) {
    10.             throw new RuntimeException(e);
    11.         }
    12.     }
    13.     public Account findAccountById(Integer id) {
    14.         try {
    15.             return runner.query("select * from account where id=?",new BeanHandler(Account.class),id);
    16.         } catch (Exception e) {
    17.             throw new RuntimeException(e);
    18.         }
    19.     }
    20.     public void saveAccount(Account account) {
    21.         try {
    22.             runner.update("insert into account(name,money) values(?,?)",account.getName(),account.getMoney());
    23.         } catch (Exception e) {
    24.             throw new RuntimeException(e);
    25.         }
    26.     }
    27.     public void updateAccount(Account account) {
    28.         try {
    29.             runner.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
    30.         } catch (Exception e) {
    31.             throw new RuntimeException(e);
    32.         }
    33.     }
    34.     public void deleteAccount(Integer id) {
    35.         try {
    36.             runner.update("delete from account where id=?",id);
    37.         } catch (Exception e) {
    38.             throw new RuntimeException(e);
    39.         }
    40.     }
    41. }

    5.SpringConfig配置主类

    1. //该类是一个配置类,作用和bean.xml相同
    2. @ComponentScan("com.hh")
    3. @Import(JDBCConfig.class)
    4. @PropertySource("classpath:jdbcConfig.properties")
    5. public class SpringConfig {
    6.     //用于创建一个QueryRunner对象
    7.     @Bean(name="queryRunner")
    8.     public QueryRunner createQueryRunner(DataSource dataSource){
    9.         return new QueryRunner(dataSource);
    10.     }
    11. }

    6.jdbcConfig.properties

    1. driver=com.mysql.jdbc.Driver
    2. url=jdbc:mysql://localhost:3306/spring?serverTimezone=UTC
    3. username=root
    4. password=123

    7.JDBCConfig配置副类

    1. //和Spring连接数据库相关的配置类
    2. public class JDBCConfig {
    3.     @Value("${driver}")
    4.     private String driver;
    5.     @Value("${url}")
    6.     private String url;
    7.     @Value("${username}")
    8.     private String username;
    9.     @Value("${password}")
    10.     private String password;
    11.     //用于创建一个QueryRunner对象
    12.     @Bean(name="queryRunner")
    13.     public QueryRunner createQueryRunner(DataSource dataSource){
    14.         return new QueryRunner(dataSource);
    15.     }
    16.     //创建一个DataSource对象
    17.     @Bean(name="dataSource")
    18.     public DataSource createDataSource() throws PropertyVetoException {
    19.         ComboPooledDataSource ds =  new ComboPooledDataSource();
    20.         ds.setDriverClass(driver);
    21.         ds.setJdbcUrl(url);
    22.         ds.setUser(username);
    23.         ds.setPassword(password);
    24.         return ds;
    25.     }
    26. }

    8.测试类

    1. @RunWith(SpringJUnit4ClassRunner.class)
    2. @ContextConfiguration(classes = SpringConfig.class)
    3. public class AccountServiceTest {
    4.     @Autowired
    5.     private AccountService service=null;
    6.     @Autowired
    7.     private Account account=null;
    8.     @Test
    9.     public void testFindAll(){
    10.        List accounts =  service.findAllAccount();
    11.        for(Account account :accounts){
    12.            System.out.println(account);
    13.        }
    14.     }
    15.     @Test
    16.     public void testFindone(){
    17.         Account account = service.findAccountById(3);
    18.         System.out.println(account);
    19.     }
    20.     @Test
    21.     public void testSave(){
    22.         service.saveAccount(account);
    23.     }
    24.     @Test
    25.     public void testUpdate(){
    26.         account.setMoney(3000f);
    27.         service.updateAccount(account);
    28.     }
    29.     @Test
    30.     public void testDelete(){
    31.         service.deleteAccount(4);
    32.     }
    33. }
  • 相关阅读:
    .NET周报 【2月第2期 2023-02-11】
    “优化STM32单片机处理大量网络数据的方法“
    py12_[接口全网最通俗易懂的一篇] Python 真假接口/抽象类/抽象方法/多态/实例级别解析
    CSS简单的图片居中
    深入浅出索引(下)
    运行软件找不到mfc140u.dll怎么解决,mfc140u.dll是什么文件
    LogTAD:无监督跨系统日志异常域检测
    字节跳动端智能工程链路 Pitaya 的架构设计
    毕业那年的大学生创新立项
    docker搭建fastdfs环境
  • 原文地址:https://blog.csdn.net/you4580/article/details/127644931