使用Spring Boot+MyBatis框架做查询操作的示例代码

 更新时间:2018年10月18日 09:37:18   作者:cairui  
这篇文章主要介绍了使用Spring Boot+MyBatis框架做查询操作的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

一.在你建立的工程下创建 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";
  }
}

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • java的Arrays工具类实战

    java的Arrays工具类实战

    java.util.Arrays类能方便地操作数组,它提供的所有方法都是静态的。Arrays作为一个工具类,能很好的操作数组。下面介绍主要使用的几个函数
    2016-12-12
  • 关于web项目读取classpath下面文件的心得分享

    关于web项目读取classpath下面文件的心得分享

    这篇文章主要介绍了关于web项目读取classpath下面文件的心得,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • Java如何定义Long类型

    Java如何定义Long类型

    这篇文章主要介绍了Java如何定义Long类型,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • Java中字符串中连续相同字符去重方法

    Java中字符串中连续相同字符去重方法

    今天小编就为大家分享一篇Java中字符串中连续相同字符去重方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • CompletableFuture 异步编排示例详解

    CompletableFuture 异步编排示例详解

    这篇文章主要为大家介绍了CompletableFuture 异步编排示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • Spring中@Autowired注解在不同方法的写法示例

    Spring中@Autowired注解在不同方法的写法示例

    这篇文章主要为大家介绍了Spring中@Autowired注解在不同方法的写法示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • springboot项目(jar包)指定配置文件启动图文教程

    springboot项目(jar包)指定配置文件启动图文教程

    这篇文章主要给大家介绍了关于springboot项目(jar包)指定配置文件启动的相关资料,在多环境部署过程中、及线上运维中可能会遇到临时指定配置文件的情况,需要的朋友可以参考下
    2023-07-07
  • springboot 通过博途获取plc点位的数据代码实现

    springboot 通过博途获取plc点位的数据代码实现

    这篇文章主要介绍了springboot 通过博途获取plc点位的数据的代码实现,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-08-08
  • SpringBoot处理JSON数据方法详解

    SpringBoot处理JSON数据方法详解

    这篇文章主要介绍了SpringBoot整合Web开发中Json数据处理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-10-10
  • SpringBoot图文并茂详解如何引入mybatis与连接Mysql数据库

    SpringBoot图文并茂详解如何引入mybatis与连接Mysql数据库

    这篇文章主要介绍了SpringBoot如何引入mybatis与连接Mysql数据库,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07

最新评论