网站建设资讯

NEWS

网站建设资讯

spring实例参考02-一个有基本框架雏形的实例-创新互联

该文供以下文章的参考内容:

波密ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!

Spring笔记https://blog.csdn.net/qq_34378496/article/details/128095043

bean类:

package com.evenif.bean;

public class Index {

    private int id;
    private String name;

    public Index() {
        System.out.println("create Index class");
    }

    public Index(int id, String name) {
        this.id = id;
        this.name = name;
    }

    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;
    }

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

dao层:

package com.evenif.dao;
import com.evenif.bean.Index;
public interface IndexDao {
    public Index getIndex();
}

dao层实现:

实现1:

package com.evenif.dao.impl;

import com.evenif.bean.Index;
import com.evenif.dao.IndexDao;

public class IndexMysqlDaoImpl implements IndexDao {
    @Override
    public Index getIndex() {
        // 假装是从数据库获取的Index对象
        return new Index(2,"mysql02");
    }
}

实现2

package com.evenif.dao.impl;

import com.evenif.bean.Index;
import com.evenif.dao.IndexDao;

public class IndexOralceDaoImpl implements IndexDao {
    @Override
    public Index getIndex() {
        // 假装是从数据库获取的Index对象
        return new Index(3,"oralce03");
    }
}

service层:

package com.evenif.service;

import com.evenif.bean.Index;

public interface IndexService {
    public Index getIndex();
}

service层实现:

package com.evenif.service.impl;

import com.evenif.bean.Index;
import com.evenif.dao.IndexDao;
import com.evenif.service.IndexService;

public class IndexServiceImpl implements IndexService {
    private IndexDao indexDao;

    public void setIndexDao(IndexDao indexDao) {
        this.indexDao = indexDao;
    }

    @Override
    public Index getIndex() {
        return indexDao.getIndex();
    }
}

测试:

package com.evenif;

import com.evenif.service.IndexService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
        //注意这个xml文件位置要放对
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        IndexService indexService = (IndexService) context.getBean("indexService");
        System.out.println(indexService.getIndex());
    }
}
//执行结果:
//Index{id=2, name='mysql02'}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


分享标题:spring实例参考02-一个有基本框架雏形的实例-创新互联
链接分享:http://cdweb.net/article/dojedh.html