天天看点

Spring基于注解的IOC-JDBC案例

IOC

    • 注解笔记
    • 导入依赖
    • 1、账户实体类
    • 2、模拟业务层
      • 业务层接口
      • 业务层实现类 -(用到IOC注解)
    • 3、模拟持久层
      • 持久层接口
      • 持久层实现类-(用到IOC注解)
    • 4、配置类 (原先是bean.xml)
    • 5、实现层 (test)
    • 6、运行结果

注解笔记

  • 注解:
  • 用于创建对象的
  • @Componcnt
               
  • @Controller @Service @Repository  明确三层架构
               
  • 用于注入数据的
  • 注入其他bean类型数据
               
  • @Autowired  自动按照类型注入    使用时会去容器里面找匹配的变量类型如果只有一个可以使用匹配成功     如果有两个需要更改变量名来区别匹配
               
  • @Qualifer   按照名称注入 不能单独使用
               
  • @Resource   直接id注入 可以单独使用 value指定bean的ID
               
  • 注入基本类型 string
               
  • @value      属性value 用于知道数据的值 用${}
               
  • 用于改变作用范围
  • @Scope 属性value 指定取值范围 singleton单例 prototype多例
               

导入依赖

<dependencies>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>5.0.2.RELEASE</version>
       </dependency>

       <dependency>
           <groupId>commons-dbutils</groupId>
           <artifactId>commons-dbutils</artifactId>
           <version>1.4</version>
       </dependency>

       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <version>8.0.11</version>
       </dependency>

       <dependency>
           <groupId>c3p0</groupId>
           <artifactId>c3p0</artifactId>
           <version>0.9.1.2</version>
       </dependency>

   </dependencies>
           

1、账户实体类

public class Account implements Serializable {
    private Integer id;
    private String name;
    private Float money;

    public Float getMoney() {
        return money;
    }

    public void setMoney(Float money) {
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}
           

2、模拟业务层

业务层接口

public interface IAccountService {
    List<Account> findAllAccount();//查询所有
    Account findAccountById(Integer accountId);//查询一个
    void saveAccount(Account account);//保存
    void updateAccount(Account account);//更新
    void deleteAccount(Integer accountId);//删除

}
           

业务层实现类 -(用到IOC注解)

//@Componcnt也可以
@Service("accountService")
public class AccountServceImpl implements IAccountService {


//    注解注入 时唯一的 不用set
    @Autowired
    private IAccountDao accountDao;


    public List<Account> findAllAccount() {
        return accountDao.findAllAccount();
    }

    public Account findAccountById(Integer accountId) {
        return accountDao.findAccountById(accountId);
    }

    public void saveAccount(Account account) {
        accountDao.saveAccount(account);
    }

    public void updateAccount(Account account) {
        accountDao.updateAccount(account);

    }

    public void deleteAccount(Integer accountId) {
        accountDao.deleteAccount(accountId);
    }

}
           

3、模拟持久层

持久层接口

public interface IAccountDao {
    List<Account> findAllAccount();//查询所有
    Account findAccountById(Integer accountId);//查询一个
    void saveAccount(Account account);//保存
    void updateAccount(Account account);//更新
    void deleteAccount(Integer accountId);//删除
}

           

持久层实现类-(用到IOC注解)

//@Componcnt也可以
@Repository("AccountDao")
public class AccountDaoImpl implements IAccountDao {

    //    注解注入 时唯一的 不用set
    @Autowired
    private QueryRunner runner;


    public List<Account> findAllAccount() {
        try {
            return runner.query("select * from account",new BeanListHandler<Account>(Account.class));

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public Account findAccountById(Integer accountId) {
        try {
            return runner.query("select * from account where id=?",new BeanHandler<Account>(Account.class),accountId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void saveAccount(Account account) {
        try {
            runner.update("insert into account(name,money) value(?,?)",account.getName(),account.getMoney());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void updateAccount(Account account) {
        try {
            runner.update("update account set name=? ,money=? where id=?",account.getName(),account.getMoney(),account.getId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void deleteAccount(Integer accountId) {
        try {
            runner.update("delete from account where id=?",accountId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
           

4、配置类 (原先是bean.xml)

@Configuration //当前类是配置类
@ComponentScan("com")//指定创建容器 要扫描的包
public class SpringConfigurntion {
    @Bean(name="runner")//当前的方法返回值存入容器中
    public QueryRunner createQueryRunner(DataSource dataSource){
        return new QueryRunner(dataSource);
    }

    @Bean(name = "dataSource")
    public DataSource createDataSource(){
        try {
            ComboPooledDataSource ds=new ComboPooledDataSource();
            ds.setDriverClass("com.mysql.cj.jdbc.Driver");
            ds.setJdbcUrl("jdbc:mysql://localhost:3306/cwz?serverTimezone = UTC");
            ds.setUser("root");
            ds.setPassword("123456");
            return ds;
        } catch (Exception e) {

            throw new RuntimeException(e);
        }

    }

}
           

5、实现层 (test)

public class testmain {

    public static void main(String[] args) {
        System.out.printf("111111111");

        //取消xml后的 获取容器
        ApplicationContext ac=new AnnotationConfigApplicationContext(SpringConfigurntion.class);

        //得到业务层对象
        IAccountService as=ac.getBean("accountService",IAccountService.class);
//        IAccountDao as1= ac.getBean("AccountDao", IAccountDao.class);

        List<Account> accounts =as.findAllAccount();
        for (Account account : accounts){
            System.out.printf(String.valueOf(account));
        }
//        System.out.printf(String.valueOf(as1));
    }
}
           

6、运行结果

Spring基于注解的IOC-JDBC案例