Mybatis动态sql语句注解的简单实例

创建一个接口类:

package com.pk.sql;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.jdbc.SQL;

public interface EmpMapper {
	
	/*	注解的动态sql
	 * 				第一种:不推荐
	 * 
	 * @Select("")*/
	
	@SelectProvider(type=EmpProvider.class,method="getSql1")
	public List queryEmp(@Param("emps") Emp emp);
	public List queryEmp1(Emp emp);
	public List queryEmp2(Emp emp);
	@SelectProvider(type=EmpProvider.class,method="update")
	public void updateEmp(@Param("emps") Emp emp);
	public List queryEmp3(Emp emp);
	@InsertProvider(type=EmpProvider.class,method="add")
	public void addEmp(@Param("add") Emp emp);
	
	
	static class EmpProvider {
		
		public String add(Map map) {
			Emp emp = map.get("add");
			SQL sql = new SQL();
			sql.INSERT_INTO("emp");
			if(emp.getEname()!=null) {
				sql.VALUES("ename", "#{add.ename}");
			}
			if(emp.getSal()!=0) {
				sql.VALUES("sal", "#{add.sal}");
			}
			return sql.toString();
		}
		
		public String update(Map map) {
			Emp emp = map.get("emps");
			SQL sql = new SQL();
			sql.UPDATE("emp");
			if(emp.getEname()!=null) {
				sql.SET("ename=#{emps.ename}");
			}
			if(emp.getSal()!=0) {
				sql.SET("sal = #{emps.sal}");
			}
			sql.WHERE("empno = #{emps.empno}");
			
			
			return sql.toString();
		}
		
		
		public String getSql1(Map map) {
			Emp emp = map.get("emps");
			SQL sql = new SQL();
			sql.SELECT("*");
			sql.FROM("emp");
			if(emp.getSal()!=0) {
				sql.AND();
				sql.WHERE(" sal = #{emps.sal}");
			}
			if(emp.getEname()!=null) {
				sql.AND();
				sql.WHERE(" ename like #{emps.ename}");
			}
			
			return sql.toString();
		}
		
		
		/**
		 * 			第二种:不推荐
		 * @param map
		 * @return
		 */
		public String getSql(Map map) {
			Emp emp = map.get("emps");
			String sql = "select * from emp where 1=1";
			if(emp.getEname()!=null) {
				sql += " and ename like #{emps.ename}";
			}
			if(emp.getSal()!=0) {
				sql += " and sal = #{emps.sal}";
			}
			return sql;
		}
	}
}

实体类:

package com.pk.sql;

import java.util.List;

public class Emp {
	private int empno;
	private String ename;
	private String job;
	private int sal;
	private List deptNoList;
	
	
	
	
	public List getDeptNoList() {
		return deptNoList;
	}
	public void setDeptNoList(List deptNoList) {
		this.deptNoList = deptNoList;
	}
	
	
	public int getSal() {
		return sal;
	}
	public void setSal(int sal) {
		this.sal = sal;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public int getEmpno() {
		return empno;
	}
	public void setEmpno(int empno) {
		this.empno = empno;
	}
	public String getEname() {
		return ename;
	}
	public void setEname(String ename) {
		this.ename = ename;
	}
	@Override
	public String toString() {
		return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + ", sal=" + sal + "]";
	}
}

在建一个测试类:

package com.pk.sql;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

/**
 * mybatis的终极思想:1.为了解决sql语句硬编码的问题(约定大于配置) 2.解决面向对象设计问题(面向接口编程)
 * 每个对象对应一个xml文件(映射文件 MAPPING) 设:emp表 emp.xml dept表 dept.xml
 * 
 * @author 
 *
 *         2018年11月10日上午9:06:27
 */
public class TestMybatis {
	// 获取SqlSession对象
	public static SqlSession getSession() {
		String resource = "config.xml";
		InputStream resourceAsStream = TestMybatis.class.getResourceAsStream(resource);
		// session工厂 负责产生会话
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
		// 会话就是打开了和数据库的链接
		SqlSession openSession = sqlSessionFactory.openSession();
		return openSession;
	}
	
	
	@Test
	public void test() {
		SqlSession session = getSession();
		Emp emps = new Emp();
		emps.setSal(4000);
		emps.setEname("%小%");
		EmpMapper mapper = session.getMapper(EmpMapper.class);
		
		List queryEmp = mapper.queryEmp(emps);
		System.out.println(queryEmp);
	}
	
	@Test
	public void test3() {
		
		SqlSession session = getSession();
		Emp emp = new Emp();
		emp.setSal(3000);
		emp.setEname("动物");
		emp.setEmpno(10019);
		
		EmpMapper mapper = session.getMapper(EmpMapper.class);
		mapper.updateEmp(emp);
		session.commit();
		session.close();
	}
	
	@Test
	public void test5() {
		
		SqlSession session = getSession();	
		EmpMapper mapper = session.getMapper(EmpMapper.class);
		Emp emp = new Emp();
		emp.setEname("张三");
		emp.setSal(5000);
		mapper.addEmp(emp);
		session.commit();
		session.close();
		
	}


}

注意: 在config.xml文件中要配好接口类的映射路径:(包名.类名)
mybatis架包下载:https://mvnrepository.com/artifact/org.mybatis/mybatis

你可能感兴趣的:(mybatis)