发表于: 2019-11-02 20:00:19

1 635


一、今天完成的事
把springboot的demo和之前的项目整合为demo
先写配置文件
server.port=8080
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=admin123
spring.datasource.dbcp2.max-conn-lifetime-millis=20
spring.datasource.dbcp2.min-idle=5
spring.datasource.dbcp2.max-wait-millis=180
加入之前写过的pojo类和dao
在springboot中,mybatis可以用注解方式或配置文件方式导入
@Mapper
public interface StudentMapper {
/**
*显示优秀学员
* @return
*/
@Select("select * from student where excellent=1")
List<Student> selectExcellentStudent();
/**
* 计算找到工作的人数
* @return
*/
@Select("select count(working) from student WHERE working=1")
Integer countByWorking();
/**
* 计算未找到工作的人数
* @return
*/
@Select("select count(working) from student where working=0")
Integer countByLearning();
}
@Mapper注解是指定这是一个mapper类
使用配置文件的方式就需要在application.properties中加入扫描
mybatis.mapper-locations=classpath:com/ksy/springboot/mapper/*.xml
mybatis.type-aliases-package=com.ksy.springboot.pojo
controller中就没有很大的变化,直接使用之前写的就可以了
@Controller
public class StudentController {
private static final Logger log= LogManager.getLogger(StudentController.class);
@Autowired
StudentMapper studentMapper;
/**
* 查询优秀学员,计算在学和入职人数
* @param model
* @return backDesk
*/
@RequestMapping("/home")
public String selectExcellentStudent(Model model) throws Exception {
log.info(studentMapper);
List<Student> studentList = studentMapper.selectExcellentStudent();
Integer count=studentMapper.countByLearning();
log.info(count);
Integer number=studentMapper.countByWorking();
model.addAttribute("studentList",studentList);
model.addAttribute("count",count);
log.info(count);
model.addAttribute("number",number);
log.info(number);
return "backDesk";
}
}
@RequestMapping 也可以写为@GetMapping,@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。
新增加一个类GlobalExceptionHandler,用于捕捉Exception异常以及其子类。
捕捉到之后,把异常信息,发出异常的地址放进ModelAndView里,然后跳转到 error.jsp
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
ModelAndView mav = new ModelAndView();
mav.addObject("exception", e);
mav.addObject("url", req.getRequestURL());
mav.setViewName("error");
return mav;
}
}
<div style="width:500px;border:1px solid lightgray;margin:200px auto;padding:80px">
系统 出现了异常,异常原因是:
${exception}
<br><br>
出现异常的地址是:
${url}
</div>
然后错误的话会出现这个画面
运行application.properties
二、遇到的问题
1.pom包一定要导全,springboot是不建议使用jsp的,所以jsp的包一定要导全
 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
             
        </dependency>
        <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>3.8.1</version>
              <scope>test</scope>
        </dependency>
        <!-- servlet依赖. -->
        <dependency>
              <groupId>javax.servlet</groupId>
              <artifactId>javax.servlet-api</artifactId>
               
        </dependency>
              <dependency>
                     <groupId>javax.servlet</groupId>
                     <artifactId>jstl</artifactId>
              </dependency>
        <!-- tomcat的支持.-->
        <dependency>
               <groupId>org.apache.tomcat.embed</groupId>
               <artifactId>tomcat-embed-jasper</artifactId>
                
        </dependency>    
    </dependencies>


上面几个包是一定要的
2.配置文件中
spring.datasource.username=root
不能写为
spring.datasource.name=root
spring.datasource.data-username=root
会连接不上数据库
3.项目中
主类一定是和其他所有文件夹平级的,主类会自动扫描

三、收获
四、明天的计划
继续学习sringboot



返回列表 返回列表
评论

    分享到