• mybatis学习8-CRUD注解方式


    前期mysql准备

    create database cong
    use cong;
    create table account(
        id int primary key auto_increment,
        name varchar(40),
        money float
    )character set utf8 collate utf8_general_ci;
    
    insert into account(name,money) values('aaa',1000);
    insert into account(name,money) values('bbb',1000);
    insert into account(name,money) values('ccc',1000);

    2.实体类

    package com.cong.pojo;
    
    public class Account {
        private int id;
        private String name;
        private float money;
       ...
    }

    查找

        @Select("select * from account")
        List<Account> findAll();//查询所有
    
        @Select("select * from account where id = #{id}")
        Account findById(int id);//根据id查询
    
        @Select("select * from account where name like #{name}")
        //@Select("select * from account where name like '%${value}%'")
        List<Account> findByName(String name);//根据名字模糊查询
    
        @Select("select count(*) from account")
        int findTotal();//查询数量

    插入

        @Insert("insert into account(name,money) values(#{name},#{money})")
        int saveAccount(Account account);//返回值是数据库的影响条目

    修改

        @Update("update account set name=#{name},money=#{money} where id = #{id}")
        void updateAccount(Account account);//修改
    
        //修改单个或少量属性,没必要传递一个对象,直接传递属性,通过注解标注
        @Update("update account set name=#{n} where id = #{i}")
        void updateAccountByParam(@Param("n")String name,@Param("i")int id);

    删除

        @Delete("delete from account where id = #{id}")
        void deleteAccount(int id);

    爱生活,爱码字

    我是匆匆、我曾喂自己半年酒。

    好好生活吧,有缘或许相见。

  • 相关阅读:
    log4j基本使用方法
    Spring MVC中页面向后台传值的几种方式
    JXL操作Excel
    模板
    url&视图
    Git for PyCharm
    ServletConfig和ServletContext
    Exception和IOException之间的使用区别
    java学习一目了然——异常必知
    java学习一目了然——IO
  • 原文地址:https://www.cnblogs.com/ccoonngg/p/11306637.html
Copyright © 2020-2023  润新知