springboot整合mybatis

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:
  1. 添加mybatis pom.xml依赖

    <dependency>
       <groupId>org.mybatis.spring.boot</groupId>
       <artifactId>mybatis-spring-boot-starter</artifactId>
       <version>1.3.0</version>
    </dependency>
    <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <scope>runtime</scope>
    </dependency>
  2. 添加application.properties mysql配置

    spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8
    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  3. mysql test库中创建account表,springboot添加account model类

    @Component
    public class Account {
        private int id ;
        private String name ;
        private double money;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public double getMoney() {
            return money;
        }
    
        public void setMoney(double money) {
            this.money = money;
        }
    }
  4. 创建DAO层接口

    import com.example.demo.domain.Account;
    import org.apache.ibatis.annotations.*;
    import org.mapstruct.Mapper;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Repository;
    
    
    import java.util.List;
    
    /**
     * @Author 冯战魁
     * @Date 2018/1/23 上午9:59
     */
    @Mapper
    @Component(value = "accountMapper")
    public interface AccountMapper {
        @Insert("insert into account(name, money) values(#{name}, #{money})")
        int add(@Param("name") String name, @Param("money") double money);
    
        @Update("update account set name = #{name}, money = #{money} where id = #{id}")
        int update(@Param("name") String name, @Param("money") double money, @Param("id") int  id);
    
        @Delete("delete from account where id = #{id}")
        int delete(int id);
    
        @Select("select id, name as name, money as money from account where id = #{id}")
        Account findAccount(@Param("id") int id);
    
        @Select("select id, name as name, money as money from account")
        List<Account> findAccountList();
    }
  5. 入口文件加入@MapperScan扫描接口的路径

    @SpringBootApplication
    @MapperScan("com.example.demo.repository")
    public class DemoApplication {
    
       public static void main(String[] args) {
          SpringApplication.run(DemoApplication.class, args);
       }
    }
  6. 添加操作接口

    import com.example.demo.repository.AccountMapper;
    import com.example.demo.domain.Account;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.List;
    
    /**
     * @Author 冯战魁
     * @Date 2018/1/23 上午10:12
     */
    @RestController
    @RequestMapping("/account")
    public class AccountController {
        @Autowired
        AccountMapper accountMapper;
        @RequestMapping("list")
        public List<Account> getAccounts() {
            return accountMapper.findAccountList();
        }
        @RequestMapping(value = "/{id}", method = RequestMethod.GET)
        public Account getAccountById(@PathVariable("id") int id) {
            return accountMapper.findAccount(id);
        }
        @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
        public String updateAccount(@PathVariable("id") int id, @RequestParam(value = "name", required = true) String name,
                                    @RequestParam(value = "money", required = true) double money) {
            int t= accountMapper.update(name,money,id);
            if(t==1) {
                return "success";
            }else {
                return "fail";
            }
        }
        @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
        public String delete(@PathVariable(value = "id")int id) {
            int t= accountMapper.delete(id);
            if(t==1) {
                return "success";
            }else {
                return "fail";
            }
        }
        @RequestMapping(value = "", method = RequestMethod.POST)
        public String postAccount(@RequestParam(value = "name") String name,
                                  @RequestParam(value = "money") double money) {
            int t= accountMapper.add(name,money);
            if(t==1) {
                return "success";
            }else {
                return "fail";
            }
        }
    }









本文转自 无心低语 51CTO博客,原文链接:http://blog.51cto.com/fengzhankui/2064206,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
30天前
|
前端开发 Java 关系型数据库
SpringBoot+MyBatis 天猫商城项目
SpringBoot+MyBatis 天猫商城项目
51 1
|
30天前
ssm(Spring+Spring mvc+mybatis)——updateDept.jsp
ssm(Spring+Spring mvc+mybatis)——updateDept.jsp
10 0
|
30天前
ssm(Spring+Spring mvc+mybatis)——showDept.jsp
ssm(Spring+Spring mvc+mybatis)——showDept.jsp
9 0
|
29天前
|
SQL Java 数据库连接
springboot中配置mybatis别名该怎么写?
springboot中配置mybatis别名该怎么写?
22 0
|
1月前
|
XML Java 关系型数据库
【SpringBoot系列】SpringBoot集成Fast Mybatis
【SpringBoot系列】SpringBoot集成Fast Mybatis
|
29天前
|
SQL JavaScript Java
springboot+springm vc+mybatis实现增删改查案例!
springboot+springm vc+mybatis实现增删改查案例!
23 0
|
29天前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
35 1
|
1月前
|
druid Java 数据库连接
Spring Boot3整合MyBatis Plus
Spring Boot3整合MyBatis Plus
39 1
|
10天前
|
SQL Java 数据库连接
【mybatis】第一篇,Springboot中使用插件PageHelper不生效解决方案
【mybatis】第一篇,Springboot中使用插件PageHelper不生效解决方案
|
21天前
|
JavaScript Java 关系型数据库
SpringBoot + Mybatis + Vue的代码生成器
SpringBoot + Mybatis + Vue的代码生成器
32 2