• 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")

  • 相关阅读:
    PHP使用Redis的GEO地理信息类型
    Redis长短链接的区别
    Linux之ln文件创建链接
    xml与json格式互转
    爬虫实例:唐诗宋词爬虫
    爬虫实例:天猫商品评论爬虫
    爬虫实例:饿了么爬虫
    爬虫实例:中国日报高频词汇爬虫
    爬虫实例:今日头条爬虫
    特殊类型的列表切片
  • 原文地址:https://www.cnblogs.com/ljkltt/p/14225200.html
Copyright © 2020-2023  润新知