集成测试-springboot

框架集成完毕后,需要增加代码程序进行简单的测试。

在src/main/java/com/atguigu/crowdfunding/controller目录中增加UserController

package com.atguigu.atcrowdfunding.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/index")
    public String index() {
        return "user/index";
    }
    
    @ResponseBody
    @RequestMapping("/json")
    public Object json() {
        Map map = new HashMap();
        map.put("username", "张三");
        return map;
    }
}
将web项目发布到服务器中,启动服务器,浏览器中分别输入路径http://127.0.0.1:8080/应用路径名称/user/index,http://127.0.0.1:8080/应用路径名称/user/json进行测试。
如果访问成功,说明项目中SpringMVC框架集成成功。
在src/main/java/com/atguigu/crowdfunding/service目录中增加UserService接口。
package com.atguigu.atcrowdfunding.service;

public interface UserService {

}
在src/main/java/com/atguigu/crowdfunding/service/impl目录中增加UserServiceImpl实现类。
package com.atguigu.atcrowdfunding.service.impl;

import com.atguigu.atcrowdfunding.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
}
在src/main/java/com/atguigu/crowdfunding/dao目录中增加UserDao接口。
package com.atguigu.atcrowdfunding.dao;

public interface UserDao {

}
修改UserController类,增加对UserService接口的引用。
package com.atguigu.atcrowdfunding.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    
    @RequestMapping("/index")
    public String index() {
        return "user/index";
    }
    
    @ResponseBody
    @RequestMapping("/json")
    public Object json() {
        Map map = new HashMap();
        map.put("username", "张三");
        return map;
    }
}
重启服务器,重新通过浏览器中访问路径http://127.0.0.1:8080/应用路径名称/user/index进行测试。
如果访问成功,说明项目中Spring框架(IOC功能)集成成功。

在src/main/java/com/atguigu/corwdfunding/bean目录中增加User实体类。

package com.atguigu.atcrowdfunding.bean;

public class User {

}
在UserService接口中增加方法声明queryById,并在UserServiceImpl类中默认实现
package com.atguigu.atcrowdfunding.service;

import com.atguigu.atcrowdfunding.bean.User;
public interface UserService {
    public User queryById();
}
package com.atguigu.atcrowdfunding.service.impl;

import com.atguigu.atcrowdfunding.bean.User;
import com.atguigu.atcrowdfunding.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    
    public User queryById() {
        return userDao.queryById();
    }
}
如果访问成功,说明项目中Spring框架(AOP功能)集成成功。
在UserDao中增加查询语句,实现数据库查询
package com.atguigu.atcrowdfunding.dao;

import com.atguigu.atcrowdfunding.bean.User;
import org.apache.ibatis.annotations.Select;

public interface UserDao {
    
	@Select("select * from t_user where id = 1")
	public User queryById();
}
如果访问成功,说明项目中Mybatis框架集成成功。



猜你喜欢

转载自blog.csdn.net/qq_40408317/article/details/80528650