目录
①问题:(问题代码我就不演示了,之前的文章里有讲解,看了就明白)
今天一定要把MyBatis练习给完结了。
一.修改
1)修改全部字段
①上代码
StudentMapper.java
//修改
int update(Student stu);
StudentMapper.xml
<!-- 修改-->
<update id="update">
update student
set name = #{name},
age = #{age},
address = #{address},
major = #{major}
where id = #{id};
</update>
MyBatisTest3.java
package Study.test;
import Study.Mapper.StudentMapper;
import Study.test1.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.InputStream;
public class MyBatisTest3 {
@Test
public void test() throws Exception {
//参数
String name = "nnn";
int age = 21;
String address = "HuNan";
String major = "JK";
int id = 2;
Student stu3 =new Student();
stu3.setId(id);
stu3.setAge(age);
stu3.setAddress(address);
stu3.setMajor(major);
stu3.setName(name);
//1.获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.获取Mapper接口代理对象
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
//4.执行方法
//4.修改
System.out.println("before :");
System.out.println(studentMapper.selectAll());
int count = studentMapper.update(stu3);
System.out.println("after :");
System.out.println(studentMapper.selectAll());
System.out.println();
//输出影响行数
System.out.println("Effected row number:");
System.out.println(count);
//提交事务
sqlSession.commit();
//5.释放资源
sqlSession.close();
}
}
运行结果:
2)修改动态字段(不多说就是用if标签)
①问题:(问题代码我就不演示了,之前的文章里有讲解,看了就明白)
1.set中最后一个参数没有设置,以逗号结束而报错
2.一个参数也没有设置时,set多余
②解决方法
使用同查询时where标签功能类似的set标签
③代码
StudentMapper.xml
<!-- 修改-->
<update id="update">
update student
<set>
<if test="name != null and name !=''">
name = #{name},
</if>
<if test="age != null">
age = #{age},
</if>
<if test="address != null and address !=''">
address = #{address},
</if>
<if test="major != null and major !=''">
major = #{major}
</if>
</set>
where id = #{id};
</update>
StudentMapper.java(主要演示最后一个参数注释掉,第一个参数注释掉大家自己尝试)
package Study.test;
import Study.Mapper.StudentMapper;
import Study.test1.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.InputStream;
public class MyBatisTest3 {
@Test
public void test() throws Exception {
//参数
String name = "nnn";
int age = 20;
int id = 2;
Student stu3 =new Student();
stu3.setId(id);
stu3.setName(name);
stu3.setAge(age);
//stu3.setAddress(address);
//stu3.setMajor(major);
//1.获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.获取Mapper接口代理对象
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
//4.执行方法
//4.修改
System.out.println("before :");
System.out.println(studentMapper.selectAll());
int count = studentMapper.update(stu3);
System.out.println("after :");
System.out.println(studentMapper.selectAll());
System.out.println();
System.out.println("Effected row number:");
System.out.println(count);
//提交事务
sqlSession.commit();
//5.释放资源
sqlSession.close();
}
}
④运行结果:
二.删除
1)删除一个
①代码
StudentMapper.java
//删除
void deleteById(int id);
StudentMapper.xml
<delete id="deleteById">
delete from student where id = #{id}
</delete>
MyBatisTest3.java
package Study.test;
import Study.Mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.InputStream;
public class MyBatisTest3 {
@Test
public void test() throws Exception {
//参数
int id = 2;
//1.获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.获取Mapper接口代理对象
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
//4.执行方法
//4.删除
System.out.println("before :");
System.out.println(studentMapper.selectAll());
studentMapper.deleteById(id);
System.out.println("after :");
System.out.println(studentMapper.selectAll());
//提交事务
sqlSession.commit();
//5.释放资源
sqlSession.close();
}
}
②运行结果:
2)批量删除(foreach标签)
foreach标签
mybatis会将数组参数,封装为一个Map集合。
默认:array = 数组<foreach collection="array "
在接口定义方法时使用@Param注解改变map集合的默认key的名称就是要么写array,要么写@Param定义的名称。
其他属性:item="id" separator="," open="(" close=")"
以id为元素,"("开始,")"结束,以”,“为分隔符
组成(id,id,id...)的语句
①代码
StudentMapper.java
//批量删除
void deleteByIds(@Param("ids") int[] ids);
StudentMapper.xml
<delete id="deleteByIds">
delete from student
where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
MyBatisTest3.java
package Study.test;
import Study.Mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.InputStream;
public class MyBatisTest3 {
@Test
public void test() throws Exception {
//参数
int[] ids = {1,4};
//1.获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.获取Mapper接口代理对象
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
//4.执行方法
//4.删除
System.out.println("before :");
System.out.println(studentMapper.selectAll());
studentMapper.deleteByIds(ids);
System.out.println("after :");
System.out.println(studentMapper.selectAll());
//提交事务
sqlSession.commit();
//5.释放资源
sqlSession.close();
}
}
② 运行结果:
大功告成!
啊哈哈哈,MyBatis增删改查练习到此结束!!!
明天见!