欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring boot

程序员文章站 2022-06-01 17:22:50
记录一下最近整理的spring boot项目 项目地址:https://gitee.com/xl0917/spring-boot 1.选择Spring Initializr 一直点击next,直到创建完成 2.创建spring boot子模块,创建无任何模板的maven项目 3.项目结构 4.pom ......

记录一下最近整理的spring boot项目

项目地址:

1.选择spring initializr   一直点击next,直到创建完成

 spring boot

2.创建spring boot子模块,创建无任何模板的maven项目

spring boot

3.项目结构

spring boot

  1. commone——公共类及工具类存放处,可在该项目中引入其他工具jar包
  2. entity——实体类层
  3. dao——数据持久层
  4. api——service接口,配置double及注册中心可供rpc消费端调用
  5. service——业务逻辑
  6. webapp——web项目url请求入口

4.pom.xml文件

  1. 父类pom文件
    <?xml version="1.0" encoding="utf-8"?>
    <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
             xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelversion>4.0.0</modelversion>
        <packaging>pom</packaging>
        <modules>
            <module>backstage-common</module>
            <module>backstage-dao</module>
            <module>backstage-entiy</module>
            <module>backstage-api</module>
            <module>backstage-service</module>
            <module>backstage-webapp</module>
        </modules>
    
        <parent>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-parent</artifactid>
            <version>2.1.3.release</version>
            <relativepath/> <!-- lookup parent from repository -->
        </parent>
    
    
        <groupid>com.xulong</groupid>
        <artifactid>com.xulong.backstage</artifactid>
        <version>0.0.1-snapshot</version>
        <name>backstage</name>
        <description>demo project for spring boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
    
    
    </project>
  2. common层pom.xml文件
    <?xml version="1.0" encoding="utf-8"?>
    <project xmlns="http://maven.apache.org/pom/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
             xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactid>com.xulong.backstage</artifactid>
            <groupid>com.xulong</groupid>
            <version>0.0.1-snapshot</version>
        </parent>
        <modelversion>4.0.0</modelversion>
    
        <artifactid>backstage-common</artifactid>
    
    
    </project>
  3. entity层pom文件
    <?xml version="1.0" encoding="utf-8"?>
    <project xmlns="http://maven.apache.org/pom/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
             xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactid>com.xulong.backstage</artifactid>
            <groupid>com.xulong</groupid>
            <version>0.0.1-snapshot</version>
        </parent>
        <modelversion>4.0.0</modelversion>
    
        <artifactid>backstage-entiy</artifactid>
    
        <dependencies>
            <dependency>
                <groupid>com.github.pagehelper</groupid>
                <artifactid>pagehelper</artifactid>
                <version>5.1.2</version>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupid>org.codehaus.mojo</groupid>
                    <artifactid>build-helper-maven-plugin</artifactid>
                    <version>3.0.0</version>
                </plugin>
            </plugins>
        </build>
    </project>
  4. dao层pom文件
    <?xml version="1.0" encoding="utf-8"?>
    <project xmlns="http://maven.apache.org/pom/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
             xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactid>com.xulong.backstage</artifactid>
            <groupid>com.xulong</groupid>
            <version>0.0.1-snapshot</version>
        </parent>
        <modelversion>4.0.0</modelversion>
    
        <artifactid>backstage-dao</artifactid>
    
        <dependencies>
            <dependency>
                <groupid>com.xulong</groupid>
                <artifactid>backstage-entiy</artifactid>
                <version>0.0.1-snapshot</version>
            </dependency>
            <dependency>
                <groupid>org.mybatis</groupid>
                <artifactid>mybatis-typehandlers-jsr310</artifactid>
                <version>1.0.2</version>
            </dependency>
            <dependency>
                <groupid>org.mybatis</groupid>
                <artifactid>mybatis</artifactid>
                <version>3.4.5</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupid>org.codehaus.mojo</groupid>
                    <artifactid>build-helper-maven-plugin</artifactid>
                    <version>3.0.0</version>
                </plugin>
            </plugins>
        </build>
    
    </project>
  5. api层pom文件
    <?xml version="1.0" encoding="utf-8"?>
    <project xmlns="http://maven.apache.org/pom/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
             xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactid>com.xulong.backstage</artifactid>
            <groupid>com.xulong</groupid>
            <version>0.0.1-snapshot</version>
        </parent>
        <modelversion>4.0.0</modelversion>
    
        <artifactid>backstage-api</artifactid>
    
        <dependencies>
            <dependency>
                <groupid>com.xulong</groupid>
                <artifactid>backstage-entiy</artifactid>
                <version>0.0.1-snapshot</version>
            </dependency>
            <dependency>
                <groupid>com.xulong</groupid>
                <artifactid>backstage-common</artifactid>
                <version>0.0.1-snapshot</version>
            </dependency>
            <dependency>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-starter</artifactid>
            </dependency>
    
            <dependency>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-starter-test</artifactid>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>
  6. service层pom文件
    <?xml version="1.0" encoding="utf-8"?>
    <project xmlns="http://maven.apache.org/pom/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
             xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactid>com.xulong.backstage</artifactid>
            <groupid>com.xulong</groupid>
            <version>0.0.1-snapshot</version>
        </parent>
        <modelversion>4.0.0</modelversion>
    
        <artifactid>backstage-service</artifactid>
    
        <dependencies>
            <dependency>
                <groupid>com.xulong</groupid>
                <artifactid>backstage-api</artifactid>
                <version>0.0.1-snapshot</version>
            </dependency>
            <dependency>
                <groupid>com.xulong</groupid>
                <artifactid>backstage-dao</artifactid>
                <version>0.0.1-snapshot</version>
            </dependency>
            <dependency>
                <groupid>com.github.pagehelper</groupid>
                <artifactid>pagehelper-spring-boot-starter</artifactid>
                <version>1.2.3</version>
                <exclusions>
                    <exclusion>
                        <groupid>org.springframework.boot</groupid>
                        <artifactid>spring-boot-starter-logging</artifactid>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <dependency>
                <groupid>org.mybatis.generator</groupid>
                <artifactid>mybatis-generator-core</artifactid>
                <version>1.3.6</version>
            </dependency>
            <dependency>
                <groupid>org.apache.shiro</groupid>
                <artifactid>shiro-spring</artifactid>
                <version>1.3.2</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupid>org.mybatis.generator</groupid>
                    <artifactid>mybatis-generator-maven-plugin</artifactid>
                    <version>1.3.6</version>
                    <dependencies>
                        <dependency>
                            <groupid>mysql</groupid>
                            <artifactid>mysql-connector-java</artifactid>
                            <version>5.1.44</version>
                        </dependency>
                    </dependencies>
                    <configuration>
                        <verbose>true</verbose>
                        <overwrite>true</overwrite>
                    </configuration>
                </plugin>
                <plugin>
                    <groupid>org.codehaus.mojo</groupid>
                    <artifactid>build-helper-maven-plugin</artifactid>
                    <version>3.0.0</version>
                </plugin>
            </plugins>
        </build>
    </project>
  7. webapp层pom文件
    <?xml version="1.0" encoding="utf-8"?>
    
    <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
             xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactid>com.xulong.backstage</artifactid>
            <groupid>com.xulong</groupid>
            <version>0.0.1-snapshot</version>
        </parent>
        <modelversion>4.0.0</modelversion>
    
        <groupid>com.xulong</groupid>
        <artifactid>backstage-webapp</artifactid>
        <version>0.0.1-snapshot</version>
        <packaging>war</packaging>
    
    
    
        <properties>
            <project.build.sourceencoding>utf-8</project.build.sourceencoding>
            <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupid>com.xulong</groupid>
                <artifactid>backstage-service</artifactid>
                <version>0.0.1-snapshot</version>
            </dependency>
            <dependency>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-starter-web</artifactid>
                <!--<exclusions>-->
                    <!--<exclusion>-->
                        <!--<groupid>org.springframework.boot</groupid>-->
                        <!--<artifactid>spring-boot-starter-tomcat</artifactid>-->
                    <!--</exclusion>-->
                <!--</exclusions>-->
            </dependency>
            <dependency>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-starter-test</artifactid>
                <exclusions>
                    <exclusion>
                        <groupid>org.springframework.boot</groupid>
                        <artifactid>spring-boot-test</artifactid>
                    </exclusion>
                </exclusions>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupid>com.jayway.jsonpath</groupid>
                <artifactid>json-path</artifactid>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupid>mysql</groupid>
                <artifactid>mysql-connector-java</artifactid>
                <version>5.1.44</version>
            </dependency>
            <dependency>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-test</artifactid>
                <version>1.5.6.release</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-starter-thymeleaf</artifactid>
            </dependency>
            <dependency>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-starter-tomcat</artifactid>
                <scope>provided</scope>
            </dependency>
    
            <!--<dependency>-->
                <!--<groupid>org.apache.tomcat</groupid>-->
                <!--<artifactid>tomcat-juli</artifactid>-->
                <!--<version>${tomcat.version}</version>-->
            <!--</dependency>-->
    
            <!--<dependency>-->
                <!--<groupid>org.apache.tomcat.embed</groupid>-->
                <!--<artifactid>tomcat-embed-jasper</artifactid>-->
            <!--</dependency>-->
        </dependencies>
    
        <build>
            <finalname>backstage-webapp</finalname>
            <pluginmanagement><!-- lock down plugins versions to avoid using maven defaults (may be moved to parent pom) -->
                <plugins>
                    <plugin>
                        <groupid>org.springframework.boot</groupid>
                        <artifactid>spring-boot-maven-plugin</artifactid>
                        <configuration>
                            <mainclass>com.xulong.backstage.backstageapplication</mainclass>
                            <executable>true</executable>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>repackage</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupid>org.apache.maven.plugins</groupid>
                        <artifactid>maven-war-plugin</artifactid>
                        <configuration>
                            <failonmissingwebxml>false</failonmissingwebxml>
                        </configuration>
                        <version>2.5</version>
                    </plugin>
                </plugins>
            </pluginmanagement>
        </build>
    </project>

     

