mybatis注解开发
主要是下面4个注解
@Select @Insert @Update @Delete
001-@Select
@Select("select * from user") List<User> findAll();
002-@Insert
@Insert("insert into user(username,address,sex,birthday)values(#{username},#{address},#{sex},#{birthday})") void saveUser(User user);
003-@Update
@Update("update user set username=#{username},sex=#{sex},birthday=#{birthday},address=#{address} where id=#{id}") void updateUser(User user);
004-@Delete
@Delete("delete from user where id=#{id} ") void deleteUser(Integer userId);
005-查询一条用户
@Select("select * from user where id=#{id} ") User findById(Integer userId);
006-模糊查询
//@Select("select * from user where username like #{username} ") @Select("select * from user where username like '%${value}%' ") List<User> findUserByName(String username);
007-查询数目
@Select("select count(*) from user ") int findTotalUser();
008-解决数据库字段和熟悉不匹配问题
@Data @ToString public class User1 { private Integer id1; private String username1; private Date birthday1; private String sex1; private String address1; } @Select("select * from user") @Results(value={ @Result(id=true,column = "id",property = "id1"), @Result(column = "username",property = "username1"), @Result(column = "address",property = "address1"), @Result(column = "sex",property = "sex1"), @Result(column = "birthday",property = "birthday1") }) List<User1> findAll();
009-解决其它方法也能用上面的注解问题
在results后面加上id 然后其它方法也引用这个id @Results(id="userMap",value={ 引用 @Select("select * from user where id=#{id} ") @ResultMap("userMap") User1 findById(Integer userId);
010-mybatis注解开发 一对多注解
A一个账户一个用户
@Select("select * from account") @Results(id="accountMap",value = { @Result(id=true,column = "id",property = "id"), @Result(column = "uid",property = "uid"), @Result(column = "money",property = "money"), @Result(property = "user1",column = "uid",one=@One(select="com.po.pf.repository.UserRepository1.findById",fetchType= FetchType.EAGER)) }) List<Account> findAll(); @Select("select * from user where id=#{id} ") User findById(Integer userId); @Data public class Account { private Integer id; private Integer uid; private Double money; //多对一(mybatis中称之为一对一)的映射:一个账户只能属于一个用户 private User1 user1; B一个用户多个账户 @Select("select * from account where uid = #{userId}") List<Account> findAccountByUid(Integer userId); @Select("select * from user") @Results(id="userMap",value={ @Result(id=true,column = "id",property = "id1"), @Result(column = "username",property = "username1"), @Result(column = "address",property = "address1"), @Result(column = "sex",property = "sex1"), @Result(column = "birthday",property = "birthday1"), @Result(property = "accounts",column = "id", many = @Many(select = "com.po.pf.repository.AccountRepository1.findAccountByUid", fetchType = FetchType.LAZY)) }) List<User1> findAll(); @Data public class User1 { private Integer id1; private String username1; private Date birthday1; private String sex1; private String address1; //一对多关系映射:一个用户对应多个账户 private List<Account> accounts;
标签:column,private,开发,Result,mybatis,注解,property,id,user From: https://www.cnblogs.com/popopopopo/p/16982860.html