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);
爱生活,爱码字
我是匆匆、我曾喂自己半年酒。
好好生活吧,有缘或许相见。