SSM框架整合(2)—— Spring框架

Spring框架

结构目录

  • java

    • dao
      • AccountDao.java(持久层)
    • domain
      • Account.java(JavaBean对象)
    • service
      • AccountService.java(业务层)
      • AccountServiceImp.java(业务层)
    • test
      • TestSpring.java(测试文件)
  • resources

    • applicationContext.xml(Spring配置文件)

Spring配置文件

  • 开启注解扫描
    • 忽略表现层注解

applicationContext.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"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

        <!-- 开启注解扫描 -->
            <!-- 使用Spring框架处理业务层(service)和持久层(dao),不处理表现层(Controller) -->
        <context:component-scan base-package="cn.water">
            <!-- 配置不扫描的注解类 -->
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
  </beans>

JavaBean对象

Accont.java

package cn.water.domain;

import java.io.Serializable;

public class Account implements Serializable {

    private Integer id;
    private String name;
    private Double money;

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + 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 Double getMoney() {
        return money;
    }

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

持久层

  • MyBatis框架生成代理对象,我们不需要创建实现类

AccountDao.java

package cn.water.dao;

import cn.water.domain.Account;

import java.util.List;

public interface AccountDao {

    /** 查询所有用户 */
    public abstract List<Account> findAll();

    /** 添加用户 */
    public abstract void add(Account account);

}

业务层

AccountService.java

package cn.water.service;

import cn.water.domain.Account;

import java.util.List;

public interface AccountService {

    /** 查询所有用户 */
    public abstract List<Account> findAll();

    /** 添加用户 */
    public abstract void add(Account account);

}

AccountServiceImp.java

package cn.water.service;

import cn.water.domain.Account;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("accountService")
public class AccountServiceImp implements AccountService{

    /** 查询所有用户 */
    @Override
    public List<Account> findAll() {
        System.out.println("业务层:查询所有用户");
        return null;
    }

    /** 添加用户 */
    @Override
    public void add(Account account) {
        System.out.println("业务层:添加用户");
    }
}

测试

TestSpring.java

package cn.water.test;

import cn.water.service.AccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {
    
    @Test
    public void run01() {
//        加载配置文件
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//        获取对象
        AccountService as = (AccountService) ac.getBean("accountService");
//        调用方法
        as.findAll();
    }
}

运行结果

业务层:查询所有用户
发布了68 篇原创文章 · 获赞 2 · 访问量 1898

猜你喜欢

转载自blog.csdn.net/qq_40981851/article/details/104218726