springboot整合mybatis 简易版

springboot整合mybatis

1:引入依赖

        <!-- mysql 驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>8.0.19</version>
        </dependency>
        <!-- alibaba的druid数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.9</version>
        </dependency>

2: 创建表

CREATE TABLE `sys_user`  (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id' ,
  `username` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT '用户名',
  `address` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT '地址',
  PRIMARY KEY (`id`)
);
INSERT INTO sys_user VALUES('1','张三','广东');
INSERT INTO sys_user VALUES('2','李四','广西');
INSERT INTO sys_user VALUES('3','王五','北京');

3:在配置文件中配置数据源

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    # 使用druid数据源
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    filters: stat
    maxActive: 20
    initialSize: 1
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20

4:Application上配置扫描mapper

@SpringBootApplication
@MapperScan(basePackages = "cloud.xingzhe.springbootMybatis.mapper")
public class SpringbootMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisApplication.class, args);
    }

}

5 mapper映射
1:默认情况下,springboot会到resource目录下与mapper.java相同的目录下找mapper.xml。如sysUserMapper.java在cloud.xingzhe.springbootMybatis.mapper 那么springboot会到 resource目录下的cloud.xingzhe.springbootMybatis.mapper找mapper.xml
2: 在配置文件中配置mapper的映射

#配置mapper.xml
mybatis:
  mapper-locations: classpath:/mapper/*.xml

6:web测试
1:创建web
springboot整合mybatis 简易版_第1张图片
SysUserController.java

@RestController
public class SysUserController {
    @Autowired
    private SysUserServer sysUserServer;
    @ResponseBody
    @RequestMapping("/findAllUser")
    public List<SysUser> findAllUser(){
        return sysUserServer.findAllUser();
    }
}

SysUserServer.java

 List<SysUser> findAllUser();

SysUserServiceImpl.java

 @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public List<SysUser> findAllUser() {
        return sysUserMapper.findAllUser();
    }

SysUserMapper.java

@Repository
public interface SysUserMapper {
   List<SysUser> findAllUser();
}

SysUserMapper.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="cloud.xingzhe.springbootMybatis.mapper.SysUserMapper">

    <select id="findAllUser" resultType="cloud.xingzhe.springbootMybatis.model.SysUser">
     select * from sys_user
    </select>
</mapper>

springboot整合mybatis 简易版_第2张图片

7:源码地址
https://github.com/xingzhewenzi/springboot-examples.git

你可能感兴趣的:(springboot,mysql,mybatis,mybatis,spring,boot,mysql)