先创建一个项目 选择maven项目,先什么都不勾,直接点击next groupid和artifactid可以随便填,然后点击next 点击finish finish之后弹出的项目在右下角一般都会有这个弹框,询问你是否要引入依赖,我们选择自动引入 初始项目是这样的 maven项目创建好之后,接下来
用来导入springboot web项目需要的依赖;
集成freemarker; 引入这两个依赖之后,pom文件是这样的
在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文件
随意添加一下内容
<!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>在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启动类
在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"; } }运行WebApplication中的main方法,启动springboot web项目 启动成功。 访问http://localhost:8080/admin/login 看到这样,说明springboot web项目已经搭建成功了。
在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 代表的是库名
在springboot包下创建dao包, 并创建UserInfoMapper接口文件
package com.springboot.dao; import com.springboot.entity.UserInfoEntity; import java.util.List; public interface UserInfoMapper { List<UserInfoEntity> selectList(); }在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>点击OK后,需要重启一下, 点击Restart重启 重启之后, 我们会发现, 在映射文件之间多了箭头, 可以直接点过去, 多了箭头, 平时工作时会方便不少
在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(); } }
此时, 我们启动项目, 会出现这样的错误 未找到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项目搭建完成
