SpringBoot整合Freemarker+Mybatis

开发工具

开始

新建工程

.选择Spring Initializr

下一步

下一步,选择需要的组件

..改一下工程名,Finish

..目录结构

首先,修改pom文件

然后,将application.properties改成yml文件,并且配置相关参数

我的数据库很简单,user表,int类型的自增id,varchar类型的name。

之后建立各个文件,目录结构如下:

HelloController

package com.example.controller;

import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@RestController
public class HelloController {

    @Autowired
    private UserService userService;

    @RequestMapping("/index")
    public String index(){
        return "Hello !!";
    }

    @RequestMapping("/add")
    public Map<Integer, Integer> addUsers(){
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < 10; i++){
            map.put(i, userService.addUser(new User("name-" + i)));
        }
        return map;
    }

    @RequestMapping("/get")
    public Map<String, Object> getUsers(){
        Map<String, Object> map = new HashMap<>();
        map.put("users", userService.getUsers());
        return map;
    }

    @RequestMapping("/view")
    public ModelAndView viw(){
        ModelAndView mv = new ModelAndView("home");
        mv.addObject("name","Tom‘s Table");
        mv.addObject("users", userService.getUsers());
        return mv;
    }
}

UserDAO

package com.example.dao;

import com.example.entity.User;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserDAO {

    int addUser(User user);

    List<User> getUsers();
}

User

package com.example.entity;

public class User {

    private int id;
    private String name;

    public User(String name) {
        this.name = name;
    }

    public User() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name=‘" + name + ‘\‘‘ +
                ‘}‘;
    }
}

UserService

package com.example.service;

import com.example.entity.User;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public interface UserService {

    int addUser(User user);

    List<User> getUsers();
}

UserServiceImpl

package com.example.service.impl;

import com.example.dao.UserDAO;
import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO;

    @Override
    public int addUser(User user) {
        return userDAO.addUser(user);
    }

    @Override
    public List<User> getUsers() {
        return userDAO.getUsers();
    }
}

然后在Resource文件夹新建一个mybatis文件夹,用来存放mapper

