Spring Boot + Shiro + JPA 项目整理

  1. 配置文件 application.properties(页面等资源放在templates中)
    #mysql configure
    spring.datasource.url = jdbc:mysql://localhost:3306/popularfeelings?serverTimezone=GMT&characterEncoding=utf-8
    spring.datasource.username = root
    spring.datasource.password = root
    spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
    spring.jpa.database = MYSQL
    #stripped before adding them to the entity manager
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
    
    # Show or not log for each sql query
    
    spring.jpa.show-sql = true
    
    # Hibernate ddl auto (create, create-drop, update)
    spring.jpa.hibernate.ddl-auto = update
    # Naming strategy
    spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
    
    #sqlserver configure
    spring.datasource.url = jdbc:sqlserver://localhost:1433;DatabaseName=test1
    spring.datasource.username = sa
    spring.datasource.password = sa
    spring.datasource.driverClassName = com.microsoft.sqlserver.jdbc.SQLServerDriver
    spring.jpa.database = SQLSERVER
    spring.jpa.properties.hibernate.default_schema = dbo
    
    #test1代表数据库名称
    spring.jpa.properties.hibernate.default_catalog= test1
    
    #设置hibernate方言
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.SQLServer2008Dialect
    
    # Show or not log for each sql query
    spring.jpa.show-sql = true
    # Hibernate ddl auto (create, create-drop, update)
    spring.jpa.hibernate.ddl-auto = update
    # Naming strategy
    spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
    
    spring.thymeleaf.prefix=classpath:/templates/page/
    spring.thymeleaf.suffix: .html
    spring.mvc.static-path-pattern=/templates/**
    spring.resources.static-locations=classpath:/templates

     

  2. Shiro配置相关(http://412887952-qq-com.iteye.com/blog/2299777)
    1.shiro config 中的shiroFilterFactoryBean.setLoginUrl("/index"); 配置的是访问链接。拦截后访问此url
    2.页面跳转 http://www.cnblogs.com/zmj1987/p/9262014.html 踩坑:只能使用注解controller 否则返回的是字符串
    3.设置css等文件不拦截  filterChainDefinitionMap.put("/templates/css/**", "anon");//设置不拦截
    4.获取静态资源路径 需设置 spring.mvc.static-path-pattern=/templates/**  
    5.shiro获取当前登录信息
    SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(userInfo,Md5pass,getName()); 设置
    TSUser user = (TSUser) SecurityUtils.getSubject().getPrincipal();  获取
    spring.resources.static-locations=classpath:/templates 
    注意:通过spring.mvc.static-path-pattern这种方式配置,会使Spring Boot的默认配置失效,也就是说,/public /resources 等默认配置不能使用。
    配置中配置了静态模式为/static/,就只能通过/static/来访问。

     

  3. spring boot 传递参数的方式

    1、@PathVariable
    获取路径参数。即url/{id}这种形式。
    2、@RequestParam
    获取查询参数。即url?name=这种形式
    public void demo(@PathVariable(name = "id") String id, @RequestParam(name = "name") String name) {
        System.out.println("id="+id);
        System.out.println("name="+name);
    }
    3.$.ajax({
                url: "/userLogin?username="+username+"&password="+password,
    后台 
     @RequestMapping(value = "/userLogin",method = RequestMethod.POST)
        public String login(String username,String password){
    4.后台向页面返回值  Exception processing template:
    5. data : JSON.stringify(vm.noticeInfo)
      public R update(@RequestBody NoticeInfoEntity noticeInfo) 
       可以直接使用一个map来接收。
    public R update(@RequestBody Map params)
    
  4. spring boot 热部署

    只需要添加devtools依赖即可
    
    		
    		
    			org.springframework.boot
    			spring-boot-devtools
    			true
    			true
    		
    JVM会定时扫描重新加载且仅加载有改动的类文件(.class文件),而不是加载所有的类文件,大大减少了类加载耗费的时间,从而实现热部署的功能。
    Eclipse默认自动编译,而idea默认手动编译,因此idea需要修改两个参数以达到任意时间自动编译的目的。
    
    首先,File->setting->搜索compiler->将Build project automatically打勾(注意后面only works not running/debugging,所以我们要实现热部署就必须打破这个限制,于是乎有了下面设置)
    
    然后,CTRL+ALT+SHIFT+/ 组合键打开 Maintenance窗口选择Registry... ,找到compiler.automake.allow.when.app.running选项,打勾开启运行时编译功能

     

  5. SpringBoot项目打war包

    1.pom.xml中的packaging设置成warwar
    
    /**
     * 修改启动类,继承 SpringBootServletInitializer 并重写 configure 方法
     */
    public class SpringBootStartApplication extends SpringBootServletInitializer {
    
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
            // 注意这里要指向原先用main方法执行的Application启动类
            return builder.sources(Application.class);
        }
    }
    
    【注意】:这个新建的类,与springboot的启动类是同级的
    2. 
            
                org.springframework.boot
                spring-boot-starter-tomcat
                provided
            
    3.
        javax.servlet
        javax.servlet-api
        3.1.0
        provided
    
    https://blog.csdn.net/Shen_S_H/article/details/79178558

     

  6. 设置tomcat不加项目名称访问 \conf\server.xml中

    可能会出现 一个tomcat中有两个项目 你的项目和root
    此时需要配置 springboot中的application.propertise中spring.jmx.enabled=false

  7. 遇到的问题

    1.No default constructor for entity
    【解决方法】
    当实体类声明了其他带参构造方法时,需要显式声明不带参构造方法。
    2.SqlServer user附近有语法错误 
    【解决方法】
    @Table(name = "[user]") user 是关键词 尽量不要用作表名,若必须使用可以使用[]解决。
    3.html页面之间跳转 404错误
    【解决方法】
    window.location.href = "signin";
    这个代码的意思是跳转到signin这个请求,SpringBoot会根据Controller中的RequestMapping找到"/signin"请求,然后执行里面的内容
    4.JPA中save方法更新时,如果试题中有的字段未null时,更新原有数据为null。
    【解决方法】
    https://www.cnblogs.com/kongxianghai/p/7575988.html  jpa自定义sql  
    更新
    @Transactional
    @Query(value=“UPDATE xxEntity xe SET xe.data= :date WHERE xe.id= :id”)
    int  update(@Param(“date”)Date date,@Param(“id”)int id);
    5.查询
    @Query("select id,ywnr,gdlsh from Bill where csr_cssj is null and  csr_id =:csr_id")
    public List findByCsrId(@Param("csr_id") String id);

     

你可能感兴趣的:(Spring Boot + Shiro + JPA 项目整理)