springboot+mysql+layui++freemarker+ftl 建项目

目标:(1)登录界面springboot+mysql+layui++freemarker+ftl 建项目_第1张图片
(2)学生信息管理界面
springboot+mysql+layui++freemarker+ftl 建项目_第2张图片

工作:1、数据库
(1)系统用户表
springboot+mysql+layui++freemarker+ftl 建项目_第3张图片
(2)学生信息表
springboot+mysql+layui++freemarker+ftl 建项目_第4张图片
2、项目结构springboot+mysql+layui++freemarker+ftl 建项目_第5张图片
3、这个FreeMarkerConfig 类必须写,不然**.ftl**的页面显示不出来。
在这里插入图片描述
代码如下:

package com.student.config;

import freemarker.template.TemplateException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import java.io.IOException;
import java.util.Properties;

/**
 * @Author LSS
 * @Description: TODO
 * @Date in 2019/4/24 0024 17:38
 * @Modify By:
 */
@Configuration
public class FreeMarkerConfig {

    @Bean
    public ViewResolver viewResolverFtl() {
        FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
        resolver.setCache(false);
        resolver.setViewClass(org.springframework.web.servlet.view.freemarker.FreeMarkerView.class);
        resolver.setRequestContextAttribute("re");
        resolver.setExposeRequestAttributes(true);
        resolver.setExposeSessionAttributes(true);
        resolver.setSuffix(".ftl");
        resolver.setContentType("text/html;charset=UTF-8");
        resolver.setOrder(0);
        return resolver;
    }

    @Bean
    public ViewResolver viewResolverHtml() {
        FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
        resolver.setCache(false);
        resolver.setViewClass(org.springframework.web.servlet.view.freemarker.FreeMarkerView.class);
        resolver.setRequestContextAttribute("re");
        resolver.setExposeRequestAttributes(true);
        resolver.setExposeSessionAttributes(true);
        resolver.setOrder(1);
        resolver.setSuffix(".html");
        resolver.setContentType("text/html;charset=UTF-8");
        return resolver;
    }

    @Bean
    public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException {
        FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
        factory.setTemplateLoaderPath("classpath:/ftl/");
        factory.setDefaultEncoding("UTF-8");
        factory.setPreferFileSystemAccess(false);
        FreeMarkerConfigurer result = new FreeMarkerConfigurer();
        freemarker.template.Configuration configuration = factory.createConfiguration();
        configuration.setClassicCompatible(true);
        result.setConfiguration(configuration);
        Properties settings = new Properties();
        settings.put("template_update_delay", "0");
        settings.put("default_encoding", "UTF-8");
        settings.put("number_format", "0.######");
        settings.put("classic_compatible", true);
        settings.put("template_exception_handler", "ignore");
        result.setFreemarkerSettings(settings);
        return result;
    }
}

4、controller层:
(1)LoginController类

package com.student.controller;

import com.student.entity.db.SysUserDO;
import com.student.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @Author LSS
 * @Description: TODO
 * @Date in 2019/4/26 0026 8:51
 * @Modify By:
 */
@RequestMapping(value = "/jp")
@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(){
        return "login/login";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(String userName, String userPassword, HttpSession session, ModelMap modelMap){

        SysUserDO sysUserDO = new SysUserDO();
        sysUserDO.setUserName(userName);
        sysUserDO.setUserPassword(userPassword);
        List> userList = loginService.userList(sysUserDO);
        if (userList.size()>0){
            return "main/main";
        }
        return "错了老弟";
    }
}

(2)StudentController类

package com.student.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.student.entity.ReType;
import com.student.entity.db.StuInforDO;
import com.student.entity.db.SysUserDO;
import com.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @Author LSS
 * @Description: TODO
 * @Date in 2019/4/24 0024 20:20
 * @Modify By:
 */

@RequestMapping(value = "/stuInfo")
@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 单击学生信息管理菜单栏,先跳这。进入学生信息页面stuInfo.ftl
     * @return
     */
    @RequestMapping(value = "/stuInfo",method = RequestMethod.GET)
    public String stuInfo(){
        //跳入stuInfo.ftl界面
        return "/stuInfo/stuInfo";
    }

    /**
     * 从stuInfo.ftl那跳过来的,
     * @param
     * @return
     */
    @RequestMapping(value = "/stuInfoList",method = RequestMethod.GET)
    @ResponseBody
    public ReType stuInfoList(String stuName,Integer stuAge,HttpSession session,String page,String limit){
        // 登录名和密码存在HttpSession里了,要想用,就用getAttribute拿出来
        SysUserDO sysUserDO = (SysUserDO) session.getAttribute("SysUserDO");

        PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit));
        StuInforDO stuInforDO = new StuInforDO();
        stuInforDO.setStuName(stuName);
        stuInforDO.setStuAge(stuAge);
        List> stuInforDOS = studentService.stuInfoList(stuInforDO);
        PageInfo pageInfo = new PageInfo(stuInforDOS);
        ReType reType = new ReType(pageInfo.getTotal(), pageInfo.getList());
        return reType;
    }