UserDAO.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.dao.UserDAO">

    <insert id="addUser" parameterType="User">INSERT INTO USER(NAME) VALUES (#{name})</insert>

    <select id="getUsers" resultType="User">SELECT * FROM USER </select>

</mapper>

home.css

@charset "UTF-8";

h1{
    color: #ff527d;
}

home.ftl

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
    <link rel="stylesheet" type="text/css" href="/css/home.css">
</head>
<body>
    <h1>${name}</h1>
    <table>
        <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
        </tr>
        </thead>
        <tbody>
        <#list users as u>
            <tr>
                <td>${u.id}</td>
                <td>${u.name}</td>
            </tr>
        </#list>
        </tbody>
    </table>
</body>
</html>

对于mybatis相关xml生成以及操作,ftl文件的新建,请看最后。

最关键的类:启动类,很有必要说明一下。

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@MapperScan("com.example.dao")
@ComponentScan("com.example")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

默认SpringBoot的装配规则是: 只扫描Application所在的包以及所有子包!这个坑我一直踩却没有怀疑他。

@MapperScan-----扫描Mapper类

@ComponentScan----扫描指定的包

到此为止,可以启动SpringBoot应用了。

---------------------

---------------------

----------------------最后一个,从后台读取数据,freemarker渲染视图

下面进行采坑指南---

第一个,用过eclipse+mybatis插件的都知道,有个插件来生成mybatis配置文件和映射文件是个方便的事,其实IDEA也有,但是用的我有点不习惯↓↓

..Settings

..找到下面这个

..搜索mybatis,选择下面这三个,主要是第二个和第三个,单独选中右侧会出现Install选项

..安装完之后,会让你重启,重启之后,会出现Mybatis的选项,不过问题是,它只有config的新建选项,没有mapper的新建选项。

..我不知道大家有啥更好的方法没有,反正我去尝试编辑右键菜单,无果。只好采取以下方法。

..找到other选项卡,看到这里你们肯定会问,这里不是有吗,对呀,你有本事把它搞到右键菜单啊。复制右侧的内容

..回到Files,点击加号,输入Name,扩展名,并把复制的内容粘贴到此处。注意把namespace里面的代码去掉

点击OK,可以看到,出现了Mapper选项,但是!他并没有像eclipse里面的插件那样智能,他只是生成一个普通的文件而已。里面的namespace的值需要你自己修改。

到此,实现了Mybatis配置文件的代码追踪等等实用的操作

插一句:网上都是告诉你使用generator来生成mapper文件以及简单的sql语句,节省了时间,但是我不是特别喜欢那种全自动的。

 第二个,关于freemarker模板的问题。

SpringBoot的文件目录:static用来存放静态文件,templates用来存放视图。那么,freemarker模板需要引用static里面的css文件呢?

如果在freemarker文件里面直接这样写,他说他找不到,但是,你不用管它!当项目运行起来,SpringBoot会自动从static里面去找。如果你有强迫症,没关系,我能忍。

第三个,同样,IDEA如何创建freemarker文件?

唉,没有相应的模板。你可以自己建一个:

复制HTML5的模板

点击加号,新建一个

点击OK,右键new

点击,输入文件名(这里的$Title$,因为不是html文件,所以识别不了,你看着费劲可以修改模板)

原文地址:https://www.cnblogs.com/LUA123/p/8456898.html

时间: 2024-08-30 12:58:08

SpringBoot整合Freemarker+Mybatis的相关文章

springboot整合freemarker

前后端分离现在越来越多,如何有效的使用springboot来整合我们的页面是一个很重要的问题. springboot整合freemarker有以下几个步骤,也总结下我所犯的错误: 1.加依赖: 2.配置文件修改: 3.在templates下面编写后缀为ftl的页面: 4.错误出现:404问题: (1)检查是@RestController还是@Controller,如果要返回页面必须用@Controller (2)这次问题出现的很粗心:(漏掉一个小点) 原文地址:https://www.cnblo

SpringBoot学习8:springboot整合freemarker

1.创建maven项目,添加pom依赖 <!--springboot项目依赖的父项目--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dep

springboot整合freemarker自动加载宏

springboot引入freemarker最大的问题,在于springboot的application.properties配置文件,不能覆盖所有的freemarker配置 如果freemarker有自定义宏应该怎样引入呢? 首先 application.properties增加配置(与freemarker.properties一样) auto_import = _auto_include = /layout/pageShow.html 然后增加FreemarkerConfig类,启动即可 /

dubbo学习(三) springboot整合dubbo mybatis mysql

dubbo-admin查看服务和服务提供方配置 服务消费方配置 和web 整合相对复杂一些,常出现的问题是Controller中 有@Reference配置的属性  没注入进来而调用时报null 异常 原文地址:https://www.cnblogs.com/LDDXFS/p/9941203.html

SpringBoot整合freemarker中自定义标签获取字典表的数据

因为在前端要根据字典表中的数据去将1.2这些值转换成对应的文字解释 1.首先要创建一个类去实现 TemplateDirectiveModel 类 @Component public class DictDirective implements TemplateDirectiveModel { @Override public void execute(Environment environment, Map map, TemplateModel[] templateModels, Templat

springboot学习入门简易版四---springboot2.0静态资源访问及整合freemarker视图层

2.4.4 SpringBoot静态资源访问(9) Springboot默认提供静态资源目录位置需放在classpath下,目录名需要符合如下规则 /static  /public  /resources  /META-INF/resources 可以在src/main/resources目录下创建static,在该位置放置一个图片文件. 启动程序后,尝试访问http://localhost:8080/D.JPG,如能显示图片,配置成功. 2.5 SpringBoot整合freemarker视图

springboot整合mybatis,freemarker

springboot 整合mybaits,,freemarker 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=&

SpringBoot 2.SpringBoot整合Mybatis

一.创建Springboot的配置文件:application.properties SpringApplication 会从 application.properties 文件中加载配置信息,下面是添加Spring配置信息的文件目录顺序: 当前目录下的/config子目录中 当前目录中 一个 classpath 包下的 /config 目录中 classpath 根目录中 大家根据自己习惯来即可. /application.properties 文件配置如下: spring.datasourc

SpringMVC+FreeMarker+Mybatis 整合

这个项目自己有时写写测试代码什么的, 代码比较简单,代码已放在 github : https://github.com/zhouyongtao/homeinns-web 目录结构: 贴一下Maven的POM <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=&quo