首页 > 其他分享 >mybatis注解开发

mybatis注解开发

时间:2022-12-14 18:00:12浏览次数:29  
标签:column private 开发 Result mybatis 注解 property id user

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

相关文章

  • mybatis一级缓存
    mybatis一级缓存缓存概念存在于内存中的临时数据为什么要使用缓存使用mybatis缓存,减少和数据库的交互次数提高执行效率缓存的使用范围经常查询并且不经常改变的数据的......
  • mybatis-plus的BaseMapper
    顾名思义,BaseMapper就是基础的mapper,我们可以通过继承BaseMapper来实现基础的CRUD功能而无需再写单独的xml文件,这个对于SQL不复杂的场景和表来说非常的友好。基本的使用......
  • Maven构建spring整合mybatis的项目
    1.使用Maven构建java项目,修改pom.xml文件,添加所需的依赖jar包<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:......
  • 微信小程序云开发项目源码(失物招领平台)
    1.小程序运行效果  2.小程序源码获取地址​​github地址​​ 3.附上微信云开发后台数据库表结构 ......
  • mybatis的resultType和resultMap
    resultType作为返回值可以是一个基本类型也可以是实体类对象也就是说是一个具体的类如果我们要返回的对象不是一个具体的类假如我们的实体类的属性和数据库的字段不一......
  • Chrome开发者工具抓取重定向页面之前的数据包
    如果一个POST请求处理完成会重定向到另外一个页面,相当于进行了刷新页面操作,原来的POST请求请求信息在​​Network​​这个Tab就看不到了,显示的都是重定向页面的相关HTTP请求......
  • mybatis的连接池
    mybatis的连接池连接池:我们在实际开发中都会使用连接池因为它可以减少我们获取连接所消耗的时间连接池就是用于存储连接的一个容器容器其实就是一个集合对象该集合必须......
  • 基于XML配置开发AspectJ
    1.pom.xml,添加依赖<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-i......
  • Mybatisplus自动生成SQL语句变成下划线
    ###Errorqueryingdatabase.Cause:java.sql.SQLSyntaxErrorException:Unknowncolumn'l_o_g_i_n_n_a_m_e'in'fieldlist'###Theerrormayexistincom/rqzx/api......
  • 基于Springboot+Mybatis+mysql+vue宠物领养网站1
    @目录一、系统介绍二、功能展示1.主页(普通用户)2.登陆、注册(普通用户)3.宠物大全(普通用户)4.宠物详情(申请领养、点赞、评论)(普通用户)5.我的申请(普通用户)6.个人信息(普通用户......