20200111——基于xml的ioc案例

配置pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>day02_eesy_02account_xmlioc</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <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>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>

    </dependencies>

</project>

文件目录结构
有一个持久层的实体类Dao
有一个service的接口,有一个接口的实现类

实体类代码

package com.itheima.domain;

import java.io.Serializable;

/**
 * @Classname Account
 * @Description 账户的实体类
 * @Date 2020/1/11 20:50
 * @Created by mmz
 */
public class Account  implements Serializable {
    private int id;
    private String name;
    private float money;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public float getMoney() {
        return money;
    }

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

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

service接口

package com.itheima.service;

import com.itheima.domain.Account;

import java.util.List;

/**
 * @Classname IAccountService
 * @Description 账户的业务层接口
 * @Date 2020/1/11 20:49
 * @Created by mmz
 */
public interface IAccountService {
    //查询索引
    List<Account> findAccount();

    //查询一个
    Account findAccountById(int accoutId);

    //保存操作
    void saveAccount(Account account);

    //更新
    void updateAccount(Account account);

    //删除
    void deleteAccount(int accoutId);
}

service接口的实现类

package com.itheima.service.impl;

import com.itheima.domain.Account;
import com.itheima.service.IAccountService;

import java.util.List;

/**
 * @Classname AccoutServiceImpl
 * @Description TODO
 * @Date 2020/1/11 20:54
 * @Created by mmz
 */
public class AccoutServiceImpl implements IAccountService {
    public List<Account> findAccount() {
        return null;
    }

    public Account findAccountById(int accoutId) {
        return null;
    }

    public void saveAccount(Account account) {

    }

    public void updateAccount(Account account) {

    }

    public void deleteAccount(int accoutId) {

    }
}

提示:右键generate implement method 可以引入接口的方法

完成Dao接口的实现类

package com.itheima.dao.impl;

import com.itheima.dao.IAccountDao;
import com.itheima.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;

/**
 * @Classname AccoutDaoImpl
 * @Description 账户持久层的实现类
 * @Date 2020/1/11 21:42
 * @Created by mmz
 */
public class AccoutDaoImpl implements IAccountDao {
    private QueryRunner runner;

    public QueryRunner getRunner() {
        return runner;
    }

    public void setRunner(QueryRunner runner) {
        this.runner = 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(int accoutId) {
        try{
            return runner.query("select * from account where id = ?",new BeanHandler<Account>(Account.class),accoutId);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public void saveAccount(Account account) {
        try{
            runner.update("insert into account(name,money) values(?,?)",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(int accoutId) {
        try{ 
            runner.update("delete from   account  where id = ?",accoutId);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}

最主要的配置bean.xml文件
bean的依赖

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

配置每一个bean

 <bean id="accountService" class="com.itheima.service.impl.AccoutServiceImpl">
            <!--注入dao对象-->
            <property name="iAccountDao" ref="accoutDao"></property>
        </bean>
        <bean id="accoutDao" class="com.itheima.dao.impl.AccoutDaoImpl">
            <property name="runner" ref="runner"></property>
        </bean>
    <!--配置QueryRunner对象-->
    <bean id="runner" class="org.apache.commons.dbutils.QueryRunner">
        <constructor-arg name="ds" ref="dataSource"></constructor-arg>
    </bean>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--连接数据库的必备信息-->
        <property name="driverClass" value="com.mysql.jdbc.driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql//localhost:3306/eesy"></property>
        <property name="user" value="root"></property>
        <property name="password" value="password"></property>
    </bean>

利用构造方法配置就是bean中,用到constructor-arg标签,set方法就是property这种标签来定义

在这里插入图片描述

运行成功
提示:mysql如果是高版本的,要配置高版本的驱动

        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy?serverTimezone=GMT%2B8&amp;characterEncoding=utf-8"></property>

数据库配置的时候要这么写

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,基于XML实现IOC的例子如下: 假设我们有一个User类,它有一个name属性和一个UserService类,它有一个findUserByName方法,我们想要使用IOC容器来实现自动化依赖注入。 首先,在配置文件中定义UserService bean如下: ``` <bean id="userService" class="com.example.UserService"/> ``` 然后,我们定义User bean如下: ``` <bean id="user" class="com.example.User"> <property name="name" value="John"/> </bean> ``` 我们可以看到,在User bean中,我们将name属性设置为"John"。 现在,在UserService类中,我们将依赖User对象并使用findUserByName方法: ``` public class UserService { private User user; // Setter method for user public void setUser(User user) { this.user = user; } public void findUserByName() { System.out.println("Finding user by name: " + user.getName()); } } ``` 注意,我们提供了一个setter方法来设置user属性,这意味着我们可以在配置文件中注入User bean。 最后,在我们的应用程序中,我们可以使用IOC容器获取UserService bean并调用findUserByName方法: ``` // Load the application context from the XML configuration file ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // Get the userService bean from the context UserService userService = (UserService) context.getBean("userService"); // Call the findUserByName method userService.findUserByName(); ``` 当我们运行这个应用程序时,它将输出以下内容: ``` Finding user by name: John ``` 这就是基于XML实现IOC的例子!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值