最简单的服务器
-
Controller:接收web请求
//示例
@Controller
public class IndexController {
@RequestMapping("/")
@ResponseBody //用于显示文本
public String index() {
return "Hello NowCoder";
}
@RequestMapping(value = "/profile/{groupId}/{userId}")
@ResponseBody
public String profile(@PathVariable("groupId") String groupId,
@PathVariable("userId") int userId,
@RequestParam(value = "type", defaultValue = "1") int type,
@RequestParam(value = "key", defaultValue = "nowcoder") String key) {
return String.format("{%s},{%d},{%d},{%s}", groupId, userId, type, key);
}
}
-
Service:各种函数,通过调取DAO层中的方法,与本地服务器中的数据进行交互
-
DAO层:提供数据支持,使方法变量与数据库中的数据进行交互
package com.example.wenda_new1.dao;
import com.example.wenda_new1.model.User;
import org.apache.ibatis.annotations.*;
//示例
@Mapper
public interface UserDAO {
String TABLE_NAME = "user";
String INSERT_FIELDS = " name, password, salt, head_url";
String SELECT_FIELDS ="id, "+INSERT_FIELDS;
@Insert({"insert into ",TABLE_NAME,"(",INSERT_FIELDS,
") values(#{name},#{password},#{salt},#{headUrl})"})
int addUser(User user);
@Select({"select ",SELECT_FIELDS, " from",TABLE_NAME," where id=#{id}"})
User selectById(int id);
@Select({"select ",SELECT_FIELDS, " from",TABLE_NAME," where name=#{name}"})
User selectByName(String name);
@Update({"update ",TABLE_NAME," set password=#{password} where id=#{id}"})
void updatePassword(User user);
@Delete({"delete from ",TABLE_NAME," where id=#{id}"})
void deleteById(int id);
}
Request&response(HttpServletResponse&HttpServletRequest)
request | 参数解析 cookie读取 |
response | 页面内容返回 cookie下发(response.addCookie(new Cookie(key, value));) |
重定向
301:永久转移
302:临时转移