spring-boot简单整合mybatis

硅谷探秘者 2600 0 0

1.项目结构

image.png

2.pom文件依赖

<?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>club.jiajiajia.springboot</groupId>
  <artifactId>springbootTest</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>springbootTest</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
  <!-- 继承父包 -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath></relativePath>
  </parent>

  <dependencies>
    <!-- spring-boot的web启动的jar包 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>


    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>


    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.0</version>
    </dependency>

  </dependencies>


</project>

3.application.properties文件配置

server.port=8081

#springboot   mybatis
#jiazai mybatis peizhiwenjian
mybatis.mapper-locations = classpath:mapper/*Mapper.xml
mybatis.config-location = classpath:mapper/config/sqlMapConfig.xml
mybatis.type-aliases-package = club.jiajiajia.springboot.bean

#shujuyuan
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://1111110:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = ******

4.sqlMapConfig.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>

</configuration>

5.UserMapper.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="club.jiajiajia.springboot.dao.UserMapper">
    <select id="findAll" resultType="user">
        select * from user
    </select>
</mapper>

6.User

package club.jiajiajia.springboot.bean;

public class User {
    private Integer id;
    private String name;
    private Integer age;

    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return id+":"+name+":"+age;
    }
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}

7.UserMapper

package club.jiajiajia.springboot.dao;

import club.jiajiajia.springboot.bean.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
@Mapper
public interface UserMapper {
    public List<User> findAll();
}

8.Controller

package club.jiajiajia.springboot.controller;

import club.jiajiajia.springboot.bean.User;
import club.jiajiajia.springboot.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
public class SpringBootTestController {
    @Autowired
    private UserMapper userMapper;

    @GetMapping("/test")
    public Map<String,Object> test(){

        List<User> users=userMapper.findAll();
        System.out.println(users);
        Map<String,Object> map=new HashMap<>();
        map.put("code",200);
        map.put("data",users);
        return map;
    }
}

9.Main

package club.jiajiajia.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * Hello world!
 *
 */
@SpringBootApplication
public class HelloWorldSpringbootMain
{
    public static void main( String[] args )
    {
        SpringApplication.run(HelloWorldSpringbootMain.class,args);
    }
}

10.访问接口:


image.png


评论区
请写下您的评论...
暂无评论...
猜你喜欢
框架 2562 springbootmybatis1.创建maven项目2.sql文件SETNAMESutf8mb4;SETFOREIGN_KEY_CHECKS=0
spring/springmvc 1538 springmvc+mybatisshiro权限1.需要的jar包propertiesshiro.version1.3.2/shiro.version/propertiesdependency
框架 2345 在springboot视图层,官方推荐使用thymeleaf。thymeleaf只是渲染html的一种方式,是一种模板。第一步创建一个maven项目第二步:修改Jdk版本,添加thymeleaf
linux系统 1653 编写shell脚本有两点要求1.脚本一般都是以#!/bin/bash开头(告诉我们系统我们这个脚本使用bash进行解释,不加也可以运行)2.给予脚本可执行的权限3.执行shell脚本的方式1)./脚本名2)sh+脚本名(此种方式不需要有可执行权限,但是不建议使用)
spring/springmvc 2649 搭建spring+springmvc+mybatis+maven项目(2)在搭建spring+springmvc+mybatis+maven项目(1)中我们搭建了基本的maven环境,并且可以运行项
weblog 1027 pomparent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId
框架 2667 1.配置springboot支持websocketpackagecom.example.demo.websocket;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.web.so
框架 2222 创建一个Maven项目,2.修改jdk版本(因为这里使用的springboot是1.5,在2.0一下springboot推荐使用1.7)!--修改jdk版本springboot2.0之前推荐使用Jdk1.7--properties java.version1.7/java.version/propertiesdependencies3.注入springboot启动器坐标所谓的springboot启
归档
2018-11  12 2018-12  33 2019-01  28 2019-02  28 2019-03  32 2019-04  27 2019-05  33 2019-06  6 2019-07  12 2019-08  12 2019-09  21 2019-10  8 2019-11  15 2019-12  25 2020-01  9 2020-02  5 2020-03  16 2020-04  4 2020-06  1 2020-07  7 2020-08  13 2020-09  9 2020-10  5 2020-12  3 2021-01  1 2021-02  5 2021-03  7 2021-04  4 2021-05  4 2021-06  1 2021-07  7 2021-08  2 2021-09  8 2021-10  9 2021-11  16 2021-12  14 2022-01  7 2022-05  1 2022-08  3 2022-09  2 2022-10  2 2022-12  5 2023-01  3 2023-02  1 2023-03  4 2023-04  2 2023-06  3 2023-07  4 2023-08  1 2023-10  1 2024-02  1 2024-03  1 2024-04  1
标签
算法基础 linux 前端 c++ 数据结构 框架 数据库 计算机基础 储备知识 java基础 ASM 其他 深入理解java虚拟机 nginx git 消息中间件 搜索 maven redis docker dubbo vue 导入导出 软件使用 idea插件 协议 无聊的知识 jenkins springboot mqtt协议 keepalived minio mysql ensp 网络基础 xxl-job rabbitmq haproxy srs 音视频 webrtc javascript
目录
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。