搭建springboot+mybatis+freemarker项目

1. 创建springboot web项目

先创建一个项目
在这里插入图片描述
选择maven项目,先什么都不勾,直接点击next
在这里插入图片描述
groupid和artifactid可以随便填,然后点击next
在这里插入图片描述
点击finish
在这里插入图片描述
finish之后弹出的项目在右下角一般都会有这个弹框,询问你是否要引入依赖,我们选择自动引入
在这里插入图片描述
初始项目是这样的
在这里插入图片描述
maven项目创建好之后,接下来

1、首先,pom.xml文件添加springboot web依赖:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

用来导入springboot web项目需要的依赖;

2、添加freemarker依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
            <!--<version>1.5.2.RELEASE</version>-->
        </dependency>

集成freemarker;
引入这两个依赖之后,pom文件是这样的

在这里插入图片描述

3、配置freemarker

resources文件夹下创建application.properties文件,并添加

#此处为配置静态文件的位置;注意:千万千万不能与ftl模板文#件的存放位置重合,那样会导致ftl文件以静态方式解析。
#spring.resources.static-locations=classpath:/resources/,classpath:/static/html/,classpath:/templates/
#freemarker配置
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
#默认以.ftl结尾
spring.freemarker.suffix=.ftl
#ftl文件存放的位置
spring.freemarker.template-loader-path=classpath:/templates/

在这里插入图片描述
application.properties配置文件中的spring.freemarker.template-loader-path属性可以看出,我们是要把ftl文件保存在templates文件夹下的,因此,我们在resources文件夹下创建templates文件夹,用来存放ftl文件,并创建login.ftl文件

4. 创建login.ftl文件

随意添加一下内容

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
    <title>springbootDemo</title>
</head>

<body>
<h1>哈哈哈哈哈哈哈哈</h1>
</body>
</html>

在这里插入图片描述

5.创建WebApplication启动类

在java文件夹下创建com.springboot包,并在该包下创建WebApplication类作为启动类

package com.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

添加@SpringBootApplication注解代表这是一个springboot启动类
在这里插入图片描述

6.创建Controller

在springboot包下创建controller包,在controller包下创建IndexCtrl类,添加@Controller注解代表这是一个Controller

package com.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class IndexCtrl {

    @GetMapping("admin/login")
    public String login() {
		System.out.println("hello world !");
        return "login";
        
    }
}

在这里插入图片描述

7. 启动WebApplication

运行WebApplication中的main方法,启动springboot web项目
在这里插入图片描述
启动成功。
访问http://localhost:8080/admin/login
在这里插入图片描述
看到这样,说明springboot web项目已经搭建成功了。

2. 集成mybatis

2.1创建实体类

在springboot包下创建entity包,并创建实体类UserInfoEntity

package com.springboot.entity;

import javax.persistence.Table;

@Table(name = "user_info", schema = "pangting")
public class UserInfoEntity {
    private String userName;
    private Integer age;
    private Double height;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Double getHeight() {
        return height;
    }

    public void setHeight(Double height) {
        this.height = height;
    }
}

@Table(name = “user_info”, schema = “pangting”)注解中,name代表的是表名,pangting 代表的是库名

2.2 创建dao层映射接口

在springboot包下创建dao包, 并创建UserInfoMapper接口文件

package com.springboot.dao;

import com.springboot.entity.UserInfoEntity;

import java.util.List;

public interface UserInfoMapper {

    List<UserInfoEntity> selectList();
    
}

在这里插入图片描述

2.3 创建xml映射文件

在resources文件夹下创建mapper包, 用来保存xml映射文件, 并且在mapper包下创建UserInfoMapper.xml文件,对应UserInfoEntity实体类.

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.springboot.dao.UserInfoMapper" >
    <resultMap id="BaseResultMap" type="com.springboot.entity.UserInfoEntity" >
        <result column="user_name" property="userName" jdbcType="VARCHAR" />
        <result column="age" property="age" jdbcType="INTEGER" />
        <result column="height" property="height" jdbcType="DOUBLE" />
    </resultMap>

    <select id="selectList"  resultMap="BaseResultMap" >
        select
        info.user_name,
        info.age,
        info.height
        from
        pangting.user_info as info

    </select>

