Mybatis add, modify and delete data

1. New data
The method generated by the MyBatis generator contains two new methods, insert and insertSelective, which can be used directly. The difference between these two methods is that the SQL statement corresponding to insertSelective adds a NULL check, and only inserts fields whose data is not NULL, and insert inserts all fields and inserts NULL values. The test method is as follows, sample code:

//新增
@Test
public void insert() throws ParseException{
    
    
	//获取sqlsession
	SqlSession sqlSession=sessionFactory.openSession();
	try{
    
    
		//获取dao/mapper
		UserDAO userDao=sqlSession.getMapper(UserDAO.class);
		
		User user=new User();
		user.setUsername("小度");
		user.setPassword("345");
		user.setDetime(new Date());
		user.setSex(false);
		user.setPowerid(3);
		
		//新增数据,返回受影响的行数
		int num=userDao.insertSelective(user);
		if(num>0){
    
    
			System.out.println("新增成功");
		}
		
		//提交事务
		sqlSession.commit();
	}finally{
    
    
		//关闭sqlSession
		sqlSession.close();
	}
}

2. To modify data
and add method types, the MyBatis generator method already contains two modification methods, namely updateByPrimryKey, updateByPrimaryKeySelective, the difference is that updateByPrimryKey will modify all fields, if it is empty, NULL will be inserted, and the SQL statement corresponding to updateByPrimaryKeySelective will be added. NULL check, only modify fields that are not NULL. The test method is as follows, sample code:

//修改
@Test
public void update() throws ParseException{
    
    
	//获取sqlsession
	SqlSession sqlSession=sessionFactory.openSession();
	try{
    
    
		//获取dao/mapper
		UserDAO userDao=sqlSession.getMapper(UserDAO.class);
		//先查询出需要修改的数据
		/*User user=userDao.selectByPrimaryKey(22);*/
		User user=new User();
		user.setUserid(22);
		user.setUsername("萨克斯");
		
		//修改数据,返回受影响的行数
		int num=userDao.updateByPrimaryKeySelective(user);
		if(num>0){
    
    
			System.out.println("修改成功");
		}
		
		//提交事务
		sqlSession.commit();
	}finally{
    
    
		//关闭sqlSession
		sqlSession.close();
	}
}

3. Delete data
The method generated by the MyBatis generator contains the method deleteByPrimaryKey to delete data according to the primary key, sample code:

//删除
@Test
public void delete(){
    
    
	//获取sqlsession
	SqlSession sqlSession=sessionFactory.openSession();
	try{
    
    
		//获取dao/mapper
		UserDAO userDao=sqlSession.getMapper(UserDAO.class);
		//删除数据,返回受影响的行数
		int num=userDao.deleteByPrimaryKey(31);
		if(num>0){
    
    
			System.out.println("删除成功");
		}
		
		//提交事务
		sqlSession.commit();
	}finally{
    
    
		//关闭sqlSession
		sqlSession.close();
	}
}

4. Add the return primary key.
Write the UserDAO.java file and add the following code:

/**
 * 新增返回主键
 * @param record
 * @return
 */
public int insertReturnId(User record);

Write the UserDAO.xml file, copy the insertSelective part of the code directly, and add useGeneratedKeys=“true” keyProperty=“userId” on the basis of the original new method. The keyProperty corresponds to the attributes of the PO class. The sample code:

<insert id="insertReturnId" parameterType="com.gx.po.User" useGeneratedKeys="true" keyProperty="userid" >
  insert into user
  <trim prefix="(" suffix=")" suffixOverrides=",">
    <if test="userid != null">
      userid,
    </if>
    <if test="username != null">
      username,
    </if>
    <if test="password != null">
      `password`,
    </if>
    <if test="userage != null">
      userage,
    </if>
    <if test="sex != null">
      sex,
    </if>
    <if test="createtime != null">
      createtime,
    </if>
    <if test="detime != null">
      detime,
    </if>
    <if test="powerid != null">
      powerid,
    </if>
  </trim>
  <trim prefix="values (" suffix=")" suffixOverrides=",">
    <if test="userid != null">
      #{
    
    userid,jdbcType=INTEGER},
    </if>
    <if test="username != null">
      #{
    
    username,jdbcType=VARCHAR},
    </if>
    <if test="password != null">
      #{
    
    password,jdbcType=VARCHAR},
    </if>
    <if test="userage != null">
      #{
    
    userage,jdbcType=INTEGER},
    </if>
    <if test="sex != null">
      #{
    
    sex,jdbcType=BIT},
    </if>
    <if test="createtime != null">
      #{
    
    createtime,jdbcType=DATE},
    </if>
    <if test="detime != null">
      #{
    
    detime,jdbcType=TIMESTAMP},
    </if>
    <if test="powerid != null">
      #{
    
    powerid,jdbcType=INTEGER},
    </if>
  </trim>
</insert>

Guess you like

Origin blog.csdn.net/weixin_44547592/article/details/111329521