开发工具

开始

新建工程

.选择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文件,所以识别不了,你看着费劲可以修改模板)

SpringBoot整合Freemarker+Mybatis的更多相关文章

  1. SpringBoot整合freemarker 引用基础

    原 ElasticSearch学习笔记Ⅲ - SpringBoot整合ES 新建一个SpringBoot项目.添加es的maven坐标如下: <dependency> <groupI ...

  2. springboot整合freemarker

    前后端分离现在越来越多,如何有效的使用springboot来整合我们的页面是一个很重要的问题. springboot整合freemarker有以下几个步骤,也总结下我所犯的错误: 1.加依赖: 2.配 ...

  3. springboot 整合 freemarker

    springboot 整合 freemarker 依赖 <parent> <groupId>org.springframework.boot</groupId> & ...

  4. 【SpringBoot】11.Springboot整合SpringMVC+Mybatis(上)

    Springboot整合SpringMVC+Mybatis 需求分析:通过使用Springboot+SpringMVC+Mybatis 整合实现一个对数据库表users表的CRUD操作. 1.创建项目 ...

  5. 【SpringBoot】09.SpringBoot整合Freemarker

    SpringBoot整合Freemarker 1.修改pom文件,添加坐标freemarker启动器坐标 <project xmlns="http://maven.apache.org ...

  6. SpringBoot学习8:springboot整合freemarker

    1.创建maven项目,添加pom依赖 <!--springboot项目依赖的父项目--> <parent> <groupId>org.springframewor ...

  7. springboot整合freemarker(转)

    添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  8. 【SpringBoot】11-1.Springboot整合Springmvc+Mybatis增删改查操作(下)

    整合过程:https://www.isdxh.com/68.html 一.增--增加用户 1.创建实体类 package com.dxh.pojo; public class Users { priv ...

  9. Spring-Boot整合freemarker引入静态资源css、js等

    一.概述 springboot 默认静态资源访问的路径为:/static 或 /public 或 /resources 或 /META-INF/resources 这样的地址都必须定义在src/mai ...

随机推荐

  1. spring 标签

    */ @Slf4j @Service public class RetryService { @Autowired private MqConfig mqConfig; /** * 如果网络连接失败, ...

  2. 21.PHP实现Word/Excel/PPT转换为PDF

    参考文档: https://www.cnblogs.com/woider/p/7003481.html http://blog.csdn.net/aoshilang2249/article/detai ...

  3. 【CPU】理解CPU

    CPU,全称Central Processing Unit,即中央处理器. 何为CPU? 计算机必须能够自动地从主存中取出一条条指令执行,专门来执行指令的就是CPU. 一.指令的执行过程 为了理解CP ...

  4. Deepfakes:AI换脸技术自制明星XX片

    ps:亮瞎狗眼 去年开始就在国外网站上比较火的项目了,通过Deepfakes技术可以将视频中的人脸换成自己喜欢的明星, 当时就有不少人制作了换脸视频,其中大部分是替换了XX片的女主角. 国外网站Red ...

  5. BZOJ2084[Poi2010]Antisymmetry——回文自动机

    题目描述 对于一个01字符串,如果将这个字符串0和1取反后,再将整个串反过来和原串一样,就称作“反对称”字符串.比如00001111和010101就是反对称的,1001就不是.现在给出一个长度为N的0 ...

  6. 洛谷P2084 进制转换

    题目背景 无 题目描述 今天小明学会了进制转换,比如(10101)2 ,那么它的十进制表示的式子就是 : 1*2^4+0*2^3+1*2^2+0*2^1+1*2^0, 那么请你编程实现,将一个M进制的 ...

  7. 为什么开启子进程 一定要放在 if __name__ == '__main__' 下面

  8. BZOJ 1497 最大获利

    最大权闭合子图 对于这个题,可以抽象成一个图论模型,如果我们把用户与其要求建立的中转站连边,获得的利益看成正权值,付出的代价看成负权值,我们可以发现,选取一个用户的时候,就相当于选取了一个闭合子图. ...

  9. centos6.8下安装matlab2009(图片转帖)

    前言 如何优雅的在centos6.8上安装matlab2009. 流程 不过我个人安装过程完后启动matlab的时候又出现了新问题: error while loading shared librar ...

  10. linux tar 解压命令

    如果提示 common not find 先进行安装如下 wget http://www.rarsoft.com/rar/rarlinux-5.3.0.tar.gz tar -zxvf rarlinu ...