一.在你建立的工程下创建 Module 选择Spring initializr创建。

二.在Type处选择: Maven Project(项目的构建工具)

三.创建依赖时勾上web,mybatis,mysql(这个看你个人需要吧,可以自主选择)

建立好的项目结构如下:

注意:application.properties和application.yml是同一个东西,均为项目的核心配置文件

内容如下:

#连接数据库
spring.datasource.url=jdbc:mysql://localhost:3306/smbms
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#引入mybatis的配置文件
mybatis:
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.sprboot.pojo

相应的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="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>springboot</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--添加web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入spring boot包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring-Mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--使用json对象-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
</dependency>
<!--使用thymeleaf标签-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

相应的接口UserMapper如下:

@Repository
public interface UserMapper {
List<User> getList();
}

service如下:

public interface UserService {
List<User> getList();
}

impl如下:

@Service
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public List<User> getList() {
return userMapper.getList();
}
}

在resources中建一个文件夹mapper里面放mapper.xml文件,代码如下:

<select id="getList" resultType="User">
select * from smbms_user
</select>

在templates文件夹中建html文件(注意:Spring Boot中不能跳转到.jsp文件,所以只能用html)

核心代码如下:

<table>
<th>工号</th>
<th>用户名</th>
<th>姓名</th>
<th>性别</th>
<th>生日</th>
<th>电话</th>
<th>地址</th>
<th>创建时间</th>
<tr th:each="user : ${users}">
<td th:text="${user.id}"></td>
<td th:text="${user.userCode}"></td>
<td th:text="${user.userName}"></td>
<td th:text="${user.gender}"></td>
<td th:text="${user.birthday}"></td>
<td th:text="${user.phone}"></td>
<td th:text="${user.address}"></td>
<td th:text="${user.createdBY}"></td>
</tr>
</table>

此处有一个th标签,需要引入一个<html xmlns:th="http://www.thymeleaf.org">
并在pom.xml中引入对应的jar包(html中不能使用jstl表达式)

大家可以扩展一下thymeleaf的知识

控制器代码如下:

@Controller
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/")
public String getStuinforList(HttpServletRequest request, Model model){
List<User> list=userService.getList();
model.addAttribute("users",list);
System.out.println(list);
return "/index.html";
}
}

注:在调通的时候,可能会报很多的错,基本上都是注解的使用出错,希望各位能够细心点

使用Spring Boot+MyBatis框架做查询操作的更多相关文章

  1. Spring Boot + Mybatis + Redis二级缓存开发指南

    Spring Boot + Mybatis + Redis二级缓存开发指南 背景 Spring-Boot因其提供了各种开箱即用的插件,使得它成为了当今最为主流的Java Web开发框架之一.Mybat ...

  2. spring boot + mybatis + layui + shiro后台权限管理系统

    后台管理系统 版本更新 后续版本更新内容 链接入口: springboot + shiro之登录人数限制.登录判断重定向.session时间设置:https://blog.51cto.com/wyai ...

  3. Spring Boot入门教程2-1、使用Spring Boot+MyBatis访问数据库(CURD)注解版

    一.前言 什么是MyBatis?MyBatis是目前Java平台最为流行的ORM框架https://baike.baidu.com/item/MyBatis/2824918 本篇开发环境1.操作系统: ...

  4. SSM(Spring + Springmvc + Mybatis)框架面试题

    JAVA SSM框架基础面试题https://blog.csdn.net/qq_39031310/article/details/83050192 SSM(Spring + Springmvc + M ...

  5. Spring Boot + Mybatis 实现动态数据源

    动态数据源 在很多具体应用场景的时候,我们需要用到动态数据源的情况,比如多租户的场景,系统登录时需要根据用户信息切换到用户对应的数据库.又比如业务A要访问A数据库,业务B要访问B数据库等,都可以使用动 ...

  6. SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一)

    1. 前言 最近在写毕设过程中,重新梳理了一遍SSM框架,特此记录一下. 附上源码:https://gitee.com/niceyoo/jeenotes-ssm 2. 概述 在写代码之前我们先了解一下 ...

  7. 详解spring boot mybatis全注解化

    本文重点介绍spring boot mybatis 注解化的实例代码 1.pom.xml //引入mybatis <dependency> <groupId>org.mybat ...

  8. Spring boot Mybatis整合构建Rest服务(超细版)

     Springboot+ Mybatis+MySql整合构建Rest服务(涵盖增.删.改.查) 1.概要 1.1 为什么要使用Spring  boot? 1.1.1 简单方便.配置少.整合了大多数框架 ...

  9. Spring Boot 整合 Redis 实现缓存操作

    摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢!   『 产品没有价值,开发团队再优秀也无济于事 – <启示录> 』   本文提纲 ...

随机推荐

  1. unity探索者之微信分享所有流程,非第三方插件

    版权声明:本文为原创文章,转载请声明http://www.cnblogs.com/unityExplorer/p/7560575.html 很久没有写新博客了,前段时间有些忙,这几天趟了几个微信分享的 ...

  2. css3 属性 text-overflow 实现截取多余文字内容 以省略号来代替多余内容

    css3 属性 text-overflow: ellipsis 前言 正文 结束语 前言 我们在设计网站的时候有时会遇到这样一个需求:因为页面空间大小的问题,需要将多余的文字隐藏起来,并以省略号代替, ...

  3. neutron plugin 与 extension 编写流程

    原文链接:neutron plugin 与 extension 编写流程 参考: 怎样写 OpenStack Neutron 的 Plugin (一)怎样写 OpenStack Neutron 的 P ...

  4. muduo源码解析8-date类

    date class date:copyable { }: 作用: 此类作用主要是实现年月日和julianDay的互相转换内部最重要的一个数据成员m_julianDayNumber在mymuduo:: ...

  5. 使用PowerShell连接Ubuntu

    Ubuntu安装PowerShell Ubuntu安装PowerShell帮助文档 # Download the Microsoft repository GPG keys wget -q https ...

  6. 【WEB自动化测试之控件定位】基于HTML5控件的唯一控件属性定位

      一.WEB控件定位是什么 要想弄懂这个问题,我们还是基于实践来学习.我们先来看一条入门级别自动化测试用例的构成. DemoCase:正确用户名和密码登录博客园,登录成功 URL: https:// ...

  7. HDU多校-1004-Vacation(思维)

    Tom and Jerry are going on a vacation. They are now driving on a one-way road and several cars are i ...

  8. 微服务实战SpringCloud之Spring Cloud Feign替代HTTP Client

    简介 在项目中我们有时候需要调用第三方的API,微服务架构中这种情况则更是无法避免--各个微服务之间通信.比如一般的项目中,有时候我们会使用 HTTP Client 发送 HTTP 请求来进行调用,而 ...

  9. 使用kubernetes-event-exporter将k8s的事件导出到elasticsearch日志系统中

    使用kubernetes-event-exporter将k8s的事件导出到elasticsearch日志系统中 前提 版本 kubernetes v1.17.9 kubernetes-event-ex ...

  10. Ajax请求携带Cookie

    目录 xhr ajax cookie跨域处理 客户端 服务端 服务端设置跨域的几种方式 方式一 重写addCorsMappings方法 方式二 对单个接口处理 方式三 @CrossOrigin注解 方 ...