//    @RequestMapping(value = "/stuInfoList",method = RequestMethod.POST)
//    @ResponseBody
//    public List> stuInfoList(){
//        StuInforDO stuInforDO = new StuInforDO();
//        return studentService.selectStudent(stuInforDO);
//    }
}

5、运行逆向工程生成springboot+mysql+layui++freemarker+ftl 建项目_第6张图片
PaginationPlugin类的代码如下:

package com.student.plugin;
import org.junit.Test;
import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.ShellRunner;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.util.List;

/**
 * @Author LSS
 * @Description: TODO
 * @Date in 2019/4/24 0024 16:26
 * @Modify By:
 */
public class PaginationPlugin extends PluginAdapter{

    @Override
    public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,
                                              IntrospectedTable introspectedTable) {
        // add field, getter, setter for limit clause
        addLimit(topLevelClass, introspectedTable, "limitStart");
        addLimit(topLevelClass, introspectedTable, "limitEnd");
        return super.modelExampleClassGenerated(topLevelClass, introspectedTable);
    }

    @Override
    public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(
            XmlElement element, IntrospectedTable introspectedTable) {
        XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$
        isNotNullElement.addAttribute(new Attribute("test", "limitStart != null and limitStart>=0")); //$NON-NLS-1$ //$NON-NLS-2$
        isNotNullElement.addElement(new TextElement("limit #{limitStart} , #{limitEnd}"));
        element.addElement(isNotNullElement);
        return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable);
    }
    private void addLimit(TopLevelClass topLevelClass,
                          IntrospectedTable introspectedTable, String name) {
        CommentGenerator commentGenerator = context.getCommentGenerator();
        Field field = new Field();
        field.setVisibility(JavaVisibility.PROTECTED);
        field.setType(PrimitiveTypeWrapper.getIntegerInstance());
        field.setName(name);
        commentGenerator.addFieldComment(field, introspectedTable);
        topLevelClass.addField(field);
        char c = name.charAt(0);
        String camel = Character.toUpperCase(c) + name.substring(1);
        Method method = new Method();
        method.setVisibility(JavaVisibility.PUBLIC);
        method.setName("set" + camel);
        method.addParameter(new Parameter(PrimitiveTypeWrapper.getIntegerInstance(), name));
        method.addBodyLine("this." + name + "=" + name + ";");
        commentGenerator.addGeneralMethodComment(method, introspectedTable);
        topLevelClass.addMethod(method);
        method = new Method();
        method.setVisibility(JavaVisibility.PUBLIC);
        method.setReturnType(PrimitiveTypeWrapper.getIntegerInstance());
        method.setName("get" + camel);
        method.addBodyLine("return " + name + ";");
        commentGenerator.addGeneralMethodComment(method, introspectedTable);
        topLevelClass.addMethod(method);
    }

    /**
     * This plugin is always valid - no properties are required
     */
    public boolean validate(List warnings) {
        return true;
    }

    /**
     * 自动生成底层数据层、映射层和相应xml文件
     */
    @Test
    public static void generate() {
        String config = PaginationPlugin.class.getClassLoader().getResource(
                "mybatisGeneratorConfig.xml").getFile();
        String[] arg = { "-configfile", config, "-overwrite" };
        ShellRunner.main(arg);
    }
    public static void main(String[] args) {
        generate();
    }
}

6、ReType类,定义数据返回的格式。

package com.student.entity;

import java.io.Serializable;
import java.util.List;

/**
 * @Author LSS
 * @Description: TODO
 * @Date in 2019/4/26 0026 16:03
 * @Modify By:
 */
public class ReType implements Serializable {
    /**状态*/
    public int code=0;
    /**状态信息*/
    public String msg="";
    /**数据总数*/
    public long count;
    /**查询返回的数据*/
    public List data;
    public ReType() {
    }

