前言:本篇博客主要對MVC架構、Mybatis工程加深下理解,前面寫過一篇博客:SprintBoot+html/css/js+mybatis的demo,里面涉及到了Mybatis的應用,此篇博客主要介紹一種將sql語句寫到了配置文件里的方法,即Mybatis里Mapper.xml文件配置,其主要用于定義sql語句和映射關系
目錄
MVC架構流程圖
配置文件
mapper層
model層
service層?
controller層
結果展示
MVC架構流程圖
根據自己的理解畫了一下訪問接口時,涉及到service層、mapper層、mybatis工程的應用
上篇博客地址:
https://blog.csdn.net/MRJJ_9/article/details/131884585
配置文件
xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:填寫映射當前的Mapper接口,所有的增刪改查的參數(shù)和返回值類型,
就可以直接填寫縮寫,不區(qū)分大小寫,直接通過方法名去找類型-->
<mapper namespace="com.example.interfaceautotest.mapper.CaseMapper">
<!-- id 對應的是mapper.CaseMapper里的方法名-->
<select id="getInfoByPhone" resultType="com.example.interfaceautotest.model.MysqlUserData">
select * from user where phone =#{phone} and pw =#{pw} </select>
</mapper>
在application.properties文件里完成對應的配置
spring.datasource.url=jdbc:mysql://localhost:3306/auto_test_data?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
mybatis.mapper-locations=classpath:/mapper/*.xml
mapper層
mapper里的方法
package com.example.interfaceautotest.mapper;
import com.example.interfaceautotest.model.MysqlUserData;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface CaseMapper {
MysqlUserData getInfoByPhone(@Param("phone") String phone,
@Param("pw") String pw);
}
model層
model層與數(shù)據庫的關聯(lián)
package com.example.interfaceautotest.model;
//注冊表
public class MysqlUserData{
private String id;
private String usr;
private String pw;
private String phone;
private String email;
public String getId(){
return id;
}
public void setId(String id){
this.id = id;
}
public String getUsr(){
return usr;
}
public void setUsr(String usr){
this.usr = usr;
}
public String getPw(){
return pw;
}
public void setPw(String pw){
this.pw = pw;
}
public String getPhone(){
return phone;
}
public void setPhone(String phone){
this.phone = phone;
}
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email = email;
}
}
model層與service層關聯(lián)的返回結果
package com.example.interfaceautotest.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {
public int code;
public String msg;
public Object data;
public int getCode(){
return code;
}
public void setCode(int code){
this.code = code;
}
public String getMsg(){
return msg;
}
public void setMsg(String msg){
this.msg = msg;
}
public Object getData(){
return data;
}
public void setData(Object data){
this.data = data;
}
}
service層?
package com.example.interfaceautotest.service.impl;
import com.example.interfaceautotest.mapper.CaseMapper;
import com.example.interfaceautotest.model.MysqlUserData;
import com.example.interfaceautotest.model.Result;
import com.example.interfaceautotest.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service("UserSevice")
public class UserServiceImpl implements UserService {
@Resource
CaseMapper CaseMapper;
public Result login(String username, String password) {
if (username == null || password == null)
return new Result(-1,"用戶名或密碼不能為空","用戶名或密碼不能為空");
MysqlUserData user = CaseMapper.getInfoByPhone(username, password);
if (user==null){
return new Result(-3,"用戶名或密碼錯誤","用戶名或密碼錯誤");
}
else {
return new Result(1,"登錄成功",user);
}
}
}
controller層
package com.example.interfaceautotest.controller;
import com.example.interfaceautotest.model.Result;
import com.example.interfaceautotest.service.UserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@RestController
@RequestMapping("/test")
public class Login {
@Resource
UserService UserService;
@GetMapping("/login")
public Result login(String username, String password){
return UserService.login(username,password);
}
}
結果展示
傳入的參數(shù)phone和pw在數(shù)據庫里可以查詢到
?
傳入的參數(shù)phone和pw在數(shù)據庫里不可以查詢到?文章來源:http://www.zghlxwxcb.cn/news/detail-636866.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-636866.html
到了這里,關于(MVC)SpringBoot+Mybatis+Mapper.xml的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!