(五)写一个测试页

写要给简单的测试页,可以进入到主页,主要想串一下MVC。
1.在resources/templates/下新建一个index.html




    
    management


    Hello world!


2.在src下新建controller包和service包


image.png

3.在service包下新建一个service接口类以及对应的实现类,并将一个mapper注入到service实现类(不知道ideal创建实现类的时候是否有跟eclipse一样可选的接口,idea创建类的时候貌似没看到有让选择接口的地方)

package com.lee.management.service;

public interface IndexService {

}

package com.lee.management.service.impl;

import com.lee.management.mapper.SysUserMapper;
import com.lee.management.service.IndexService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
@Service
class IndexServiceImpl implements IndexService {

    @Resource
    private SysUserMapper sysUserMapper;


}

4.给所有mapper接口添加注解(这里学会一个idea关闭单个文件的快捷键Ctrl+f4)

import org.springframework.stereotype.Repository;

@Repository
public interface SysMenuMapper {

5.controller及service代码编写

package com.lee.management.controller;

import com.lee.management.service.IndexService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

@Controller
public class IndexController {

    @Resource
    private IndexService indexService;

    @RequestMapping("/index")
    public String indexPage(){
        System.out.println(indexService.getSysUser().getName());
        return "index";
    }

}
package com.lee.management.service;

import com.lee.management.entity.SysUser;

public interface IndexService {
    public SysUser getSysUser();
}
package com.lee.management.service.impl;

import com.lee.management.entity.SysUser;
import com.lee.management.mapper.SysUserMapper;
import com.lee.management.service.IndexService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
@Service
class IndexServiceImpl implements IndexService {

   @Resource
   private SysUserMapper sysUserMapper;

   @Override
   public SysUser getSysUser() {
       return sysUserMapper.selectByPrimaryKey(1L);
   }
}

6.启动项目,查看运行结果


启动报错了

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'indexServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sysUserMapper' defined in file [D:\IntelliJIDEAWorkSpace\management\target\classes\com\lee\management\mapper\SysUserMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration': Unexpected exception during bean creation; nested exception is java.lang.TypeNotPresentException: Type org.apache.ibatis.scripting.LanguageDriver not present

百度说jar冲突,通过maven面板的show dependencies查看依赖关系:



然后发现了一个冲突:



然后把pom.xml里的mybatis版本修改如下:

            org.mybatis
            mybatis
            3.5.5
        

然后再启动就正常了。

2021-11-25 14:49:14.759  INFO 9524 --- [           main] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page template: index
2021-11-25 14:49:14.886  INFO 9524 --- [           main] .s.s.UserDetailsServiceAutoConfiguration : 

Using generated security password: 787f64a6-1fad-4b14-b696-5958faabe1bb

2021-11-25 14:49:14.967  INFO 9524 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Will secure any request with [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@1702830d, org.springframework.security.web.context.SecurityContextPersistenceFilter@64469d8, org.springframework.security.web.header.HeaderWriterFilter@12c60152, org.springframework.security.web.csrf.CsrfFilter@19382338, org.springframework.security.web.authentication.logout.LogoutFilter@2e51d054, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@5c7668ba, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@474c9131, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter@6a937336, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@7dd611c8, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@4c18621b, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@43e1692f, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@278667fd, org.springframework.security.web.session.SessionManagementFilter@6cd164a6, org.springframework.security.web.access.ExceptionTranslationFilter@282ffbf5, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@6ec7bce0]
2021-11-25 14:49:15.048  INFO 9524 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-11-25 14:49:15.063  INFO 9524 --- [           main] c.lee.management.ManagementApplication   : Started ManagementApplication in 1.938 seconds (JVM running for 2.626)

访问首页出来登录(我觉得是spring security在起作用,下期研究一下spring security)


image.png

如果把pom.xml中带security的依赖先注释掉,再启动项目就达到预想的目的了。



你可能感兴趣的:((五)写一个测试页)