    public ReType(long count, List data) {
        this.count = count;
        this.data = data;
    }
}

7、可以重写sql语句查询数据。这时,需要写一个StuInforMapper.xml和StuInforMapper,配套出现的这时。
(1)StuInforMapper.xml的代码如下:




    
        
        
        
        
    
    

(2)StuInforMapper的代码如下:
此处方法名字 stuInfoList 必须对应StuInforMapper.xml的 《select id=“stuInfoList” parameterType=“com.student.entity.db.StuInforDO” resultType=“java.util.Map”>
里的id

package com.student.mapper;

import com.student.entity.db.StuInforDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Map;

public interface StuInforMapper {
    // 查询学生信息
    List> stuInfoList(StuInforDO stuInforDO);
}

8、接口类
(1)LoginService接口

package com.student.service;

import com.student.entity.db.SysUserDO;

import java.util.List;
import java.util.Map;

public interface LoginService {
    List> userList(SysUserDO sysUserDO);
}

(2)StudentService 接口

package com.student.service;

import com.student.entity.db.StuInforDO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
public interface StudentService {

// 查询学生信息
List> stuInfoList(StuInforDO stuInforDO);

}
9、实现类
(1)LoginServiceImpl接口实现类

package com.student.service.Impl;

import com.student.entity.db.SysUserDO;
import com.student.mapper.SysUserMapper;
import com.student.service.LoginService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author Administrator
 * @Description: TODO
 * @Date in 2019/4/26 0026 13:56
 * @Modify By:
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private SysUserMapper sysUserMapper;
    @Override
    public List> userList(SysUserDO sysUserDO) {
        return sysUserMapper.userList(sysUserDO);
    }
}

(2)StudentServiceImpl接口实现类

package com.student.service.Impl;

