springboot集成mybatis

springboot集成mybatis

添加mybatis,选择数据库驱动,Druid包
在这里插入图片描述
Druid

<!-- druid -->
       <dependency>
		   <groupId>com.alibaba</groupId>
		   <artifactId>druid-spring-boot-starter</artifactId>
		   <version>${druid.version}</version>
		</dependency>

yml配置

datasource:
    
    url: jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&useUnicode=true&useSSL=false&serverTimeZone=GMT  
    username: ****
    password: ****
     
    druid:
      initial-size: 5  # 初始化大小
      min-idle: 2  # 最小
      max-active: 10  # 最大
      max-wait: 60000  # 连接超时时间
      time-between-eviction-runs-millis: 60000  # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      min-evictable-idle-time-millis: 300000  # 指定一个空闲连接最少空闲多久后可被清除,单位是毫秒
      validationQuery: SELECT 'x'
      test-while-idle: true  # 当连接空闲时,是否执行连接测试
      test-on-borrow: false  # 当从连接池借用连接时,是否测试该连接
      test-on-return: false  # 在连接归还到连接池时是否测试该连接
      filters: config,wall,stat  # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
      poolPreparedStatements: true
      maxPoolPreparedStatementPerConnectionSize: 20
      maxOpenPreparedStatements: 20
      connectionProperties: druid.stat.slowSqlMillis=200;druid.stat.logSlowSql=true;config.decrypt=false
      web-stat-filter:
        enabled: true
        url-pattern: /*
        exclusions: /druid/*,*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        reset-enable: true
        login-username: admin
        login-password: ****
mybatis:
   mapper-locations: classpath:mybatis/**/*.xml   #resources包下的xml文件加载到mybatis中
    

编写实体类

例:

public class P1 implements Serializable{
	private static final long serialVersionUID = 1L;
	
	private List<P1Detail> p1DetailList ;
    private long id;

编写mapper

例:添加@mapper注解

import java.util.List;
@mapper
public interface P1Mapper {

	public List<P1> selectListById(String pID);
	
}

编写service

例:

import java.util.List;

public interface P1Service {

	public List<P1> selectListById(String pID);
	
}

编写serviceImpl

例:添加@service注解

@service
public class P1ServiceImpl implements P1Service {


	@Autowired
	private P1Mapper p1Mapper;

	@Override
	public List<P1> selectListById(String pID) {
		return p1Mapper.selectListById(pID);
	}	
}

编写xml
namespace写mapper路径
type为实体类的路径

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="net.perfectcn.ocrbf.mapper.P1Mapper">
  <resultMap id="BaseResultMap" type="net.perfectcn.ocrbf.domain.P1">

编写controller

例:

@Controller
@RequestMapping("/product1")
public class Product1Controller {
	@Autowired
	private P1Service p1Service;
	
	@GetMapping("/list")
    @ResponseBody
    public List<P1> selectListById(String pID){
        return  p1Service.selectListById(pID);
    }

}

结果显示json数据

例:

[{"id":"1f1e8ebd303345e298a75786da2201eb","createName":"test","createNameNo":"test"}]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值