zoukankan      html  css  js  c++  java
  • SpringBoot整合Mybatis

    IDEA 2019.2

    JDK 8

    一、项目结构

     二、依赖 POM

    三、项目启动

    App.java

    package com.dxj;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    @SpringBootApplication
    public class App {
        public static void main(String[] args) {
            SpringApplication.run(App.class);
        }
    }

    四、创建项目代码

     

     例如:entity->China.java

    package com.dxj.entity;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import org.apache.ibatis.type.Alias;
    @Alias("China")
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class China {
        private Integer id;
        private  String name;
        private  Integer pid;
    }

      mapper-->IChinaMapper.java

    package com.dxj.mapper;
    import com.dxj.entity.China;
    import org.apache.ibatis.annotations.Mapper;
    import java.util.List;
    @Mapper
    public interface IChinaMapper {
        List<China> findAllProvince();
    }

    service-->IChinaService.java

    package com.dxj.service;
    import com.dxj.entity.China;
    import java.util.List;
    public interface IChinaService {
        List<China> findAllProvince();
    }

    service-->impl-->IChinaServiceImpl.java

    import com.dxj.entity.China;
    import com.dxj.mapper.IChinaMapper;
    import com.dxj.service.IChinaService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    import java.util.List;
    @Service("ics")
    @Transactional
    public class IChinaServiceImpl implements IChinaService {
    
        @Autowired
        private IChinaMapper icm;
    
        @Override
        public List<China> findAllProvince() {
            return icm.findAllProvince();
        }
    }

    controllers-->ChinaController.java

    package com.dxj.controllers;
    
    import com.dxj.entity.China;
    import com.dxj.service.IChinaService;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import javax.annotation.Resource;
    import java.util.List;
    
    @RestController
    public class ChinaController {
    
        @Resource
        private IChinaService ics;
    
        @RequestMapping("/findAllProvince.action")
        public List<China> findAllProvince(){
            return  ics.findAllProvince();
        }
    }

    五、配置文件

     1.application.yml

    server:
      port: 8099
    spring:
      datasource:
        druid:
          url: jdbc:mysql://localhost:3306/hospital?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
          username: root
          password: 1234
          driver-class-name: com.mysql.jdbc.Driver
    mybatis:
      mapper-locations: mapper/*.mapper.xml
      type-aliases-package: com.dxj.entity

    2.xx.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.dxj.mapper.IChinaMapper">
        <resultMap id="ChinaMap" type="China">
            <id column="china_id"  property="id" />
            <result column="china_name"  property="name" />
            <result column="china_pid"  property="pid" />
        </resultMap>
    
        <!-- 全查询 -->
        <select id="findAllProvince" resultMap="ChinaMap">
            SELECT * FROM china_tab WHERE china_pid=0;
        </select>
        
    </mapper>
  • 相关阅读:
    Django_jinja2
    css画太极
    python 自己实现map
    python 比赛 组合问题
    python 找素数
    如何快速掌握一门新技术/语言/框架
    jQuery常用事件-思维导图
    jQuery常用函数-思维导图
    jQuery选择器汇总-思维导图
    3.git版本控制-管理修改、撤销、删除
  • 原文地址:https://www.cnblogs.com/dxjx/p/12462007.html
Copyright © 2011-2022 走看看