(格式:th: href/ img / src="@{xxx}**")
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8"> <meta content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta content="Gurdeep singh osahan"> <meta content="Gurdeep singh osahan"> <title>Miver</title> <!-- Favicon Icon --> <link type="image/png" th:href="@{images/fav.svg}"> <!-- Bootstrap core CSS --> <link th:href="@{vendor/bootstrap/css/bootstrap.min.css}"> <!-- Font Awesome--> <link th:href="@{vendor/fontawesome/css/font-awesome.min.css}"> <!-- Material Design Icons --> <link th:href="@{vendor/icons/css/materialdesignicons.min.css}" media="all" type="text/css"> <!-- Slick --> <link th:href="@{vendor/slick-master/slick/slick.css}" type="text/css"> <!-- Lightgallery --> <link th:href="@{vendor/lightgallery-master/dist/css/lightgallery.min.css}"> <!-- Select2 CSS --> <link th:href="@{vendor/select2/css/select2-bootstrap.css}" /> <link th:href="@{vendor/select2/css/select2.min.css}"> <!-- Custom styles for this template --> <link th:href="@{css/style.css}"> </head> <body> <div> <div> <a href="http://www.likecs.com/index.html"> <img th:src="@{images/logo.svg}"> </a> </div> </div> <script th:src="@{vendor/jquery/jquery.min.js}"></script> <script th:src="@{vendor/bootstrap/js/bootstrap.bundle.min.js}"></script> <!-- Contact form JavaScript --> <!-- Do not edit these files! In order to set the email address and subject line for the contact form go to the bin/contact_me.php file. --> <script th:src="@{js/jqBootstrapValidation.js}"></script> <script th:src="@{js/contact_me.js"></script> <!-- Slick --> <script th:src="@{vendor/slick-master/slick/slick.js}" type="text/javascript" charset="utf-8"> </script> <!-- lightgallery --> <script th:src="@{vendor/lightgallery-master/dist/js/lightgallery-all.min.js}"></script> <!-- select2 Js --> <script th:src="@{vendor/select2/js/select2.min.js}"></script> <!-- Custom --> <script th:src="@{js/custom.js}"></script> </body> </html> 八、整合Druid(阿里巴巴的数据库连接池) 8.1、更改yml中数据库连接type为Druid spring: datasource: username: root password: root url: jdbc:mysql://localhost:3306/curry?usrUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource thymeleaf: cache: false #关闭缓存 mode: HTML5 #设置模板类型 encoding: utf-8 #设置编码 # 打印自动生成的sql语句 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl 8.2、实现Druid绑定到yml中并实现一个后台监控功能
package com.example.springboot_data.config; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; import java.util.HashMap; @Configuration public class DruidConfig { //绑定到yml中 @ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource druidDataSource(){ return new DruidDataSource(); } @Bean //后台监控功能 :spring可以在增加web功能下的web.xml中配置 但是springboot就通过ServletRegistrationBean配置 public ServletRegistrationBean statViewServlet(){ ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*"); //后台登录 账户密码配置 HashMap<String,String> initParameters = new HashMap<>(); //账户密码设置 initParameters.put("loginUsername","admin"); initParameters.put("loginPassword","admin"); // 禁止谁访问! initParameters.put("ahui","192.168.11.12"); bean.setInitParameters(initParameters); System.out.println(bean.getInitParameters()+"this is getInit"); return bean; } } 8.3、实现filter过滤器 import org.springframework.context.annotation.Configuration; import javax.servlet.Filter; import javax.sql.DataSource; import java.util.HashMap; @Configuration public class DruidConfig { @Bean public FilterRegistrationBean webStatFilter(){ FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>(); bean.setFilter(new WebStatFilter()); // 过滤请求 HashMap<String, String> initParameters = new HashMap<>(); //过滤这些东西 initParameters.put("exclusions","*.js,*.css,/druid/*"); bean.setInitParameters(initParameters); return bean; } } 九、springboot整合mybatis 9.1、UserController实现跳转 package com.example.springboot_mybatis.controller; import com.example.springboot_mybatis.mapper.UserMapper; import com.example.springboot_mybatis.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class UserController { @Autowired(required = false) private UserMapper userMapper; @GetMapping("/queryUserList") public List<User> queryUserList(){ List<User> users = userMapper.queryUserList(); users.forEach(System.out::println); return users; } // 使用restful风格取值id 并查询 @GetMapping("/queryById/{id}") public User queryById( @PathVariable Integer id){ return userMapper.queryById(id); } } 9.2、UserMapper接口实现查询功能 package com.example.springboot_mybatis.mapper; import com.example.springboot_mybatis.pojo.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; import java.util.List; @Mapper // @Mapper表示这是一个mybatis的mapper类 // 也可以在主程序入口使用@MapperScan("com.example.springboot_mybatis.mapper")) @Repository //@Component public interface UserMapper { @Select("select * from user") List<User> queryUserList(); @Select("select * from user where id = #{id}") User queryById(@Param("id") int id); int addUser(@Param("User")User user); int updateUser(@Param("User")User user); int deleteUserById(@Param("id")int id); } 9.3、User实体类 package com.example.springboot_mybatis.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class User { private Integer id; private String name; private Integer age; private String email; private Integer version; private String gmt_create; private String gmt_modified; } 9.4、yml配置连接数据库及绑定mybatis spring: datasource: type: org.springframework.jdbc.datasource.DriverManagerDataSource username: root password: root url: jdbc:mysql://localhost:3306/curry?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.cj.jdbc.Driver #整合mybatis mybatis: type-aliases-package: com.example.springboot_mybaits.pojo # mapper-locations: classpath:mybatis/mapper/*.xml 用注解就直接省去xml配置 十、SpringSecurity安全机制安全框架:shiro、springsecurity
安全框架的作用:认证、授权
功能权限
访问权限
菜单权限
重要security类:
webSecurityConfiguration : 自定义 security 策略
AuthenticationManagerBuilder : 自定义认真策略
@EnableWebSecurity : 开启 WebSecurity 模式