欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-27 22:47:09
1.新建maven项目 先新建一个maven项目,勾选上creat a simple project,填写groupid,artifactid 2.建立项目结构...

1.新建maven项目

先新建一个maven项目,勾选上creat a simple project,填写groupid,artifactid

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

2.建立项目结构

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.代码编写

在包的最外层添加启动类

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);
  }
}

实体类

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接口实现

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的实现

<?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

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