5.配置文件application.properties

#数据库链接配置
spring.datasource.url=jdbc:mysql://**.**.***.***:3306/backstage?useunicode=true&characterencoding=utf-8&allowmultiqueries=true&zerodatetimebehavior=converttonull&usessl=false
spring.datasource.driverclassname=com.mysql.jdbc.driver
spring.datasource.username=root
spring.datasource.password=

server.port=8080


mybatis.mapper-locations=classpath:mybatis/mapper/*mapper*.xml
mybatis.configuration.log-prefix=com.xulong.backstage.dao


#静态资源
#表示所有的访问都经过静态资源路径
spring.mvc.static-path-pattern=/static/**
#在这里表示配置静态资源根路径
spring.resources.static-locations=classpath:/static/

#mvc跳转路径
thymeleaf.prefix=classpath:/templates/

#热部署生效
spring.devtools.restart.enabled=true
#静态文件热部署
spring.thymeleaf.cache=false

#显示sql语句
logging.level.com.xulong.backstage.dao.mapper=debug

#pagehelper分页插件配置
pagehelper.helper-dialect=mysql
pagehelper.offset-as-page-num=true
pagehelper.row-bounds-with-count=true
pagehelper.page-size-zero=false
pagehelper.reasonable=false
pagehelper.support-methods-arguments=true
pagehelper.params=pagenum=page;pagesize=size;count=countsql;

6.mybatis.generator插件配置

  1. generatorconfig.xml配置
    <?xml version="1.0" encoding="utf-8"?>
    <!doctype generatorconfiguration
        public "-//mybatis.org//dtd mybatis generator configuration 1.0//en"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
    
    <generatorconfiguration>
        <properties resource="meta-inf/mybatis/generator.properties"/>
        <!-- 指定数据连接驱动jar地址 -->
        <classpathentry location="d:\software\mvn_repo\mysql\mysql-connector-java\5.1.44\mysql-connector-java-5.1.44.jar" />
     <context id="mysqltables"
                 targetruntime="mybatis3">
    
            <property name="javafileencoding" value="utf-8"/>
            <property name="addgwtinterface" value="false"/>
            <property name="suppressjavainterface" value="false"/>
    
            <plugin type="org.mybatis.generator.plugins.serializableplugin"/>
            <!--<plugin type="org.meta-inf.mybatis.mapper.generator.plugins.guavastyletostringplugin"/>-->
    <!-- 去掉生成出来的代码的注解 -->
            <commentgenerator>
                <property name="suppressallcomments" value="true" />
                <property name="suppressdate" value="true" />
            </commentgenerator>
    
            <!-- 数据库连接url、用户名、密码 -->
            <jdbcconnection
                driverclass="${jdbc.driverclass}"
                connectionurl="${jdbc.url}"
                userid="${jdbc.username}"
                password="${jdbc.password}">
            </jdbcconnection>
    
            <javatyperesolver>
                <property name="forcebigdecimals" value="false" />
            </javatyperesolver>
    <!-- 生成模型的包名和位置 -->
            <javamodelgenerator
                targetproject="../${module.domain}/src/main/java"
                targetpackage="${package.domain}">
                <property name="enablesubpackages" value="true"/>
                <property name="trimstrings" value="true"/>
                <property name="constructorbased" value="false"/>
            </javamodelgenerator>
    
            <!-- 生成映射文件的包名和位置 -->
            <sqlmapgenerator
                targetproject="../${module.dao}/src/main/resources"
                targetpackage="mybatis/mapper">
                <property name="enablesubpackages" value="false"/>
            </sqlmapgenerator>
    
            <!-- 生成dao的包名和位置 -->
            <javaclientgenerator
                targetproject="../${module.dao}/src/main/java"
                targetpackage="${package.mapper}"
                type="xmlmapper">
                <property name="enablesubpackages" value="true"/>
            </javaclientgenerator>
    <table tablename="role_info" domainobjectname="roleinfo"
                   enableselectbyexample="false"
                   enablecountbyexample="false"
                   enabledeletebyexample="false"
                   enableupdatebyexample="false">
                <property name="useactualcolumnnames" value="false"/>
                <property name="selectallorderbyclause" value="role_code desc"/>
                <!--<generatedkey column="stock_code" sqlstatement="mysql" identity="true"/>-->
            </table>
    
        </context>
    </generatorconfiguration>
  2. 引入文件generator.properties配置
    jdbc.driverclass = com.mysql.jdbc.driver
    jdbc.url = jdbc:mysql://**.**.***.***:3306/backstage?useunicode=true&characterencoding=utf-8&allowmultiqueries=true&zerodatetimebehavior=converttonull&usessl=false
    jdbc.username = root
    jdbc.password = 
    
    module.domain = backstage-entiy
    module.dao = backstage-dao
    
    package.mapper = com.xulong.backstage.dao.mapper
    package.domain = com.xulong.backstage.entity
    driverpath = d:\repository4\mysql\mysql-connector-java\5.1.44\mysql-connector-java-5.1.44.jar
    
    jdk.use_jdk8_time = true

7.shiro框架配置

  1. java类方式配置
    package com.xulong.backstage;
    
    import org.apache.shiro.mgt.securitymanager;
    import org.apache.shiro.spring.lifecyclebeanpostprocessor;
    import org.apache.shiro.spring.security.interceptor.authorizationattributesourceadvisor;
    import org.apache.shiro.spring.web.shirofilterfactorybean;
    import org.apache.shiro.web.mgt.defaultwebsecuritymanager;
    import org.springframework.aop.framework.autoproxy.defaultadvisorautoproxycreator;
    import org.springframework.beans.factory.annotation.autowired;
    import org.springframework.context.annotation.bean;
    import org.springframework.context.annotation.configuration;
    import org.springframework.context.annotation.dependson;
    
    import java.util.linkedhashmap;
    
    
    /**
     * @author xulong
     * shiro 安全框架配置
     */
    
    @configuration
    public class shiroconfiguration {
    
        //==============shiro================
        /**
         * 配置securitymanager对象(shiro核心)
         * @param useruserrealm
         * @return
         */
        @bean("securitymanager")
        public defaultwebsecuritymanager newdefaultwebsecuritymanager(shirouserrealm useruserrealm){
            defaultwebsecuritymanager smanager = new defaultwebsecuritymanager();
            smanager.setrealm(useruserrealm);
            return smanager;
        }
    
        @bean("shirofilter")
        public shirofilterfactorybean newshirofilterfactory(
                @autowired securitymanager securitymanager){
            shirofilterfactorybean bean = new shirofilterfactorybean();
            bean.setsecuritymanager(securitymanager);
            //未登录跳转的url
            bean.setloginurl("/login.html");
            linkedhashmap<string,string> fcmap = new linkedhashmap<>();
            fcmap.put("/login.html", "anon");
            fcmap.put("/validatelogin.html","anon");
            fcmap.put("/test","anon");
    
    
            //退出url
            fcmap.put("/logout.html", "logout");
            //放开静态文件权限
            fcmap.put("/static/**","anon");
    
            fcmap.put("/**", "authc"); //必须授权才能访问  authc
            bean.setfilterchaindefinitionmap(fcmap);
            //登录成功跳转的url
            bean.setsuccessurl("/index.html");
            return bean;
        }
        /**
         * 配置shiro生命周期管理器
         * @return
         */
        @bean("lifecyclebeanpostprocessor")
        public lifecyclebeanpostprocessor newlifecyclebeanpostprocessor(){
            return new lifecyclebeanpostprocessor();
        }
    
        /**
         * 配置shiro注解应用
         * */
        @dependson(value="lifecyclebeanpostprocessor")
        @bean
        public defaultadvisorautoproxycreator newdefaultadvisorautoproxycreator(){
            return new defaultadvisorautoproxycreator();
        }
    
        @bean
        public authorizationattributesourceadvisor newauthorizationattributesourceadvisor(securitymanager securitymanager){
            authorizationattributesourceadvisor bean= new authorizationattributesourceadvisor();
            bean.setsecuritymanager(securitymanager);
            return bean;
        }
    
    
    
    }
  2. 登录验证类
    package com.xulong.backstage;
    
    import com.xulong.backstage.common.constants.dictcodeconstants;
    import com.xulong.backstage.dao.mapper.usermapper;
    import com.xulong.backstage.entity.user;
    import org.apache.shiro.authc.*;
    import org.apache.shiro.authc.credential.credentialsmatcher;
    import org.apache.shiro.authc.credential.hashedcredentialsmatcher;
    import org.apache.shiro.authz.authorizationinfo;
    import org.apache.shiro.realm.authorizingrealm;
    import org.apache.shiro.subject.principalcollection;
    import org.springframework.beans.factory.annotation.autowired;
    import org.springframework.stereotype.service;
    
    /**
     * @author xulong
     * shiro 验证登录等类
     */
    @service
    public class shirouserrealm extends authorizingrealm {
    
        @autowired
        private usermapper usermapper;
    
        @override
        protected authenticationinfo dogetauthenticationinfo(authenticationtoken token) throws authenticationexception {
            //1.获取客户端提交的用户信息
            usernamepasswordtoken uptoken = (usernamepasswordtoken)token;
            string username = uptoken.getusername();
            uptoken.getpassword();
            user user = usermapper.finduserbyusercode(username);
            if(user == null){
                throw new authenticationexception("用户名或密码不正确!");
            }
            if(dictcodeconstants.dict_100002_1.equals(user.getuserstatus())){
                throw new authenticationexception("账户已被冻结,请联系管理员!");
            }
            if(dictcodeconstants.dict_100002_2.equals(user.getuserstatus())){
                throw new authenticationexception("账户已被禁用,请联系管理员!");
            }
            simpleauthenticationinfo simple =
                    new simpleauthenticationinfo(
                            user,  //传入对象后,后续可以在shiro中获取登录用户的对象信息
                            user.getpassword(),
                            getname());
            return simple;//返回给认证管理器
        }
        /**
         * 此方法用于获取用户权限信息----执行权限操作
         */
        @override
        protected authorizationinfo dogetauthorizationinfo(principalcollection principal) {
    
            return null;
        }
    
        /**
         * 设置凭证匹配器
         */
        @override
        public void setcredentialsmatcher(credentialsmatcher credentialsmatcher) {
    //密码验证是否成功方法,比如密码错误有其它操作或者有自己的校验要求 customcredentialsmatcher cmatcher = new customcredentialsmatcher(); super.setcredentialsmatcher(cmatcher); } }
  3. 密码验证类
    package com.xulong.backstage;
    
    import org.apache.shiro.authc.authenticationexception;
    import org.apache.shiro.authc.authenticationinfo;
    import org.apache.shiro.authc.authenticationtoken;
    import org.apache.shiro.authc.usernamepasswordtoken;
    import org.apache.shiro.authc.credential.simplecredentialsmatcher;
    
    import java.util.objects;
    
    public class customcredentialsmatcher extends simplecredentialsmatcher {
    
        @override
        public boolean docredentialsmatch(authenticationtoken token, authenticationinfo info) {
            usernamepasswordtoken utoken=(usernamepasswordtoken) token;
            string dbpassword = (string) info.getcredentials();
            string inpassword = new string(utoken.getpassword());
            boolean fals = objects.equals(dbpassword, inpassword);
            if(fals == false){
                //更改当前用户登录错误次数,当错误次数大于3冻结当前用户
                throw new authenticationexception("用户名或密码不正确!");
            }
            return fals;
        }
    }

8.thymeleaf部分使用

  1. 例如:当数据库存储时间格式为20190401092416,在网页中想显示为2091-04-01 09:24:16,但是thtmeleaf未提供方法转换,需要我们写一个转换类
  2. dateutil类写一个格式转换方法
    package com.xulong.backstage.common.util;
    
    import java.text.simpledateformat;
    import java.util.date;
    
    /**
     * @author xulong
     * @date 2019/03/26
     */
    public class dateutil {
    
        /** 模式 :yyyymmddhhmmss */
        public static final string yyyymmdd_hhmmss = "yyyymmddhhmmss";
        /** 模式 :yyyymmdd */
        public static final string yyyymmdd = "yyyymmdd";
        /** 模式 :yyyymm */
        public static final string yyyymm = "yyyymm";
        /** 模式 :hhmmss */
        public static final string hhmmss = "hhmmss";
        /** 模式 :hhmm */
        public static final string hhmm = "hhmm";
    
        public static final string format_ymdhms = "yyyy-mm-dd hh:mm:ss";
    
        public static final string date_format_day = "yyyy-mm-dd";
    
        /**
         * 获取当前日期的数字格式
         * 如20190326
         * @return
         */
        public static long getnowdate(){
            date date = new date();
            return long.valueof(date2string(date,yyyymmdd));
        }
    
        /**
         * 获取当前时间的数字格式
         * 如20190326141225
         * @return
         */
        public static long getnowdatetime(){
            date date = new date();
            return long.valueof(date2string(date,yyyymmdd_hhmmss));
        }
    
    
        /**
         * longformat:(long类型的日期转化为格式化日期字符串,如20160717122121转化为2016-07-17 12:21:21)
         * @param srclong long类型的日期如20160717122121
         * @param pattern 格式化日期字符串如2016-07-17 12:21:21
         * @return
         */
        public static string longformat(long srclong,string pattern){
            if (srclong == null) {
                return "";
            }
            if (srclong == 0) {
                return "0";
            }
            date date = string2date(string.valueof(srclong));
            return date2string(date,pattern);
        }
    
        /**
         * intformat:(long类型的日期转化为格式化日期字符串,如20160717转化为2016-07-17)
         *
         * @param src     integer 类型的日期如20160717122121
         * @param pattern 格式化日期字符串如2016-07-17 12:21:21
         * @return
         * @throws
         * @since dateutils.java ver 1.0
         */
        public static string intformat(integer src, string pattern) {
            if (src == null) {
                return "";
            }
            if (src == 0) {
                return "0";
            }
            date date = string2date(string.valueof(src));
            return date2string(date, pattern);
        }
    
        /**
         * 方法说明:日期类型按照指定格式转成字符串.
         *
         * @param date
         * @param pattern
         * @return
         */
        public static string date2string(date date, string pattern) {
            if (null == date) {
                date = new date();
            }
            if (pattern.length() == 0 || pattern == null) {
                pattern = format_ymdhms;
            }
            try {
                return getdateformat(pattern).format(date);
            } catch (exception e) {
                throw new runtimeexception("日期转换异常");
            }
        }
    
        /**
         * 方法说明:字符串转日期类型.
         *
         * @param date
         * @return
         */
        public static date string2date(string date) {
            try {
                if (date.length() == 8) {
                    return getdateformat(yyyymmdd).parse(date);
                } else if (date.length() == 14) {
                    return getdateformat(yyyymmdd_hhmmss).parse(date);
                } else if (date.length() == 6) {
                    return getdateformat(hhmmss).parse(date);
                } else {
                    return null;
                }
            } catch (exception e) {
                throw new runtimeexception("日期转换异常");
            }
        }
    
    
        /**
         * 获取指定日期指定格式的日期字符串
         * @return
         */
        private static string getdate(string pattern,date date){
            simpledateformat format = getdateformat(pattern);
            return format.format(date);
        }
    
    
        /**
         * 获取指定模式pattern的simpledateformat对象
         * @param str
         * @return
         */
        private static simpledateformat getdateformat(string str){
           return new simpledateformat(str);
        }
    
    
    
    }
  3. 在控制层封装dateutil对象
     @requestmapping("/index.html")
        public string index(model model){
            model.addattribute("dateutil",new dateutil());
            long date = 20160824121425l;
            model.addattribute("date",date);
            return "index";
        }
  4. html页面中书写方式
    <div class="row" style="text-align:center;font-size: 60px;color: #00aced;margin-top:10%">
                      欢迎来到后台管理页面<br>
                      <span th:text="${dateutil.longformat(da,'yyyy-mm-dd hh:mm:ss')}"></span>
                  </div>
  5. 页面显示的内容

    spring boot

上一篇: 安装confluence (转)

下一篇: qtchooser