程序员社区

Spring基于XML开发案例

文章目录

      • 0.实体类
      • 1.业务层接口
      • 2.业务层实现类
      • 3.持久层接口
      • 4.持久层实现类
      • 5.配置文件:bean.xml
      • 6.测试类

数据库:

create table account(
	id int primary key auto_increment,
	name varchar(40),
	money float
)character set utf8 collate utf8_general_ci;

insert into account(name,money) values('aaa',1000);
insert into account(name,money) values('bbb',1000);
insert into account(name,money) values('ccc',1000);

在这里插入图片描述
项目的目录结构:
在这里插入图片描述
pom.xml:

<dependencies>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-dbutils/commons-dbutils -->
        <dependency>
            <groupId>commons-dbutils</groupId>
            <artifactId>commons-dbutils</artifactId>
            <version>1.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

0.实体类

public class Account {
    private Integer id;
    private String name;
    private Float 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 Float getMoney() {
        return money;
    }

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

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

1.业务层接口

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

2.业务层实现类

//业务层实现类,业务层调用持久层
public class AccountServiceImpl implements AccountService{
    private AccountDao accountDao;

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

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

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

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

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

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

3.持久层接口

//账户的持久层接口
public interface AccountDao {
    //查询所有
    List<Account> findAllAccount();
    //查询一个
    Account findAccountById(Integer id);
    //保存
    void saveAccount(Account account);
    //更新
    void updateAccount(Account account);
    //删除
    void deleteAccount(Integer id);
}

4.持久层实现类

//持久层实现类
public class AccountDaoImpl implements AccountDao {
    private QueryRunner 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(Integer id) {
        try {
            return runner.query("select * from account where id=?",new BeanHandler<Account>(Account.class),id);
        } 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(Integer id) {
        try {
            runner.update("delete from account where id=?",id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

5.配置文件:bean.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">

    <!--配置AccountService-->
    <bean id="accountService" class="com.hh.service.AccountServiceImpl">
        <!--注入accountDao-->
        <property name="accountDao" ref="accountDao"/>
    </bean>

    <!--配置AccountDao-->
    <bean id="accountDao" class="com.hh.dao.AccountDaoImpl">
        <!--注入QueryRunner-->
        <property name="runner" ref="queryRunner"/>
    </bean>

    <!--配置QueryRunner-->
    <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
        <!--注入数据源-->
        <constructor-arg name="ds" ref="dataSource"/>
    </bean>

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

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

6.测试类

public class MyTest {
    @Test
    public void testFindAll(){
        //获取Spring核心容器
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        //得到业务层对象
        AccountService service =
                context.getBean("accountService", AccountService.class);
       List<Account> accounts =  service.findAllAccount();
       for(Account account :accounts){
           System.out.println(account);
       }
    }

    @Test
    public void testFindone(){
        //获取Spring核心容器
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        //得到业务层对象
        AccountService service =
                context.getBean("accountService", AccountService.class);
        Account account = service.findAccountById(3);
        System.out.println(account);
    }

    @Test
    public void testSave(){
        //获取Spring核心容器
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        //得到业务层对象
        AccountService service =
                context.getBean("accountService", AccountService.class);
        //获取Account对象
        Account account = context.getBean("account",Account.class);
        service.saveAccount(account);
    }

    @Test
    public void testUpdate(){
        //获取Spring核心容器
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        //得到业务层对象
        AccountService service =
                context.getBean("accountService", AccountService.class);
        context.getBean("account");
        //获取Account对象
        Account account = service.findAccountById(4);
        account.setMoney(3000f);
        service.updateAccount(account);
    }

    @Test
    public void testDelete(){
        //获取Spring核心容器
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        //得到业务层对象
        AccountService service =
                context.getBean("accountService", AccountService.class);
        context.getBean("account");
        service.deleteAccount(4);
    }
}

赞(0) 打赏
未经允许不得转载:IDEA激活码 » Spring基于XML开发案例

相关推荐

  • 暂无文章

一个分享Java & Python知识的社区