19-springboot整合mybaits

本文详细介绍了如何在SpringBoot项目中整合MyBatis,包括配置Druid数据源、MyBatis的基本使用、MyBatis-Generator生成代码、解决@Repository注解报错问题以及PageHelper分页插件的集成。此外,还涵盖了日志配置的相关步骤。
摘要由CSDN通过智能技术生成

新建springboot项目

druid学习地址

https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter

 

1.引入依赖

 <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid-spring-boot-starter</artifactId>
     <version>1.1.10</version>
  </dependency>

application.yml和application.properties区别?
  yml文件的好处,天然的树状结构,一目了然,实质上跟properties是差不多的。

2.application.yml配置druid

server:
  servlet:
    context-path: /springboot

spring:
  datasource:
    #1.JDBC
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
    username: root
    password: 123
    druid:
      #2.连接池配置
      #初始化连接池的连接数量 大小,最小,最大
      initial-size: 5
      min-idle: 5
      max-active: 20
      #配置获取连接等待超时的时间
      max-wait: 60000
      #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      time-between-eviction-runs-millis: 60000
      # 配置一个连接在池中最小生存的时间,单位是毫秒
      min-evictable-idle-time-millis: 30000
      validation-query: SELECT 1 FROM DUAL
      test-while-idle: true
      test-on-borrow: true
      test-on-return: false
      # 是否缓存preparedStatement,也就是PSCache  官方建议MySQL下建议关闭   个人建议如果想用SQL防火墙 建议打开
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
      filter:
        stat:
          merge-sql: true
          slow-sql-millis: 5000
      #3.基础监控配置
      web-stat-filter:
        enabled: true
        url-pattern: /*
        #设置不统计哪些URL
        exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
        session-stat-enable: true
        session-stat-max-count: 100
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        reset-enable: true
        #设置监控页面的登录名和密码
        login-username: admin
        login-password: admin
        allow: 127.0.0.1
        #deny: 192.168.1.100

 

 启动SpringBoot项目访问druid
  
  http://localhost:tomcat端口号/项目名称/druid/

配置mybatis

1.导入依赖

<dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
     <version>1.3.2</version>
  </dependency>

 

MyBatis-Spring-Boot-Starter依赖将会提供如下:
  1) 自动检测现有的DataSource。
  2) 将创建并注册SqlSessionFactory的实例,该实例使用SqlSessionFactoryBean将该DataSource作为输入进行传递。
  3) 将创建并注册从SqlSessionFactory中获取的SqlSessionTemplate的实例。
  4) 自动扫描您的mappers,将它们链接到SqlSessionTemplate并将其注册到Spring上下文,以便将它们注入到您的bean中。

  就是说,使用了该Starter之后,只需要定义一个DataSource即可(application.properties或application.yml中可配置),它会自动创建使用该DataSource的SqlSessionFactoryBean以及SqlSessionTemplate。会自动扫描你的Mappers,连接到SqlSessionTemplate,

2.使用Mybatis-Generator插件生成代码

2.1 导入并修改generatorConfig.xml和jdbc.properties(resources下)

generatorConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 是一个快速开发框架,MyBatis 是一款优秀的基于 Java 的持久层框架。在 Spring Boot 中使用 MyBatis 可以极大地提高开发效率和运行效率。 具体步骤如下: 1. 引入 MyBatis 和 MyBatis-SpringBoot-Starter 依赖。 2. 配置数据源和 MyBatis。 3. 编写实体类和映射文件。 4. 编写 DAO 层接口和 SQL 语句。 5. 在 Service 层中调用 DAO 层接口。 6. 在 Controller 层中调用 Service 层方法,返回结果给前端。 示例代码: 1. 引入依赖: ``` <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> ``` 2. 配置数据源和 MyBatis: ``` spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=root mybatis.mapper-locations=classpath:mapper/*.xml ``` 3. 编写实体类和映射文件: 实体类: ``` public class User { private Long id; private String name; private Integer age; // 省略 getter 和 setter 方法 } ``` 映射文件: ``` <?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="com.example.demo.dao.UserMapper"> <resultMap id="BaseResultMap" type="com.example.demo.entity.User"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="age" property="age" /> </resultMap> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long"> select * from user where id = #{id} </select> </mapper> ``` 4. 编写 DAO 层接口和 SQL 语句: ``` public interface UserMapper { User selectByPrimaryKey(Long id); } ``` 5. 在 Service 层中调用 DAO 层接口: ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getUserById(Long id) { return userMapper.selectByPrimaryKey(id); } } ``` 6. 在 Controller 层中调用 Service 层方法,返回结果给前端: ``` @RestController public class UserController { @Autowired private UserService userService; @GetMapping(value = "/user/{id}") public User getUserById(@PathVariable Long id) { return userService.getUserById(id); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值