我们知道springboot最优秀的一点就是,帮助我们配置很多信息,减少了我们去配置文件的时间,也就是springboot自动装配的强大所在。
页面跳转
很多使用方法和spring差不多,但是不需要我们去配置了
@Controller 表示这个类是跳转或者返回数据的类
@Autowired 自动注入属性
@RequestMapping({“”,“”})请求地址,可以设置多个地址,也可以设置请求方法
@ResponseBody 表示返回的是数据
例子
@RequestMapping("/log")//请求的地址
@ResponseBody
public String toindexixs(String name)//传入的数据
{
System.out.println(name);
return name;//返回的数据
}
@Configuration 表示当前类是配置类,保证当前类能够被扫描到。
使用方法
@Configuration
public class MyMvcConfig implements WebMvcConfigurer{
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/A").setViewName("index");
}
}
继承WebMvcConfigurer类
重写addViewControllers(ViewControllerRegistry registry)方法
可以设置,我们将要跳转的地址
使用mybatis
在springboot中使用mybatis同样简单
在springboot中我们使用mybatis不需要再写mybatis的一系列配置,还记得jdbctemplate吗,这些都不要。我们只需要
@Autowired
UserMapper userdao;
即可直接使用自己写的sql语句。
我们需要在接口类中添加注解
@Mapper
@Repository
保证接口能够被扫描,并且配置接口的.xml即可
当然我们还要在application.properties中配置以下属性
spring.datasource.username =
spring.datasource.password =
spring.datasource.url =
spring.datasource.driver-class-name =
mybatis.type-aliases-package=com.example.demo2.pojo//可选择配置
mybatis.mapper-locations=classpath:*.xml//表示接口的.xml的具体位置,如果接口的.xml和接口在同一个包下,可以不用配置,但是建议放在resource目录下
springboot的安全方案
@EnableWebSecurity 表示这是安全类
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
// 定制请求的授权规则
// 首页所有人可以访问
http.authorizeRequests().antMatchers("/").permitAll()
.antMatchers("/a").hasRole("vip1");
http.csrf().disable();
http.logout().logoutSuccessUrl("/");//注销后进入的页面
http.rememberMe().rememberMeParameter("remember");//记忆功能
http.formLogin()
.usernameParameter("username")//前端传来的数据,与前端name一致
.passwordParameter("password")//
.loginPage("/tologin")//表示你的登录页面
.loginProcessingUrl("/login"); // 登陆表单提交请求
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//在内存中定义,也可以在jdbc中去拿....
//给登录用户授权,让登录者可以访问网站
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("lin").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
}}
pom.xml的配置
添加了mybatis整合springboot的依赖和thymeleaf的依赖
总结:
总的来说,springboot的知识很多,我们可以通过官方文档学习新知识,在我们需要使用某些功能的时候,只需要查找对应的使用方法。