1.新建maven项目

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

2.建立项目结构

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.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.lee.test.mapper.UserMapper;
import com.lee.test.pojo.User;
import com.lee.test.service.UserService; @Service
public class UserServiceImpl implements UserService { @Autowired
private UserMapper userMapper; @Override
public List<User> getUser(int id) {
return userMapper.getUser(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

eclipse下整合springboot和mybatis的更多相关文章

  1. Seata整合SpringBoot和Mybatis

    Seata整合SpringBoot和Mybatis 一.背景 二.实现功能 三.每个服务使用到的技术 1.账户服务 2.订单服务 四.服务实现 1.账户服务实现 1.引入jar包 2.项目配置 3.建 ...

  2. 2分钟在eclipse下使用SpringBoot搭建Spring MVC的WEB项目

    1. 首先用eclipse创建一个maven工程, 普通maven工程即可 2. 修改pom如下: <?xml version="1.0" encoding="UT ...

  3. intellij idea 整合springboot和mybatis

    参考: http://blog.csdn.net/winter_chen001/article/details/77249029

  4. 基于 SpringBoot2.0+优雅整合 SpringBoot+Mybatis

    SpringBoot 整合 Mybatis 有两种常用的方式,一种就是我们常见的 xml 的方式 ,还有一种是全注解的方式.我觉得这两者没有谁比谁好,在 SQL 语句不太长的情况下,我觉得全注解的方式 ...

  5. SpringBoot和Mybatis的整合

    这里介绍两种整合SpringBoot和Mybatis的模式,分别是“全注解版” 和 “注解xml合并版”. 前期准备开发环境 开发工具:IDEAJDK:1.8技术:SpringBoot.Maven.M ...

  6. SpringBoot与Mybatis整合方式01(源码分析)

    前言:入职新公司,SpringBoot和Mybatis都被封装了一次,光用而不知道原理实在受不了,于是开始恶补源码,由于刚开始比较浅,存属娱乐,大神勿喷. 就如网上的流传的SpringBoot与Myb ...

  7. springboot+springmvc+mybatis项目整合

    介绍: 上篇给大家介绍了ssm多模块项目的搭建,在搭建过程中spring整合springmvc和mybatis时会有很多的东西需要我们进行配置,这样不仅浪费了时间,也比较容易出错,由于这样问题的产生, ...

  8. SpringBoot系列——MyBatis整合

    前言 MyBatis官网:http://www.mybatis.org/mybatis-3/zh/index.html 本文记录springboot与mybatis的整合实例:1.以注解方式:2.手写 ...

  9. 零基础IDEA整合SpringBoot + Mybatis项目,及常见问题详细解答

    开发环境介绍:IDEA + maven + springboot2.1.4 1.用IDEA搭建SpringBoot项目:File - New - Project - Spring Initializr ...

随机推荐

  1. Gym - 101670G Ice cream samples(CTU Open Contest 2017 尺取法)

    题目: To encourage visitors active movement among the attractions, a circular path with ice cream stan ...

  2. LINUX-光盘

    cdrecord -v gracetime=2 dev=/dev/cdrom -eject blank=fast -force 清空一个可复写的光盘内容 mkisofs /dev/cdrom > ...

  3. 解决CUDA程序的黑屏恢复问题

    本文引用自 http://blog.163.com/yuhua_kui/blog/static/9679964420146183211348/ 问题描述:   在运行CUDA程序时,出现黑屏,过一会儿 ...

  4. Java基础学习总结(78)——Java main方法深入研究学习

    1.不用main方法如何定义一个类? 不行,没有main方法我们不能运行Java类. 在Java 7之前,你可以通过使用静态初始化运行Java类.但是,从Java 7开始就行不通了. 2.main() ...

  5. mongodb local数据库的空间初始化好大啊!

    新建立了一个replicat set,登录到primary里,show dbs一看吓一跳 local数据库竟然占用了80多G的空间 [root@wxlab31 bin]# ./mongo --host ...

  6. Eddy's mistakes

    Problem Description Eddy usually writes  articles ,but he likes mixing the English letter uses, for ...

  7. Android GIS开发系列-- 入门季(6)GraphicsLayer添加文字与图片标签

    一.GraphicsLayer添加图片 GraphicLayer添加图片Graphic,要用到PictureMarkerSymbol,也是样式的一种.添加代码如下: Drawable drawable ...

  8. nodejs 运行

    运行 Node.js 程序的基本方法就是执行 node script.js,其中 script.js①是脚本的文件名. 除了直接运行脚本文件外,node --help 显示的使用方法中说明了另一种输出 ...

  9. 迭代器概念与traits编程技法

    //迭代器是一种smart pointer template<typename T> class ListItem { public: T value() const { return _ ...

  10. Windows 注册表常用操作

    1 添加一个主键(比如在HKEY_LOCAL_MACHINE\SOFTWARE\中添加一个ABCEDFGHIJKLMN主键) Windows Registry Editor Version 5.00 ...