溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

eclipse下整合springboot和mybatis的方法步驟

發(fā)布時(shí)間:2020-09-30 03:14:13 來(lái)源:腳本之家 閱讀:364 作者:leexboo 欄目:編程語(yǔ)言

1.新建maven項(xiàng)目

先新建一個(gè)maven項(xiàng)目,勾選上creat a simple project,填寫(xiě)groupid,artifactid

eclipse下整合springboot和mybatis的方法步驟

2.建立項(xiàng)目結(jié)構(gòu)

eclipse下整合springboot和mybatis的方法步驟

3.添加依賴

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.0.3.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>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
  </dependency>

  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
   <dependency> 
     <groupId>org.mybatis.spring.boot</groupId> 
     <artifactId>mybatis-spring-boot-starter</artifactId> 
     <version>1.3.2</version> 
   </dependency>
   <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
  </dependency>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   </dependency>
</dependencies>
<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>

4.代碼編寫(xiě)

在包的最外層添加啟動(dòng)類(lèi)

package com.lee.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

實(shí)體類(lèi)

package com.lee.test.pojo;

import org.springframework.stereotype.Component;

@Component
public class User {

  private int id;

  private String name;

  private String telephone;

  public int getId() {
    return id;
  }

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

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getTelephone() {
    return telephone;
  }

  public void setTelephone(String telephone) {
    this.telephone = telephone;
  }

}

mapper接口

package com.lee.test.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.lee.test.pojo.User;

@Mapper
public interface UserMapper {

  List<User> getUser(int id);

}

service接口

package com.lee.test.service;

import java.util.List;

import com.lee.test.pojo.User;

public interface UserService {
  public List<User> getUser(int id);

}

service接口實(shí)現(xiàn)

package com.lee.test.service;

import java.util.List;

import com.lee.test.pojo.User;

public interface UserService {
  public List<User> getUser(int id);

}

controller層

package com.lee.test.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lee.test.pojo.User;
import com.lee.test.service.UserService;

@RestController
public class UserController {

  @Autowired
  private UserService userService;

  @RequestMapping("/getUser")
  public List<User> getUser(@RequestParam("id") int id) {
    return userService.getUser(id);
  }

}

還有mapper.xml的實(shí)現(xiàn)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://www.mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lee.test.mapper.UserMapper">
  <select id="getUser" parameterType="java.lang.Integer" resultType="com.lee.test.pojo.User">
  select * from t_user where id = #{id}
  </select>
</mapper>

最后是一些配置在application.properties中

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations: classpath:mapper/*.xml

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI