xml配置Spring

1.导入坐标

<?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>org.example</groupId>
    <artifactId>SpringAnLi</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>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</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>
            <scope>test</scope>
        </dependency>

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


</project>

2.写Account实体类

package domain;

import java.io.Serializable;

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

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Integer getMoney() {
        return money;
    }

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

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

3.写持久层和service层的接口和实现类

package dao;

import domain.Account;

import java.sql.SQLException;
import java.util.List;

public interface IAccountDao {
    void save(Account account);



    List<Account> findAll() throws SQLException;

}

package dao;

import domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class IAccountImpl implements IAccountDao {
    private QueryRunner runner;
    public void setRunner(QueryRunner runner){
        this.runner=runner;

    }

    public void save(Account account) {
        try {
            runner.update("insert into account(name,money) value(?,?)",
                    account.getName(),account.getMoney());
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    public List<Account> findAll() throws SQLException {

            return runner.query("select* from account",
                    new BeanListHandler<Account>(Account.class)
                    );
        }

}

package service;

import domain.Account;

import java.sql.SQLException;
import java.util.List;

public interface IAccountService {
    void saveAccount(Account account);



    List<Account> findAllAccount() throws SQLException;
}

package service;

import dao.IAccountDao;
import domain.Account;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

public class AccountServiceImpl implements IAccountService {
    private IAccountDao accountDao;

    public void setAccountDao(IAccountDao accountDao){
        this.accountDao=accountDao;
    }


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

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

4.配置xml

<?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">

    <!--配置service-->

    <bean id="accountService" class="service.AccountServiceImpl">

        <!--注入dao-->
        <property name="accountDao" ref="accountDao"></property>
    </bean>

    <!--配置dao对象-->
    <bean id="accountDao" class="dao.IAccountImpl">
        <property name="runner" ref="runner"></property>
    </bean>


    <!--配置QueryRunner-->
    <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
        <!--注入数据源-->
        <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:///mybatis"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>





</beans>

5.编写测试类

import domain.Account;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.IAccountService;

import java.sql.SQLException;
import java.util.List;

public class testDemo {

    @Test
    public void testSaveAccount()
    {   Account account = new Account();
    account.setName("keke");
    account.setMoney(9999);
    ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
    IAccountService as = ac.getBean("accountService",IAccountService.class);
        as.saveAccount(account);
        System.out.println(as);
    }

    @Test
    public void findAll() throws SQLException {
        ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
        IAccountService as=ac.getBean("accountService",IAccountService.class);
        List<Account> allAccount = as.findAllAccount();
        System.out.println(allAccount);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
在使用 Spring 框架时,我们可以通过 XML 配置文件来配置 Spring 容器及其管理的对象。下面是一个简单的 Spring XML 配置示例: ``` <?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 id="myBean" class="com.example.MyBean"> <property name="property1" value="value1" /> <property name="property2" value="value2" /> </bean> </beans> ``` 在这个示例中,我们定义了一个名为 "myBean" 的 Spring Bean,它的实现类是 com.example.MyBean。我们还为这个 Bean 设置了两个属性,分别是 "property1" 和 "property2",它们的值分别是 "value1" 和 "value2"。 要在代码中使用这个 Bean,我们可以使用以下代码: ``` ApplicationContext context = new ClassPathXmlApplicationContext("path/to/config.xml"); MyBean myBean = (MyBean) context.getBean("myBean"); ``` 这里我们首先创建了一个 ApplicationContext 对象,它会加载我们的配置文件。然后我们通过 getBean() 方法获取了名为 "myBean" 的 Bean,并将其转换成了 MyBean 类型的对象。 这只是一个简单的示例,实际中的 Spring XML 配置可能会更加复杂。但是无论如何,XML 配置都是 Spring 框架中的一个重要特性,它可以让我们更加灵活地管理应用程序中的对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值