MyBatis中动态Sql实现增删改查

  1. 编写查询的接口
    1. package com.dao;
      
      import java.util.List;
      import java.util.Map;
      
      import org.apache.ibatis.annotations.Param;
      
      import com.pojo.Role;
      import com.pojo.User;
      
      public interface UserMapper {
      	// 使用where标签多条件查询
      	List findUserByNameAndRoleIdAndPhone(@Param("uname") String name,
      			@Param("roleId") Integer roleId, @Param("phone") String phone);
      	//使用trim标签多条件查询
      	List findUserByNameAndRoleIdAndPhone1(@Param("uname") String name,
      			@Param("roleId") Integer roleId, @Param("phone") String phone);
      
      	// 使用set标签修改
      	int updateRole(Role role);
      
      	// 删除
      	int deleteUser(int id);
      
      	// 添加
      	int addRole(Role role);
      
      	// 用户传递过来的是一个数组,根据数组进行查询
      	List findUserAndRoleByIds(Integer[] ids);
      
      	// 用户传递过来的是一个数组,根据数组进行查询
      	List findUserAndRoleByNames(List names);
      
      	// 使用map集合进行入参查询
      	List findUserAndRoleByMap(Map map);
      	
      	//使用choose标签确定只能以某一个作为条件进行查询
      	List findUserByOne(User user);
      }
      

       

  2.  编写sql的映射文件UserMapper.xml
    1. 
      
      
      
      	
      	
      	
      	
      	
      	
      	
      	
      	
      	
      	
      		 UPDATE `smbms_role` 
      		 
      		
      	
      		
      		
      			roleCode=#{roleCode},
      		 	roleName=#{roleName},
      		 	createdBy=#{createdBy},
      		 	modifyBy=#{modifyBy},
      		
      	
      
      		
      	
      		DELETE FROM `smbms_user`
      		
      			id=#{id}
      		
      	
      
      	
      	
      		INSERT INTO `smbms_role` 
      		
      			id,
      			roleCode,
      		 	roleName,
      		 	createdBy,
      		 	creationDate,
      		 	modifyBy,
      		 	modifyDate,
      		
      		
      			#{id},
      			#{roleCode},
      		 	#{roleName},
      		 	#{createdBy},
      		 	#{creationDate},
      		 	#{modifyBy},
      		 	#{modifyDate},
      		
      	
      	
      	
      	
      	
      		
      		
      	
      	
      	
      	
      	
      	
      	
      	
      	
      	
      

       

  3. 编写测试类
    1. package com.test;
      
      import java.util.ArrayList;
      import java.util.HashMap;
      import java.util.List;
      import java.util.Map;
      
      import org.apache.ibatis.annotations.Select;
      import org.apache.ibatis.session.SqlSession;
      import org.apache.log4j.Logger;
      import org.junit.Test;
      
      import com.dao.UserMapper;
      import com.pojo.Role;
      import com.pojo.User;
      import com.util.MyBatisUtil;
      
      public class MyBatisTest {
      	Logger logger=Logger.getLogger(MyBatisTest.class);
      	
      	//使用where多条件查询
      	@Test
      	public void testWhere(){
      		SqlSession sqlSession = MyBatisUtil.openSession();
      		List users = sqlSession.getMapper(UserMapper.class).findUserByNameAndRoleIdAndPhone("王洋", null, null);
      		for (User u : users) {
      			logger.debug(u);
      		}
      	}
      	
      	//使用trim多条件查询
      	@Test
      	public void testTrim(){
      		SqlSession sqlSession = MyBatisUtil.openSession();
      		List users = sqlSession.getMapper(UserMapper.class).findUserByNameAndRoleIdAndPhone1("张", null, null);
      		for (User u : users) {
      			logger.debug(u);
      		}
      	}
      	
      	//修改操作
      	@Test
      	public void testSet(){
      		SqlSession sqlSession = MyBatisUtil.openSession();
      		Role role=new Role();
      		role.setId(3);
      		role.setRoleName("员工");
      		role.setModifyBy(2);
      		int count = sqlSession.getMapper(UserMapper.class).updateRole(role);
      		sqlSession.commit();
      		logger.debug(count);
      	}
      	
      	//删除操作
      	@Test
      	public void testDelete(){
      		SqlSession sqlSession = MyBatisUtil.openSession();
      		int count = sqlSession.getMapper(UserMapper.class).deleteUser(17);
      		sqlSession.commit();
      		logger.debug(count);
      	}
      	
      	//添加操作
      	@Test
      	public void testInsert(){
      		SqlSession sqlSession = MyBatisUtil.openSession();
      		Role role=new Role();
      		role.setRoleName("员工");
      		int count = sqlSession.getMapper(UserMapper.class).addRole(role);
      		sqlSession.commit();
      		logger.debug(count);
      	}
      	
      	//根据数组进行查询 
      	@Test
      	public void testArray(){
      		SqlSession sqlSession = MyBatisUtil.openSession();
      		Integer[] ids=new Integer[]{6,8,10};
      		List users = sqlSession.getMapper(UserMapper.class).findUserAndRoleByIds(ids);
      		for (User u : users) {
      			logger.debug(u+","+u.getUserRoleName());
      		}
      	}
      	//根据list集合入参查询
      	@Test
      	public void testList(){
      		SqlSession sqlSession = MyBatisUtil.openSession();
      		List names=new ArrayList();
      		names.add("张华");
      		names.add("张晨");
      		names.add("asd");
      		names.add("赵燕");
      		List users = sqlSession.getMapper(UserMapper.class).findUserAndRoleByNames(names);
      		for (User u : users) {
      			logger.debug(u+","+u.getUserRoleName());
      		}
      	}
      	
      	//根据map集合入参查询
      	@Test
      	public void testMap(){
      		SqlSession sqlSession = MyBatisUtil.openSession();
      		Map map=new HashMap();
      		String[] a={"张三","lisi","张华","张晨"};
      		List phoneList=new ArrayList();
      		phoneList.add("18098765434");
      		phoneList.add("13544561111");
      		phoneList.add("123");
      		map.put("nameKey", a);
      		map.put("phoneKey", phoneList);
      		List users = sqlSession.getMapper(UserMapper.class).findUserAndRoleByMap(map);
      		for (User u : users) {
      			logger.debug(u+","+u.getUserRoleName());
      		}
      	}
      	
      	//测试 choose
      	@Test
      	public void testChoose() {
      		SqlSession sqlSession = MyBatisUtil.openSession();
      		User user=new User();
      		//user.setId(2);
      		//user.setUserName("张");
      		//user.setPhone("13544561111");
      		List users = sqlSession.getMapper(UserMapper.class).findUserByOne(user);
      		for (User u : users) {
      			logger.debug(u);
      		}
      	}
      	
      }
      
       

你可能感兴趣的:(MyBatis)