最简 最快 最全方式集成SpringBoot Mybatis(注解式) (Eclipse Maven)

 

一.新建maven项目

二.代码

1.pom.xml

<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>com.jz.test</groupId>
	<artifactId>springboottest</artifactId>
	<version>0.0.1-SNAPSHOT</version>
   
    <dependencies> 
        <dependency> 
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.7.RELEASE</version>
        </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>
            <version>5.1.21</version>
        </dependency>
    </dependencies>
</project>

2.实体类

package com.jz.test.entity;


public class TrpApi {
	private String id;
	private String apiType;
	private String url;
	private String name;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getApiType() {
		return apiType;
	}
	public void setApiType(String apiType) {
		this.apiType = apiType;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	
}

3.application.yml

server:
  port: 8888
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/TRP?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: password#@!
    driver-class-name: com.mysql.jdbc.Driver

4.Mapper

package com.jz.test.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import com.jz.test.entity.PageData;
import com.jz.test.entity.TrpApi;

/*
	@Mapper注解的的作用
	1:为了把mapper这个DAO交給Spring管理 http://412887952-qq-com.iteye.com/blog/2392672
	2:为了不再写mapper映射文件 https://blog.csdn.net/phenomenonstell/article/details/79033144
	3:为了给mapper接口 自动根据一个添加@Mapper注解的接口生成一个实现类 http://www.tianshouzhi.com/api/tutorials/mapstruct/292
 */
@Mapper
public interface TrpApiMapper {
	/*
	 * 增删改查注解对应
	 * @Insert
	 * @Delete
	 * @Update
	 * @Select
	 * */
	@Select("select * from trp_api")
	List<PageData> selectAll();
	@Select("select * from trp_api where POSITION(#{name} in name)")
	List<TrpApi> selectByName(String name);
}

5.Controller

package com.jz.test.controller;

import java.util.List;

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

import com.jz.test.entity.PageData;
import com.jz.test.entity.TrpApi;
import com.jz.test.mapper.TrpApiMapper;

@RestController
@RequestMapping("/api")
public class TrpApiController {
	@Autowired
	private TrpApiMapper apiMapper;
	
	@RequestMapping(value="/", method=RequestMethod.GET)
	public List<PageData> selectAll(String id){
		return apiMapper.selectAll();
	}
	
	@RequestMapping(value="{name}", method=RequestMethod.GET)
	public List<TrpApi> selectByName(@PathVariable("name") String name){
		return apiMapper.selectByName(name);
	}
}

6.启动类

package com.jz.test;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
/*
 * 指定mybatis扫描com.jz.test.mapper包下的Mapper
 */
@MapperScan(basePackages = "com.jz.test.mapper")
public class SpringApp {
	public static void main(String[] args) {
		SpringApplication.run(SpringApp.class, args);
	}
}

三.项目结构

四.启动测试

在main方法下直接运行服务器

打开浏览器输入地址

成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值