SpringBoot2.5.6集成mybatis
1.应用依赖
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.8</version> </dependency>
2.application.yml配置
spring: datasource: username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Shanghai #type: com.alibaba.druid.pool.DruidDataSource type: com.alibaba.druid.pool.DruidDataSource druid: # 链接池初始化大小 initial-size: 8 # 最大活跃数 max-active: 16 # 最小空闲数 min-idle: 1 # 最大等待时间 max-wait: 60000 #指定MyBatis映射文件的路径 mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.pab.data.datasource.entity
3.mapper 类
@Mapper public interface UserMapper { public List<User> queryUserList(); }
4.mapper.xml
<?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.pab.data.datasource.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.pab.data.datasource.entity.User"> <!-- column 数据库中的字段名称 property 映射对象的属性名称 jdbcType 列在数据库中字段的类型(可以省略不写) --> <id column="id" jdbcType="INTEGER" property="id" /> <result column="username" jdbcType="VARCHAR" property="username" /> <result column="password" jdbcType="VARCHAR" property="password" /> <result column="name" jdbcType="VARCHAR" property="name" /> </resultMap> <select id="queryUserList" resultMap="BaseResultMap"> select * from user </select> </mapper>