• SpringBoot整合MyBatis注解版


    1.pom配置

    <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.4</version>
    </dependency>

    2.创建实体类

    package com.example.demo.entity;

    public class User {
      private Integer id;
      private String name;
      private String email;

      public Integer getId() {
        return id;
      }

      public void setId(Integer id) {
        this.id = id;
      }

      public String getName() {
        return name;
      }

      public void setName(String name) {
        this.name = name;
      }

      public String getEmail() {
        return email;
      }

      public void setEmail(String email) {
        this.email = email;
      }
    }

    3.创建mapper层(写SQL)

    package com.example.demo.mapper;

    import com.example.demo.entity.User;
    import org.apache.ibatis.annotations.*;
    import org.springframework.stereotype.Repository;

    import java.util.List;

    @Repository
    @Mapper
    public interface UserMapper {

      @Select("select * from user")
      public List<User> getUserList();

      @Select("select * from user where id = #{id}")
      public User getUserById(Integer id);

      //新增返回id值
      @Options(useGeneratedKeys = true,keyProperty = "id")
      @Insert("insert into user(name,email) values(#{name},#{email})")
      public int insertUser(User user);

      @Update("update user set name=#{name},email=#{email} where id = #{id}")
      public int updateUser(User user);

      @Delete("delete from user where id = #{id}")
      public int deleteUserById(Integer id);
    }

    4.创建Controller层(接收请求和发送数据,没有写service)

    @Autowired
    UserMapper userMapper;

    /**
    * 获取所有数据
    * @return
    */
    @GetMapping("/user")
    public List<User> getUserList(){
    return userMapper.getUserList();
    }

    /**
    * 查询一组数据
    * @param id integer
    * @return json
    */
    @GetMapping("/user/{id}")
    public User getUserById(@PathVariable("id") Integer id){
    return userMapper.getUserById(id);
    }

    /**
    * 新增数据
    * @param user object
    * @return json
    */
    @GetMapping("/add")
    public User insertUser(User user){
    userMapper.insertUser(user);
    return user;
    }

    /**
    * 修改数据
    * @param user object
    * @return json
    */
    @GetMapping("/update")
    public User updateUser(User user){
    userMapper.updateUser(user);
    return user;
    }

    /**
    * 删除一组数据
    * @param id integer
    * @return boolean
    */
    @GetMapping("/del/{id}")
    public Boolean deleteUser(@PathVariable("id") Integer id){
    try {
    userMapper.deleteUserById(id);
    }catch (Exception e){
    e.printStackTrace();
    return false;
    }
    return true;
    }

    5.自定义MyBatis的配置规则

    package com.example.demo.config;

    import org.apache.ibatis.session.Configuration;
    import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
    import org.springframework.context.annotation.Bean;

    //自定义驼峰命名规则
    @org.springframework.context.annotation.Configuration
    public class MyBatisConfig {
      //给容器中添加ConfigurationCustomizer组件
      @Bean
      public ConfigurationCustomizer configurationCustomizer(){
        return new ConfigurationCustomizer() {
          @Override
          public void customize(Configuration configuration) {
            configuration.setMapUnderscoreToCamelCase(true);
          }
        };
      }
    }

    6.MapperScan

    //当Mapper过多的时候,为了方便,给主程序添加MapperScan,作用就是会自动扫描并且给指定路径下的所有文件自动添加Mapper注解
    @MapperScan(value = "com.example.demo.mapper")

  • 相关阅读:
    CVE-2017-10271
    [GKCTF2020]cve版签到
    [GXYCTF2019]禁止套娃 无参数RCE
    [护网杯 2018]easy_tornado
    记两道xctf上的web进阶区 反序列化
    msf卸载win defender
    Cron表达式详解
    Linux ifconfig只有lo没有别的网络的问题
    记一道文件上传
    【解决】手机安卓已经导入burp证书,但仍提示此证书并非来自被信任的机构
  • 原文地址:https://www.cnblogs.com/ljkltt/p/14225200.html
Copyright © 2020-2023  润新知