SpringBoot(二)

自定义类型的查询以及返回

1、java定义接收数据库中数据的类

public class User {
    private int id;
    private String nick;
    private String sex;
    private int age;
    private String photo;
    ...
}

2 、定义获取该对象的查询方法

public User searchUserByPsw(@Param("acc") String account, @Param("psw") String psw);

3 、查询的具体实现

表和类之间关系映射
<resultMap type="com.example.springdemo.bean.User"
id="userResut">
    <id property="id" column="id" />
    <result property="nick" column="nick" />
    <result property="sex" column="sex" />
    <result property="age" column="age" />
    <result property="photo" column="photo" />
</resultMap>
查询将结果映射关系设置到查询 resultMap 属性上
<select id="searchUserByPsw" resultMap="userResut">
    select * from users where
    psw=#{psw} and (nick=#{acc} or id=#{acc})
</select>

4 、使用

@RequestMapping("/login")
public String login(String account, String psw) {
    User user = userMapper.searchUserByPsw(account, psw);
    if(user != null) {
        // 登录成功
        return Commom.success(user);
    }else {
        // 登录失败
        return Commom.fail(null);
    }
}

在客户端全局变量的实现

1 、自定义 Application 类,继承 Application ,注册到 manifest 中

package com.example.httpdemo;
import android.app.Application;
import com.example.bean.User;
public class MyApp extends Application {
    // 已经登录的用户信息
    private User loginUser;
    public User getLoginUser() {
        return loginUser;
    }
    public void setLoginUser(User loginUser) {
        this.loginUser = loginUser;
    }
    public boolean isLogin() {
        return null != loginUser;
    }
}
注册:
<application
    android:name="com.example.httpdemo.MyApp"
    ...>
    ...
</application>

2 、某个位置记录数据,如登录成功,记录已登录的用户信息

User u = (User) msg.obj;
// 记录到应用全局变量中
((MyApp)getApplication()).setLoginUser(u);

3 、其他页面取出内容,只要通过 context.getApplication() 得到应用对象,然后通过其方法获取内容即可

public class ContentActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        MyApp app = (MyApp) getApplication();
        User u = app.getLoginUser();
        tvInfo.setText(u.toString());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值