Model层下接口以及工具类

工具类

public class MyApplication extends Application {

    public static GetDataInterface getDataInterface;

    @Override
    public void onCreate() {
        super.onCreate();
        //1.  用于全局配置初始化Fresco 图片加载
        Fresco.initialize(this);
        //2.  用于全局配置初始化Retrofit 网络请求
        //构建Retrofit类,初始化参数
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://result.eolinker.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        //3.//创建网络请求接口实例
        getDataInterface = retrofit.create(GetDataInterface.class);
    }
}

MyModel 层

public class MyModel {


    public void getDataM(final ModelCallBack modelCallBack){
        Call<DataDataBean> into = MyApplication.getDataInterface.getInto();
        //发起异步请求
        into.enqueue(new Callback<DataDataBean>() {
            @Override
            public void onResponse(Call<DataDataBean> call, Response<DataDataBean> response) {
                //获取响应的数据
                if (response.body()!=null&&response.isSuccessful()){
                    DataDataBean dataDataBean = response.body();
                    modelCallBack.onSuccess(dataDataBean);
                }
            }

            @Override
            public void onFailure(Call<DataDataBean> call, Throwable t) {
                //请求失败时返回数据
                modelCallBack.onFailure(new Exception(""));
            }
        });
    }
}

DateDataBean  数据接口


Model层下接口

public interface ModelCallBack {
    public void onSuccess(DataDataBean dataDataBean);
    public void onFailure(Exception  e);
}

Present层下 


import xieshuaikang.com.www.zhoukao1_moni_20171230.Model.DataDataBean;
import xieshuaikang.com.www.zhoukao1_moni_20171230.Model.Interfac.ModelCallBack;
import xieshuaikang.com.www.zhoukao1_moni_20171230.Model.MyModel;
import xieshuaikang.com.www.zhoukao1_moni_20171230.View.Fragment.ShouYe_Fragment;
import xieshuaikang.com.www.zhoukao1_moni_20171230.View.Interfa.MyView;

/**
 * Created by Administrator on 2017/12/30.
 */

public class MyPresenter {

    private  MyModel myModel;
    MyView myView;

    public MyPresenter(MyView myView) {
        myModel = new MyModel();
        this.myView = myView;
    }


    public void getData() {

        myModel.getDataM(new ModelCallBack() {
            @Override
            public void onSuccess(DataDataBean dataDataBean) {
                //数据交互时,为防止内存泄露,设置view层数据为空
                if (myView!=null){
                    myView.onSuccess(dataDataBean);
                }
            }

            @Override
            public void onFailure(Exception e) {
                //数据交互时,为防止内存泄露,设置view层数据为空
                if (myView != null){
                    myView.onFailure(e);
                }
            }

        });
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MybatisPlus是对Mybatis进行了增强的工具类,简化了Mybatis的开发,提高了开发效率。下面是一个使用MybatisPlus工具类的示例: 1. 首先需要引入MybatisPlus依赖,可以在pom.xml文件中添加以下依赖: ```xml <!-- MybatisPlus依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> ``` 2. 创建一个实体类,该实体类需要继承MybatisPlus提供的Model类,并使用MybatisPlus提供的注解来指定对应的数据库表和字段。 ```java import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.extension.activerecord.Model; @TableName("t_user") public class User extends Model<User> { @TableId(value = "id", type = IdType.AUTO) private Long id; @TableField("username") private String username; @TableField("password") private String password; // 省略getter和setter方法 } ``` 3. 创建一个Mapper接口,该接口需要继承MybatisPlus提供的BaseMapper接口。 ```java import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.demo.entity.User; public interface UserMapper extends BaseMapper<User> { } ``` 4. 在Service中调用MybatisPlus提供的方法来进行数据库操作。 ```java import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.example.demo.entity.User; import com.example.demo.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService { @Autowired private UserMapper userMapper; public List<User> getUserList() { return userMapper.selectList(null); } public User getUserById(Long id) { return userMapper.selectById(id); } public User getUserByUsername(String username) { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("username", username); return userMapper.selectOne(queryWrapper); } public int addUser(User user) { return userMapper.insert(user); } public int updateUser(User user) { return userMapper.updateById(user); } public int deleteUserById(Long id) { return userMapper.deleteById(id); } } ``` 以上就是一个使用MybatisPlus工具类的示例,通过MybatisPlus的封装,可以大大减少开发者的代码量,提高开发效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值