变为负进度了,还是要用baseservlet,我就又重新写了一部分,看了好几遍视频,突然就想明白了,感觉每次要上课,就时间不连续思路总是断,今天晚自习算是搞懂了怎么写了,就是代码有点多,写的有点慢一开始那个构架也挺难理解的(对我来说)
这是我cotroller类的代码,注册部分:
@WebServlet("/register") // 定义Servlet的URL映射
@MultipartConfig
public class RegisterController1 extends BaseServlet {
SomeMethod someMethod=new SomeMethod();
@ReturnType(ResponseType.JSON)
@POST(value = "AddUser")
public String AddUser(@FormData("username") String username, @FormData("password") String password, @FormData("email") String email) throws IOException, NoSuchAlgorithmException {
RegisterMethod registerMethod=new RegisterMethod();
return registerMethod.AddUser(username,password,email);
这是baseservlet部分:
@MultipartConfig
public class BaseServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getQueryString();
req.setCharacterEncoding("UTF-8");
//用字符串记录发过来的请求(包括很多很多)
String uri=req.getRequestURI();
String methodName = new String(uri.substring(uri.lastIndexOf("/") + 1)).toString(); // 从 URI 中提取方法名
System.out.println("获得的方法:"+methodName);
Method method=null;
//通过反射获取被继承的类里面的所有方法
Method[] methods=this.getClass().getDeclaredMethods();
//获取请求头,方便及进行接下来的操作
String contentType =req.getContentType();
ObjectMapper objectMapper=new ObjectMapper();
JsonNode jsonNode = null;
if (contentType != null && contentType.startsWith("multipart/form-data")){
System.out.println("如果是文件就不进行改变");
}else {
//处理JSON 数据部分
jsonNode = objectMapper.readTree(req.getReader());
}
for (Method method1 : methods){
if("POST".equals(req.getMethod())){
System.out.println("进入了POST请求"+"获取到的方法名为:"+methodName);
if(method1.getAnnotation(POST.class)==null) {
continue;
}
if(method1.getAnnotation(POST.class).value().equals(methodName)){
System.out.println("进入了具体的方法里面");
method = method1;
Parameter[] parameters = method.getParameters();
System.out.println("参数列表为:"+ parameters.length)
Object[] args = new Object[parameters.length];
for(int i=0;i<parameters.length; i++){
Annotation[] annotations = parameters[i].getAnnotations();
if(annotations == null || annotations.length == 0) {
if (parameters[i].getType() == HttpServletRequest.class) {
args[i] = req;
} else if (parameters[i].getType() == HttpServletResponse.class) {
args[i] = resp;
}
}
else {
for(Annotation annotation:annotations){
if(annotation instanceof Param){
String name=((Param)annotation).value();
args[i]= request.getParameter(name);
System.out.println(args[i]);
方法类:
public class SomeMethod {
public User addUser(String UserName, String Email, String Password) throws IOException {
//新增账号
User user = new User();
user.setUserName(UserName);
user.setEmail(Email);
user.setPassword(Password);
//1.获取SqlSessionFactory
//1.加载mybatis的核心配置文件,获取SqlSessionFaxtory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession 对象,用它来执行sql
SqlSession sqlSession = sqlSessionFactory.openSession();//在这里面其实是可以写true或者false的没写为f表示要手动提交
//3.1获取UserMapper接口的代理对象
UserMapper userMapper = sqlSession.getMapper((UserMapper.class));
userMapper.add(user);
//提交事务
sqlSession.commit();
//5.释放资源
sqlSession.close();
return user;
}