哈哈哈哈哈哈哈👌话嘿嘿👌h
我的
1111
MenuDao层:
package com.example.demo.dao;
import com.example.demo.bean.MainMenu;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface MenuDao {
public List<MainMenu> getMenus();
}MenuController层:
package com.example.demo.logincontroller;
import com.alibaba.fastjson.JSON;
import com.example.demo.bean.MainMenu;
import com.example.demo.dao.MenuDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
@RestController
public class MenuController {
@Autowired
MenuDao menuDao;
@RequestMapping(value="/menus")
public String getAllMenus(){
System.out.println("访问成功!!!");
HashMap<String, Object> data = new HashMap<>();
List<MainMenu> menus = menuDao.getMenus();
if (menus != null){
data.put("menus", menus);
data.put("flag", 200);
}else{
data.put("flag", 404);
}
String s = JSON.toJSONString(data);
return s;
}
}
MenuMainMapper。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"> <mapper namespace="com.example.demo.dao.MenuDao"> <!-- 关系映射--> <resultMap id="menuMap" type="com.example.demo.bean.MainMenu"> <id column="id" property="id"></id> <result column="title" property="title"></result> <result column="path" property="path"></result> <collection property="sList" ofType="com.example.demo.bean.SubMenu"> <id column="sid" property="id"></id> <result column="stitle" property="title"></result> <result column="spath" property="path"></result> </collection> </resultMap> <select id="getMenus" resultMap="menuMap"> select mm.*,sm.id as sid ,sm.title as stitle ,sm.path as spath from vue_mainmenu mm ,vue_submenu sm where mm.id = mid; </select> </mapper>
Controller层:
package com.example.demo.logincontroller;
import com.alibaba.fastjson.JSON;
import com.example.demo.bean.UserName;
import com.example.demo.dao.UserNameDao;
//import jdk.nashorn.internal.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
@RestController
public class LoginController {
@Autowired
UserNameDao userNameDao;
@RequestMapping(value="/Login")
public String login(@RequestBody UserName username){
String flag = "error";
UserName userName = userNameDao.getUserNameByMassage(username.getUsername(), username.getPassword());
HashMap<String, Object> res = new HashMap<>();
if(userName != null){
flag = "hello world!!!";
}
res.put("flag", flag);
res.put("username", username);
String res_json = JSON.toJSONString(res);
// System.out.println(userName);
return res_json;
}
}UserNameDao层:
package com.example.demo.dao;
import com.example.demo.bean.UserName;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface UserNameDao {
public UserName getUserNameByMassage(@Param("username") String username, @Param("password") String password);
}解决跨域:
package com.example.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* 解决跨域问题
*/
@Configuration
public class Config extends WebMvcConfigurerAdapter {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("*")
.allowCredentials(true)
.maxAge(3600);
}
}实体层bean:
package com.example.demo.bean;
public class UserName {
private int id;
private String username;
private String password;
public UserName(){
}
public UserName(String username, String password){
this.username = username;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "UserName{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
课程实战驱动
本门课程的框架
小程序开发
框架:springboot+mybatis
技术储备要求
课程框架