• SpringBoot之通用Mapper


    1. 首先需要导入依赖

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

    2. application.properties 配置文件

    # 应用名称
    spring.application.name=springboot06
    # 应用服务 WEB 访问端口
    server.port=8080
    #指定Mybatis的实体目录
    mybatis.type-aliases-package=com.springboot06.springboot06.mybatis.entity
    # 数据库驱动:8.0以上版本需加cj
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    # 数据源名称
    spring.datasource.name=defaultDataSource
    # 数据库连接地址
    spring.datasource.url=jdbc:mysql://localhost:3306/shiroTest?serverTimezone=UTC
    # 数据库用户名&密码:
    spring.datasource.username=root
    spring.datasource.password=123456
    

    3. 启动类(@MapperScan 一定要使用tk的mapper)

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import tk.mybatis.spring.annotation.MapperScan;
    
    @SpringBootApplication
    @MapperScan("com.springboot06.mapper")
    public class Springboot06Application {
    
        public static void main(String[] args)
        {
            //run方法,表示运行SpringBoot的引导类,参数就是SpringBoot的引导类字节码对象
            SpringApplication.run(Springboot06Application.class, args);
        }
    
    }
    

    4. 实体类

    • @Table注解: 声明此对象映射到数据库的数据表,通过它可以为实体指定表(talbe)
    • @column注解:标识实体类中属性与数据表中字段的对应关系
    • @Transient注解:在实体类(pojo)属性上使用、表示数据库表中没有这个字段就忽略
    import tk.mybatis.mapper.annotation.KeySql;
    
    import javax.persistence.Id;
    import javax.persistence.Table;
    import java.io.Serializable;
    
    /**
     * 实体类users
     */
    //告知通用Mapper要对哪张表进行操作
    @Table(name = "users")
    public class Users implements Serializable {
    
        /**
         * 告知通用Mapper此属性对应表中的主键
         */
        @Id
        @KeySql(useGeneratedKeys = true)
        private Integer id;
    
        @column(name = "`username`")
        private String username;
    
        private String password;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username == null ? null : username.trim();
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password == null ? null : password.trim();
        }
    }
    

    5. mapper

    Mapper只需要继承tk.mybatis.mapper.common.Mapper这个接口,通用Mapper就会自动帮我们生成Mybatis的单表的增删改查。

    import com.springboot06.pojo.Users;
    import tk.mybatis.mapper.common.Mapper;
    
    
    public interface UsersMapper extends Mapper<Users> {
    
    }
    

    6. Service

    import com.springboot06.pojo.Users;
    
    import java.util.List;
    
    public interface UsersService {
    
        /**
         * 根据ID查询
         * @param id
         * @return
         */
        Users selectByPrimaryKey(Integer id);
    
        /**
         * 查询全部
         * @return
         */
        List<Users> selectAll();
    
        /**
         * 新增
         * @param users
         */
        void insert(Users users);
    
        /**
         * 删除
         * @param id
         */
        void deleteByPrimaryKey(Integer id);
    
        /**
         * 更新
         * @param users
         */
        void updateByPrimaryKey(Users users);
    }
    
    

    7.Controller

    import com.springboot06.pojo.Users;
    import com.springboot06.service.UsersService;
    import com.springboot06.utils.ResponseResult;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.List;
    
    /**
     * Created by papercy on  2021/6/16.
     */
    @RestController
    @RequestMapping("users")
    public class UsersController {
    
        @Autowired
        private UsersService usersService;
    
        @GetMapping("{id}")
        public ResponseResult selectByPrimaryKey(@PathVariable Integer id)
        {
            String message="";
            try {
                Users user = usersService.selectByPrimaryKey(id);
                return new ResponseResult(true,message="查询成功",user);
            }catch (Exception e) {
                e.printStackTrace();
                return new ResponseResult(false,message="查询失败");
            }
    
        }
    
        @GetMapping
        public ResponseResult selectAll()
        {
            String message="";
            try {
                List<Users> usersList = usersService.selectAll();
                return new ResponseResult(true,message="查询成功",usersList);
            }catch (Exception e) {
                e.printStackTrace();
                return new ResponseResult(false, message="查询失败");
            }
        }
    
        @PostMapping
        public ResponseResult insert(@RequestBody Users users)
        {
            String message="";
            try {
                usersService.insert(users);
                return new ResponseResult(true,message="新增成功");
            }catch (Exception e) {
                e.printStackTrace();
                return new ResponseResult(false, message="新增失败");
            }
        }
    
        @DeleteMapping
        public ResponseResult deleteByPrimaryKey(Integer id)
        {
            String message="";
            try {
                usersService.deleteByPrimaryKey(id);
                return new ResponseResult(true,message="删除成功");
            }catch (Exception e) {
                e.printStackTrace();
                return new ResponseResult(false, message="删除失败");
            }
        }
    
        @PutMapping
        public  ResponseResult updateByPrimaryKey(@RequestBody Users users)
        {
            String message="";
            try {
                usersService.updateByPrimaryKey(users);
                return new ResponseResult(true,message="更新成功");
            }catch (Exception e) {
                e.printStackTrace();
                return new ResponseResult(false, message="更新失败");
            }
        }
    
    
    }
    
    

    8.最后来一张项目结构图

    image

  • 相关阅读:
    QT槽和信号机制介绍
    J2EE面试题之二
    .net 面试题系列文章一(附答案)
    SUN JAVA面试笔试题
    用友在清华的笔试题
    Java面试笔试题整理
    雅虎公司C#笔试题(之一)
    Oracle面试题及答案整理
    一些著名的大公司JAVA面试题目
    百度技术研发笔试题目1
  • 原文地址:https://www.cnblogs.com/papercy/p/Springboot.html
Copyright © 2020-2023  润新知