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

 

一.新建maven项目

最简 最快 最全方式集成SpringBoot Mybatis(注解式) (Eclipse Maven)_第1张图片

最简 最快 最全方式集成SpringBoot Mybatis(注解式) (Eclipse Maven)_第2张图片

最简 最快 最全方式集成SpringBoot Mybatis(注解式) (Eclipse Maven)_第3张图片

二.代码

1.pom.xml


	4.0.0
	com.jz.test
	springboottest
	0.0.1-SNAPSHOT
   
     
         
            org.springframework.boot
            spring-boot-starter-web
            1.5.7.RELEASE
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        
        
            mysql
            mysql-connector-java
            5.1.21
        
    

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 selectAll();
	@Select("select * from trp_api where POSITION(#{name} in name)")
	List 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 selectAll(String id){
		return apiMapper.selectAll();
	}
	
	@RequestMapping(value="{name}", method=RequestMethod.GET)
	public List 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);
	}
}

三.项目结构

最简 最快 最全方式集成SpringBoot Mybatis(注解式) (Eclipse Maven)_第4张图片

四.启动测试

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

打开浏览器输入地址

成功

你可能感兴趣的:(java,springboot)