zoukankan      html  css  js  c++  java
  • SpringBoot数据访问之整合mybatis注解版

    use  vuesite;
    CREATE TABLE city
    (
        id      INT PRIMARY KEY auto_increment,
        name    VARCHAR(255),
        state   VARCHAR(255),
        country VARCHAR(255)
    );

    创建实体类

    package com.xbhog.pojo;
    
    import lombok.Data;
    
    @Data
    public class City {
        private Long id;
        private String name;
        private String state;
        private String country;
    }

    创建Mapper:

    创建CityMapper并采用注解的方式实现sql映射的问题:

    package com.xbhog.Mapper;
    
    import com.xbhog.pojo.City;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Select;
    
    @Mapper
    public interface CityMapper {
        @Select("select * from user where id = #{id}")
        public City getCityId(Long id);
    }

    创建Service:

    package com.xbhog.service;
    
    import com.xbhog.Mapper.CityMapper;
    import com.xbhog.pojo.City;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class CityService {
        @Autowired
        CityMapper cityMapper;
    
        public City getCityId(Long id){
            return cityMapper.getCityId(id);
        }
    }

    使用Service注解声明,并将该类加入到容器中,方便后面调用,在service层调用Mapper层的方法。

    创建Controller:

    import com.xbhog.pojo.City;
    import com.xbhog.service.CityService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import javax.annotation.Resource;
    
    @Controller
    public class Mycontro {
        @Autowired
        CityService cityService;
    
    
        @ResponseBody
        @GetMapping("/city")
        public City getCity(@RequestParam("id") Long id){
            return cityService.getCityId(id);
        }
    }

    增加数据库信息

    mybatis混合版:

    我们在CItyMapper中添加一个方法:

    package com.xbhog.Mapper;
    
    import com.xbhog.pojo.City;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Select;
    
    import java.util.List;
    
    @Mapper
    public interface CityMapper {
        @Select("select * from city where id = #{id}")
        public City getCityId(Long id);
    
        public void addCity(City city);
    }

    https://www.cnblogs.com/xbhog/p/15201414.html

    故乡明
  • 相关阅读:
    redis的持久化机制和数据同步
    树莓派开机自动运行脚本或者程序
    树莓派安装Firefox+Selenium+geckodriver
    树莓派修改启动界面
    树莓派安装使用RXTX
    树莓派开启或关闭开启自动登陆
    树莓派USB存储设备自动挂载并通过脚本实现自动拷贝,自动播放视频,脚本自动升级等功能
    树莓派镜像使用帮助
    BlueZ
    (转)MQTT 入门介绍
  • 原文地址:https://www.cnblogs.com/luweiweicode/p/15205235.html
Copyright © 2011-2022 走看看