SpringBoot集成Mybatis

1.创建一个springboot项目,点击File->New->Project->Spring Initializr->勾选Spring Web

-- ----------------------------
-- Table structure for hr_user
-- ----------------------------
DROP TABLE IF EXISTS `hr_user`;
CREATE TABLE `hr_user`  (
  `id` bigint(20) NOT NULL,
  `age` tinyint(3) NULL DEFAULT NULL COMMENT '年龄',
  `name` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '名字',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of hr_user
-- ----------------------------
INSERT INTO `hr_user` VALUES (1, 20, 'wg1');
INSERT INTO `hr_user` VALUES (2, 20, 'wg2');
INSERT INTO `hr_user` VALUES (3, 20, 'wg3');

2.引入相关依赖包

        
        
            mysql
            mysql-connector-java
        
        
            com.alibaba
            druid
            1.1.17
            compile
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

        
        
            org.projectlombok
            lombok
            1.18.4
        

3.配置application.yml

server:
  port: 7000
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/facebook_hr?characterEncoding=utf8&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=UTC
    username: root
    password: 123456
    initialSize: 2
    minIdle: 4
    maxActive: 20
    # 配置获取连接等待超时的时间
    maxWait: 60000
    # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    timeBetweenEvictionRunsMillis: 60000
    # 配置一个连接在池中最小生存的时间,单位是毫秒
    minEvictableIdleTimeMillis: 30000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    # 打开PSCache,并且指定每个连接上PSCache的大小
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    filters: stat,wall,slf4j
    # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

## 该配置节点为独立的节点,有很多同学容易将这个配置放在spring的节点下,导致配置无法被识别
mybatis:
  mapper-locations: classpath:mapper/*.xml #注意:一定要对应mapper映射xml文件的所在路径
  typeAliasesPackage: com.weigang.pojo # 注意:对应实体类的路径

4.Java代码

1.实体类HrUserEntity
    @Data
    @SuppressWarnings("all")
    public class HrUserEntity {

        //主键id
        private Integer id;
        //年龄
        private Integer age;
        //名字
        private String name;

    }

2.dao(接口)

    @Repository
    @Mapper
    public interface HrUserMapper{

        //获取用户列表
        @SuppressWarnings("all")
        List selectAllUser();

    }


3.service
    @Service
    public class HrUserService {

        @Autowired
        private HrUserMapper hrUserMapper;


        /**
         * 获取用户列表
         * @param
         * @return java.util.List
         */
        public List getUsers(){
            List users = hrUserMapper.selectAllUser();
            return users;
        }

    }

4.controller
    @RestController
    @RequestMapping("/user")
    public class HrUserController {

        @Autowired
        private HrUserService hrUserService;

        @GetMapping(value = "/getUsers")
        public List getUsers(){
            return hrUserService.getUsers();//http://localhost:7000/user/getUsers
        }

    }

5.项目结构

SpringBoot集成Mybatis_第1张图片

6.执行效果

SpringBoot集成Mybatis_第2张图片

你可能感兴趣的:(SpringBoot)