import com.student.entity.db.StuInforDO;
import com.student.mapper.StuInforMapper;
import com.student.service.StudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author Administrator
 * @Description: TODO
 * @Date in 2019/4/24 0024 20:24
 * @Modify By:
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    StuInforMapper stuInforMapper;
    // 查询学生信息
    @Override
    public List> stuInfoList(StuInforDO stuInforDO) {
        try {
            return stuInforMapper.stuInfoList(stuInforDO);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}

10、Application启动类

package com.student;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @Author LSS
 * @Description: TODO
 * @Date in 2019/4/24 0024 20:09
 * @Modify By:
 */
@SpringBootApplication
//开启注解事务管理
@EnableTransactionManagement
//扫描com.student.mapper下的文件
@MapperScan("com.student.mapper")
//定时任务
@EnableScheduling
public class Application {
    public static void main(String args[]){
        SpringApplication.run(Application.class);
    }
}

11、FTL前端界面
(1)login.ftl




    
    后台登录
    
    
    
    

<#---->
    
    
    
    
    












(2)main.ftl 登录之后进入主菜单界面




    
    
<#--len-脚手架-->
    学生管理
    <#---->
    <#---->
    <#---->
    <#---->
    
    
    
    
    









(3)stuInfo.ftl 学生信息界面




    
    学生信息管理
    
    
    
    
    
    
    
    





12、配置文件
(1) application.yml
因为现在一个项目有好多环境,开发环境,测试环境,准生产环境,生产环境,每个环境的参数不同,所以我们就可以把每个环境的参数配置到yml文件中,这样在想用哪个环境的时候只需要在主配置文件中将用的配置文件写上就行如下:
springboot+mysql+layui++freemarker+ftl 建项目_第7张图片
这个代码的意思就是启用application-test.ym的配置。
(2)application-test.ym

#设置端口号
server:
      port: 8088
      tomcat:
          remote-ip-header: x-forwarded-for
          protocol-header: x-forwarded-proto
          port-header: X-Forwarded-Port
      use-forward-headers: true
spring:
  redis:
    host: 127.0.0.1
    port: 6379
    password: 123456
    jedis:
      pool:
        #最大连接数
        max-active: 8
        #最大阻塞等待时间(负数表示没限制)
        max-wait: 10000ms
        #最大空闲
        max-idle: 8
        #最小空闲
        min-idle: 0
      #连接超时时间
      timeout: 10000
    database: 1
  http:
    multipart:
      maxFileSize: 5Mb
      maxRequestSize: 150Mb
  https:
    multipart:
      maxFileSize: 5Mb
      maxRequestSize: 150Mb
#  velocity:
#    charset: UTF-8
#    properties.input.encoding: UTF-8
#    properties.output.encoding: UTF-8
#    resourceLoaderPath: classpath:/templates/
 # freemarker设置
  mvc:
    static-path-pattern: /static/**
  freemarker:
    template-loader-path: classpath:/ftl/
    charset: utf-8
    cache: false
    expose-request-attributes: true
    expose-session-attributes: true
    expose-spring-macro-helpers: true
    suffix: .ftl

  datasource:
    name: commerce
    url: jdbc:mysql://192.168.1.232:3306/student?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: jp_loan
    # 使用druid数据源
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    minIdle: 1
    maxActive: 20
    initialSize: 1
    timeBetweenEvictionRunsMillis: 3000
    minEvictableIdleTimeMillis: 300000
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
mybatis:
  mapperLocations: classpath*:mapper/*.xml

13、mybatisGeneratorConfig.xml 逆向工厂配置





    
    
        
        
         
        
            
            
            
            
        

        
        

        
            
        

        
            
            
        

        
            
        

        
            
        

        
        

14、pom.xml 主配置文件



    4.0.0

    com.jp.ziger
    ziger
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        1.3.5.RELEASE
         
    

    
    
        UTF-8
        1.8
    

    

        
            org.springframework.boot
            spring-boot-starter-web
            
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
        



        
        
            org.springframework.boot
            spring-boot-starter-log4j2
        


        
            org.springframework.boot
            spring-boot-starter-velocity
        

        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        

        
        
            com.alibaba
            druid
            1.0.5
        

        
        
            mysql
            mysql-connector-java
            5.1.38
        


        

        
            junit
            junit
        
        
            org.springframework
            spring-test
        
        
            org.mybatis.generator
            mybatis-generator-core
            1.3.2
        



        
        
            org.springframework.boot
            spring-boot-starter-redis

        
        
        
            org.aspectj
            aspectjrt
            1.8.10
        
        
            org.aspectj
            aspectjweaver
            1.8.10
        
        
        
            org.springframework.boot
            spring-boot-devtools
            true
        

        
        
            net.sf.json-lib
            json-lib
            2.4
            jdk15
        

        
        
        
            net.sf.json-lib
            json-lib
            2.4
            jdk15
        

        
        
            net.iharder
            base64
            2.3.8
        

        
            com.aliyun.oss
            aliyun-sdk-oss
            2.4.1
        
        
            com.alibaba
            fastjson
            1.2.4
        

        
            com.aliyun
            aliyun-java-sdk-sts
            2.1.5
        
        
            com.aliyun
            aliyun-java-sdk-core
            2.1.4
        
        


        
        
            io.springfox
            springfox-swagger2
            2.4.0
        
        
            io.springfox
            springfox-swagger-ui
            2.4.0
        




        
        
            org.apache.commons
            commons-collections4
            4.0
        

        
        
            commons-fileupload
            commons-fileupload
            1.3
        


        
        
            com.aliyun
            aliyun-java-sdk-dysmsapi
            1.1.0
        



        
        
            commons-codec
            commons-codec
            1.3
        

        
            commons-httpclient
            commons-httpclient
            3.0-rc4
        

        
            commons-logging
            commons-logging
            1.0.4
        

        
            dom4j
            dom4j
            1.6.1
        



        
            org.springframework.boot
            spring-boot-starter-websocket
        

        

        
        
        
            org.apache.poi
            poi
            3.15
        



        
        
            cn.jpush.api
            jpush-client
            3.2.17
        
        
            cn.jpush.api
            jmessage-client
            1.1.7
        
        
            cn.jpush.api
            jiguang-common
            1.0.3
        
        
            io.netty
            netty-all
            4.1.6.Final
            compile
        
        
            com.google.code.gson
            gson
            2.3
        
        
            org.slf4j
            slf4j-api
            1.7.7
        


        
        
            com.github.ulisesbocchio
            jasypt-spring-boot-starter
            1.8
        

        
            net.mingsoft
            shiro-freemarker-tags
            0.1
        

        
            org.springframework.boot
            spring-boot-starter-freemarker
        

        
        
            com.github.pagehelper
            pagehelper
            5.1.2
        
        
            com.github.pagehelper
            pagehelper-spring-boot-autoconfigure
            1.2.5
        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.5
        

    

    
    
        

            
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    true
                
            


            
                maven-compiler-plugin
                3.1
                
                    true
                
            


        
    


你可能感兴趣的:(自学)