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. SERE0014: Illegal HTML character: decimal 154

    问题:jmeter,生成报告转化成html,报错SERE0014: Illegal HTML character: decimal 154 原因: 某些字符,特别是控制字符#x7F-#x9F ,在XM ...

  2. Python学习笔记 (2.2)Python中的字符编码问题及标准数据类型之String(字符串)

    Python3中的String类型 首先,Python中没有字符类型,只有字符串类型.单个字符按照长度为1的字符串处理,这对于曾是OIER的我来说有点不适应啊. 字符串的表示方法 最常用的就是用一对双 ...

  3. IM聊实现客户端之间信息交互需求文档

    终于放假啦~之前学习太忙很多知识点都没有写博客,可能自己学会了但没有分享给大家,接下来几天我可能把一些学过的东西整理成博客发出来供大家相互学习交流. 需求分析说明书 HuaXinIM聊软件 潘浩 20 ...

  4. 4560 NOIP2015 D2T2 子串 code vs

    4560 NOIP2015 D2T2 子串  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 有两个仅包含小写 ...

  5. Lein: Exception in thread "Thread-3" java.net.ConnectException: Connection refused

    leiningen Leiningen是你的主要工具, 它用于: 启动一个 REPL 下载+安装类库 运行你的程序 启动一个服务器, 运行你所写的webapps 安装 brew install lei ...

  6. CI session 类的用法

    最近使用codeingiter框架,发现默认的session 不是很好用,以下是用法总结:使用的是2.0.2的版本 1.扩展自带的session类:application/libraries/MY_s ...

  7. ORACLE EM的删除与创建

    手动删除ORACLE 10G EM 使用emca可以手动配置em! 配置em的过程中有一个环节要特别主要: 不论使用dbca还是使用emca -deconfig dbcontrol db -repos ...

  8. JConsole使用手冊具体解释

    一篇Sun项目主页上介绍JConsole使用的文章,前段时间性能測试的时候大概翻译了一下以便学习,今天整理一下发上来.有些地方也不知道怎么翻,就保留了原文,可能还好理解点.呵呵,水平有限,翻的不好,大 ...

  9. POJ 2104 K-th Number(区间第k大数)(平方切割,归并树,划分树)

    题目链接: http://poj.org/problem? id=2104 解题思路: 由于查询的个数m非常大.朴素的求法无法在规定时间内求解. 因此应该选用合理的方式维护数据来做到高效地查询. 假设 ...

  10. 推荐美丽的flash网页MP3音乐播放器

    文章来源:PHP开发学习门户 地址:http://www.phpthinking.com/archives/491 在网页制作中.假设想在网页中插入mp3音乐来增添网页的互动感,提升用户体验度,这个时 ...