// 使用注解实现CRUD // 查询 参数需要@Param("value") 以这个为主 @Select("select * from user where id=#{id}") User getUserById(@Param("id")int id, @Param("name") String name); // 添加 引用对象不需要@Param 这里写实体类里的 @Insert("insert into user(id,name,pwd) values(#{id},#{name},#{pwd})") intaddUser(User user); // 修改 @Update("update user set name=#{name},pwd=#{pwd} where id=#{id}") intupdateUser(User user); // 删除 @Delete("delete from user where id=#{id}") intdeleteUser(@Param("id")int id);
===================ID查询===================== Opening JDBC Connection Created connection 592617454. ==> Preparing: select * from user where id=? ==> Parameters: 1(Integer) <== Columns: id, name, pwd <== Row: 1, admin, 123 <== Total: 1 User{id=1, name='admin', pwd='123'} Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@23529fee] Returned connection 592617454 to pool.
Process finished with exit code 0
===================添加===================== Opening JDBC Connection Created connection 487075464. ==> Preparing: insert into user(id,name,pwd) values(?,?,?) ==> Parameters: 5(Integer), 小呆呆(String), 123123(String) <== Updates: 1 Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@1d082e88] Returned connection 487075464 to pool.
Process finished with exit code 0 ===================修改===================== Opening JDBC Connection Created connection 6320204. ==> Preparing: update user set name=?,pwd=? where id=? ==> Parameters: 小呆瓜(String), 33333(String), 5(Integer) <== Updates: 1 Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@60704c] Returned connection 6320204 to pool.
Process finished with exit code 0 ===================删除===================== Opening JDBC Connection Created connection 112302969. ==> Preparing: delete from user where id=? ==> Parameters: 6(Integer) <== Updates: 1 Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@6b19b79] Returned connection 112302969 to pool.
Process finished with exit code 0
Lombok的使用
官方简介
Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.
Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.