栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合mybatis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合mybatis

org.mybatis.spring.boot

mybatis-spring-boot-starter

2.1.1

三、无配置注解版

============

1、添加maven文件


org.springframework.boot

spring-boot-starter-data-jdbc

org.springframework.boot

spring-boot-starter-web

org.mybatis.spring.boot

mybatis-spring-boot-starter

2.1.1

com.oracle.ojdbc

ojdbc8

runtime

com.alibaba

druid

1.1.21

log4j

log4j

1.2.17

2、application.yml添加相关配置


spring:

datasource:

username: mine

password: mine

url: jdbc:oracle:thin:@127.0.0.1:1521:orcl

driver-class-name: oracle.jdbc.driver.OracleDriver

type: com.alibaba.druid.pool.DruidDataSource

initialSize: 5

minIdle: 5

maxActive: 20

maxWait: 60000

timeBetweenEvictionRunsMillis: 60000

minEvictableIdleTimeMillis: 300000

validationQuery: SELECt 1 FROM DUAL

testWhileIdle: true

testOnBorrow: false

testOnReturn: false

poolPreparedStatements: true

filters: stat,wall,log4j

maxPoolPreparedStatementPerConnectionSize: 20

useGlobalDataSourceStat: true

connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

Spring Boot 会自动加载spring.datasource.*相关配置,数据源就会自动注入到 sqlSessionFactory 中,sqlSessionFactory 会自动注入到 Mapper 中,对了,你一切都不用管了,直接拿起来使用就行了。

在启动类中添加对 mapper 包扫描@MapperScan

@MapperScan(value=“com.demo.mapper”)

@SpringBootApplication

public class DemoApplication {

public static void main(String[] args) {

SpringApplication.run(DemoApplication.class, args);

}

}

或者直接在 Mapper 类上面添加注解@Mapper,建议使用上面那种,不然每个 mapper 加个注解也挺麻烦的

3、controller


@RestController

public class DeptController {

@Autowired

DepartmentMapper departmentMapper;

@GetMapping("/dept/{id}")

public Department getDepartment(@PathVariable(“id”) Integer id){

return departmentMapper.getDeptById(id);

}

@GetMapping("/dept")

public int insertDeptById(@PathVariable(“id”) Integer id,@PathVariable(“departmentName”) String departmentName){

int ret = departmentMapper.insertDept(id,departmentName);

return ret;

}

}

4、开发mapper


package com.demo.mapper;

import com.demo.bean.Department;

import org.apache.ibatis.annotations.*;

public interface DepartmentMappe

【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】

浏览器打开:qq.cn.hn/FTf 开源分享

r {

@Select(“select * from SSH_DEPARTMENT where id=#{id}”)

public Department getDeptById(Integer id);

@Delete(“delete from SSH_DEPARTMENT where id=#{id}”)

public int deleteDeptById(Integer id);

@Options(useGeneratedKeys = true,keyProperty = “id”)

@Insert(“insert into SSH_DEPARTMENT(department_name) values(#{departmentName})”)

public int insertDept(Department department);

@Update(“update SSH_DEPARTMENT set departmentName=#{DEPARTMENT_NAME} where id=#{id}”)

public int updateDept(Department department);

}

  • @Select 是查询类的注解,所有的查询均使用这个

  • @Result 修饰返回的结果集,关联实体类属性和数据库字段一一对应,如果实体类属性和数据库属性名保持一致,就不需要这个属性来修饰。

  • @Insert 插入数据库使用,直接传入实体类会自动解析属性到对应的值

  • @Update 负责修改,也可以直接传入对象

  • @delete 负责删除

5、运行


上面三步就基本完成了相关 Mapper 层开发,使用的时候当作普通的类注入进入就可以了

(1)查询

(2)插入
  • 插入前数据库状态

  • 浏览器调用controller执行插入

  • 插入后结果查询

四、极简XML版本

=============

极简 xml 版本保持映射文件的老传统,接口层只需要定义空方法,系统会自动根据方法名在映射文件中找对应的 Sql

1、配置


pom 文件和上个版本一样,只是application.yml新增以下配置

mybatis.config-location=classpath:mybatis/mybatis-config.xml

mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

mybatis-config.xml 配置

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/572935.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号