</mapper>

在这里插入图片描述

2.4安装mybatis插件

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
点击OK后,需要重启一下, 点击Restart重启
在这里插入图片描述
在这里插入图片描述
重启之后, 我们会发现, 在映射文件之间多了箭头, 可以直接点过去, 多了箭头, 平时工作时会方便不少

2.5 创建service层业务代码

在springboot包下创建service包, 并创建UserInfoService接口, 接着创建其实现类UserInfoServiceImpl
UserInfoService

package com.springboot.service;

import com.springboot.entity.UserInfoEntity;

import java.util.List;

public interface UserInfoService {
    List<UserInfoEntity> selectList();
}

UserInfoServiceImpl

package com.springboot.service.impl;

import com.springboot.dao.UserInfoMapper;
import com.springboot.entity.UserInfoEntity;
import com.springboot.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public List<UserInfoEntity> selectList() {
        return userInfoMapper.selectList();
    }
}

在这里插入图片描述
在这里插入图片描述

2.6 application.properties配置文件添加mybatis配置

#Mybatis配置
#pangting为默认的数据库
spring.datasource.url=jdbc:mysql://localhost:3306/pangting?characterEncoding=utf8&characterSetResults=utf8&autoReconnect=true&failOverReadOnly=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#实体类所在的包
mybatis.type-aliases-package=com.springboot.entity

#driverClassName: com.mysql.cj.jdbc.Driver
#mybatis配置文件的位置
mybatis.config-location=classpath:mybatis-config.xml
#xml文件所在的包(SQL语句)
mybatis.mapper-locations=classpath:mapper/*.xml

在这里插入图片描述

2.7 在resources文件夹下创建mybatis-config.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--<settings>
        <setting name="logImpl" value="SLF4J" />
    </settings>-->
    <typeAliases>
        <typeAlias alias="Integer" type="java.lang.Integer" />
        <typeAlias alias="Long" type="java.lang.Long" />
        <typeAlias alias="HashMap" type="java.util.HashMap" />
        <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
        <typeAlias alias="ArrayList" type="java.util.ArrayList" />
        <typeAlias alias="LinkedList" type="java.util.LinkedList" />

    </typeAliases>


</configuration>

在这里插入图片描述
此时, 我们启动项目, 会出现这样的错误
在这里插入图片描述
未找到UserInfoMapper这个bean, 原因是spring没有扫描到这个接口文件. 因此, 我们需要在启动类WebApplication
上添加注解@MapperScan(“com.springboot.dao”)

在这里插入图片描述
次注解是专门用来扫描Mapper文件的, com.springboot.dao 代表要扫描的包名, 因此 ,我们之后的Mapper文件都写在这个包下

此时再启动项目
在这里插入图片描述
启动成功.
修改IndexCtrl中的业务逻辑和login.ftl文件
IndexCtrl

package com.springboot.controller;

import com.springboot.entity.UserInfoEntity;
import com.springboot.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class IndexCtrl {
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("admin/login")
    public String login(Model model) {

        List<UserInfoEntity> userInfoEntities = userInfoService.selectList();
        System.out.println(userInfoEntities.toString());
        model.addAttribute("userInfoList", userInfoEntities);
        return "login";

    }

}

login.ftl

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
    <title>springbootDemo</title>
</head>

<body>
<h1>哈哈哈哈哈哈哈哈</h1>

<#list userInfoList as user>
    <h1>姓名: ${user.userName},年龄: ${user.age},身高: ${user.height}</h1>
</#list>
</body>
</html>

在这里插入图片描述
在这里插入图片描述
数据库表及数据
在这里插入图片描述

重启项目,访问http://localhost:8080/admin/login
在这里插入图片描述
自此, springboot+mybatis+freemarker项目搭建完成

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值