GithubHelp home page GithubHelp logo

learngit's People

Contributors

oyjin avatar

Watchers

 avatar  avatar

learngit's Issues

MpNotes

dao层mapper接口继承BaseMpper,泛型T为要操作的实体类

mp常用注解:
	@TableName("表名")
		加在实体类上,指定映射的数据库表格(数据库表名与实体类名不一致时必须添加该注解指定映射关系)
	@TableId
		加在实体类的字段上,该字段对应数据库的主键(注:默认主键名为id,若主键字段名不是id必须加注解)
	@TableFiled("数据名")
		当实体变量名和表中数据名不一致时,在实体类变量上加该注解,指定对应表中哪个数据
		
排除非表字段的三种方式:
	1.给该字段加上transient关键字,使其不参与序列化过程,插入表格过程中这个变量会被忽略,sql语句可正常执行
	2.该字段加上static关键字,insert语句执行时会忽略该字段
	3.在该实体变量上加注解@TableFiled(exit=false),代表该字段不是数据库中的字段
	
MyBatis-Plus查询方法
  普通查询:
	1.T selectById(Serializable id) 根据ID查询,id:表格中主键ID
	2.List<T> selectBatchIds(@Param(constants.Collection)Collection<?extends Serializable>  idList) 根据传入的id集合,查询多个T对象以List<T>形式返回
	3.List<T> selectByMap(Map<key,value>) 传入的map对象key为数据库中的列名(注意不是实体类中的字段名),value为查询列的值,返回List<T>对象
  
  条件构造器查询(https://mp.baomidou.com/guide/wrapper.html#abstractwrapper):
    举例说明:
	1.查询表中为王姓或年龄大于等于25,按照年龄降序排列,年龄相同按照id升序排列   name like '王%' or age>=25 order by age desc,id asc
		QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
		queryWrapper.likeRight("name",王).or().ge("age",25).orderByDesc("age").orderByAsc("id");
		List<User> userList = userMapper.selectList(queryWrapper); 
		注:QueryWrapper用来生成查询条件,泛型为要操作的实体类,括号添加的条件为数据库中的列名不是实体类的属性; .方法名(查询条件)默认是and连接
		
	2.查询创建日期为2019年2月14日且直属上级为名字为王姓的数据		data_format(create_time,'%Y-%m-%d') and manager_id in (select id from user where name like '王%')
		QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
		queryWrapper.apply("data_format(create_time,'%Y-%m-%d')={0},"2019-02-14")
					.inSql("manager_id","select id from user where name like '王%'");
		List<User> userlist = userMapper.selectList(queryWrapper);
		注:data_format格式化查询日期
		
	3.查询名字为王姓并且(年龄小于40或邮箱不为空)  name like '王%' and (age<40 or email is not null)
		queryWrapper.likeRight("name","王").and(wq->wq.lt("age",40).or().isnNotNull("email");
		List<User> userlist = userMapper.selectList(queryWrapper);
		
	4.查询(年龄小于40或邮箱不为空)并且名字为王姓   (age<40 or email is not null) and name like '王%'
		queryWrapper.nested(wq->wq.lt("age",40).or().isNotNll("email")).likeRight("name","王%");
		注:nested用于正常嵌套 不带 AND 或者 OR,括号内可以用lambda实现的函数式
		
	5.查询年龄为30、31、34、35的数据    age in(30、31、34、35)
		queryWrapper.in("age",Arrays.asList(30,31,34,35);
		
	6.条件同上,只返回满足条件的其中一条语句即可  limit 1
		queryWrapper.in("age",Arrays.asList(30,31,34,35).last("limit 1");
		注:last()方法无视优化规则直接拼接到 sql 的最后,只能调用一次,多次调用以最后一次为准 有sql注入的风险,请谨慎使用
		
  select不列出全部字段:
	1.查询名字中包含雨并且年龄小于40的人的id和name   select id,name from table where name like '%雨%' and age <40
		queryWrapper.select("id","name").like("name","雨").lt("age",40);
		
	2.排除不需要查询的列
		queryWrapper.like("name","%雨%").lt("age",40)
				    .select(User.class,info->!info.getColumn().equals("create_time")&&
					        !info.getColumn.equals("manager_id"));
		查询结果中排除了create_time和manager_id两列
		
  条件构造器中condition的作用
	like()方法可加入三个参数:queryWrapper.like(condition,column,val) 其中condition为判断条件,控制这条语句是否加入sql语句中,后两个参数为查询的列名和值
	例子:前端有根据姓名、邮箱搜索用户的功能,输入姓名、邮箱点击搜索,将这两个值传到后台,查询符合条件的数据返回
		  name、email为前端传来的值:
		  QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
		  queryWrapper.like(StringUtils.isNotEmpty(name),"name",name)
					  .like(StringUtils.isNotEmpty(email),"email",email);
  
  实体对象作为条件构造器方法的参数
	例:
		User user1 = new User();
		user1.setName("张三");
		user1.setAge(25);
		QueryWrapper<User> queryWrapper = new QueryWrapper<User>(user1);
		List<User> userlist = userMapper.selectList(queryWrapper);
	这样查询条件即为传入实体中的属性值。
	若查询条件需要模糊查询,可在实体字段上加注解:@TableFiled(condition=SqlCondition.LIKE) 表示该字段使用模糊查询 
	若查询条件要求不等值,可在实体字段上加注解:例如在age上加注解@TableFiled(condition="%s&lt;#{%s}"),表示age<#{age}
		
  其它使用条件构造器的方法:
    1.使用selectMaps(queryWrapper)方法,以map形式而非对象形式返回
	  例如只查询特定几列时,如果使用selectList(queryWrapper),返回List<User>,返回的对象中未查询的字段值全为null,使用selectMaps(queryWrapper)方法只返回查询的键值对,更优雅
	    public void selectByWrapperMaps(){
			QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
			// 只查询id和name
			queryWrapper.select("id","name").like("name","雨").lt("age",40);
			List<Map<String,Object>> userlist = userMapper.selectMaps(queryWrapper);
			userList.forEach(System.out::println)
		}
	  返回结果为:{id=2333,name="小雨"},{id=666,name="阵雨")

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.