2019独角兽企业重金招聘Python工程师标准>>>
首先创建项目一个maven项目引入如下依赖
4.0.0
cn.itchao
itchao-mybatis
0.0.1-SNAPSHOT
jar
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
org.projectlombok
lombok
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
tk.mybatis
mapper-spring-boot-starter
1.1.0
com.alibaba
fastjson
1.2.47
项目结构如下图
首先需要定义一个基类接口MyBaseMapper
package cn.itcaho.base;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface MyBaseMapper extends Mapper,MySqlMapper{
}
创建表
CREATE TABLE `tb_brand` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL COMMENT '品牌名称',
`first_char` varchar(1) DEFAULT NULL COMMENT '品牌首字母',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8;
创建实体对象
package cn.itcaho.pojo;
import lombok.Data;
@Data
public class TbBrand {
private Long id;
private String name;
private String firstChar;
}
创建业务mapper接口
package cn.itcaho.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;
import cn.itcaho.base.MyBaseMapper;
import cn.itcaho.pojo.TbBrand;
@Mapper
@Component
public interface BrandMapper extends MyBaseMapper{
//自己写xml
List getBrandByName(TbBrand brand);
@Select("select id,name,first_char as firstChar from tb_brand")
List getBrandAll();
}
书写业务Service
package cn.itcaho.service;
import java.util.List;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.itcaho.mapper.BrandMapper;
import cn.itcaho.pojo.TbBrand;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;
@Service
public class BrandService {
@Autowired
private BrandMapper brandMapper;
//内置通用mapper全量查询
public List findAll(){
return brandMapper.selectAll();
}
//内置通用mapper按条件查询
public List findName(TbBrand brand){
Example example = new Example(TbBrand.class);
Criteria createCriteria = example.createCriteria();
createCriteria.andEqualTo(brand);
return brandMapper.selectByExample(example);
}
//自己书写xml查询
public List getBrandByName(TbBrand brand){
return brandMapper.getBrandByName(brand);
}
//注解查询
public List getBrandAll(){
return brandMapper.getBrandAll();
}
}
在src/main/resouces目录下创建mapper目录用于存放通过xml书写sql的文件
书写Controller
package cn.itcaho.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.RestController;
import cn.itcaho.pojo.TbBrand;
import cn.itcaho.service.BrandService;
@RestController
@RequestMapping("/brand")
public class BrandController {
@Autowired
private BrandService brandService;
@RequestMapping("/all")
public List findAll(){
return brandService.findAll();
}
@RequestMapping("/getBrandByName")
public List getBrandByName(TbBrand brand){
return brandService.getBrandByName(brand);
}
@RequestMapping("/getBrandAll")
public List getBrandAll(){
return brandService.getBrandAll();
}
}
核心来了,重点application.yml的配置
server:
port: 9000
spring:
datasource:
url: jdbc:mysql://192.168.100.131:3306/itchao-store
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
mybatis:
type-aliases-package: cn.itchao.pojo
mapper-locations: classpath:mapper/*.xml
configuration:
map-underscore-to-camel-case: true ##开启驼峰
mapper:
mappers: cn.itcaho.base.MyBaseMapper ##扫描基类接口
identity: mysql
logging:
level:
cn:
itchao:
mapper: debug
书写启动类
package cn.itcaho;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
启动测试
xml方式的接口测试
源码地址:https://gitee.com/shxjinchao/itchao-mybatis.git