public Student getStudent(Student student);
<select id="getEmployeeByConditionIf" resultType="com.neuedu.entity.Employee">
select *from tbl_employee where id = #{id} and user_name = #{userName} and email = #{email} and gender = #{gender}
</select>
<select id="getStudent" resultType="com.neuedu.mybatis.entity.Student">
SELECT *
FROM student
where
<if test="id != null">
id=#{id}
</if>
<if test="name !=null and name!=''">
and name=#{name}
</if>
<if test="password !=null and password !=''">
and password=#{password}
</if>
<if test="email !=null and email !=''">
and email=#{email}
</if>
</select>
@Test
public void TestgetStudent(){
StudentMapper bean = ioc.getBean(StudentMapper.class);
Student student = new Student(4,"jack", "111", "jack@qq.com");
System.out.println(student);
Student student2 = bean.getStudent(student);
System.out.println(student2);
}
#测试结果没问题,
<select id="getStudent" resultType="com.neuedu.mybatis.entity.Student">
SELECT *
FROM student
<where>
<if test="id != null">
id=#{id}
</if>
<if test="name !=null and name!=''">
and name=#{name}
</if>
<if test="password !=null and password !=''">
and password=#{password}
</if>
<if test="email !=null and email !=''">
and email=#{email}
</if>
</where>
</select>
<select id="getStudent" resultType="com.neuedu.mybatis.entity.Student">
SELECT *
FROM student
<trim prefix="where" prefixOverrides="and">
<if test="id != null">
id=#{id}
</if>
<if test="name !=null and name!=''">
and name=#{name}
</if>
<if test="password !=null and password !=''">
and password=#{password}
</if>
<if test="email !=null and email !=''">
and email=#{email}
</if>
</trim>
</select>
public List<Student> getStus(Student student);
<select id="getStus" resultType="com.neuedu.mybatis.entity.Student">
select * from student
<where>
<choose>
<when test="id !=null">
id = #{id}
</when>
<when test="name !=null and name!=''">
name = #{name}
</when>
<when test="password !=null and password!=''">
password = #{password}
</when>
<when test="email !=null and email!=''">
email = #{email}
</when>
<otherwise>
1 = 1
</otherwise>
</choose>
</where>
</select>
public void updateStu(Student student);
<update id="updateStu">
update student
<set>
<if test="name !=null and name!=''">
name=#{name},
</if>
<if test="password !=null and password !=''">
password=#{password},
</if>
<if test="email !=null and email !=''">
email=#{email}
</if>
</set>
where id = #{id}
</update>
@Test
public void TestUpdateStu(){
StudentMapper bean = ioc.getBean(StudentMapper.class);
bean.updateStu(new Student(4, "jackk", null, null));
}
<update id="updateStu">
update student
<trim prefix="set" suffixOverrides=",">
<if test="name !=null and name!=''">
name=#{name},
</if>
<if test="password !=null and password !=''">
password=#{password},
</if>
<if test="email !=null and email !=''">
email=#{email}
</if>
</trim>
where id = #{id}
</update>
public List<Student> getStuByIdForEach(@Param("ids")List<Integer> ids);
<select id="getStuByIdForEach" resultType="com.neuedu.mybatis.entity.Student">
select * from student
where id
in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
@Test
public void getStuByIdForEach(){
StudentMapper bean = ioc.getBean(StudentMapper.class);
List<Integer> list = Arrays.asList(16,17,18,19);
List<Student> stuByIdForEachlist = bean.getStuByIdForEach(list);
for (Student student : stuByIdForEachlist) {
System.out.println(student);
}
}
public void insertStus(@Param("stus")List<Student> student);
<insert id="insertStus">
insert into student (name,password,email) values
<foreach collection="stus" item="stu" separator=",">
(#{stu.name},#{stu.password},#{stu.email})
</foreach>
</insert>
@Test
public void TestInsertStus(){
StudentMapper bean = ioc.getBean(StudentMapper.class);
List<Student> list = new ArrayList<Student>();
list.add(new Student("123","123", "123"));
list.add(new Student("123","123", "123"));
list.add(new Student("123","123", "123"));
bean.insertStus(list);
}
@Test
public void TestFirstCache(){
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
session = sqlSessionFactory.openSession();
mapper = session.getMapper(EmployeeMapper.class);
Employee emp = mapper.getEmpInfoById(4);
System.out.println(emp);
Employee emp2 = mapper.getEmpInfoById(4);
System.out.println(emp2);
System.out.println(emp == emp2);
session.commit();
session.close();
}
@Test
public void TestFirstCache(){
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
session = sqlSessionFactory.openSession();
mapper = session.getMapper(EmployeeMapper.class);
Employee emp = mapper.getEmpInfoById(4);
System.out.println(emp);
SqlSession session2 = sqlSessionFactory.openSession();
EmployeeMapper mapper2 = session2.getMapper(EmployeeMapper.class);
Employee emp2 = mapper2.getEmpInfoById(4);
System.out.println(emp2);
System.out.println(emp == emp2);
session.commit();
session.close();
}
@Test
public void TestFirstCache(){
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
session = sqlSessionFactory.openSession();
mapper = session.getMapper(EmployeeMapper.class);
Employee emp = mapper.getEmpInfoById(4);
System.out.println(emp);
Employee emp2 = mapper.getEmpInfoById(16);
System.out.println(emp2);
System.out.println(emp == emp2);
session.commit();
session.close();
}
@Test
public void TestFirstCache(){
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
session = sqlSessionFactory.openSession();
mapper = session.getMapper(EmployeeMapper.class);
Employee emp = mapper.getEmpInfoById(4);
System.out.println(emp);
mapper.deleteEmp(16);
Employee emp2 = mapper.getEmpInfoById(4);
System.out.println(emp2);
System.out.println(emp == emp2);
session.commit();
session.close();
}
@Test
public void TestFirstCache(){
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
session = sqlSessionFactory.openSession();
mapper = session.getMapper(EmployeeMapper.class);
Employee emp = mapper.getEmpInfoById(4);
System.out.println(emp);
session.clearCache();
Employee emp2 = mapper.getEmpInfoById(4);
System.out.println(emp2);
System.out.println(emp == emp2);
session.commit();
session.close();
}
<setting name="cacheEnabled" value="true"/>
<cache eviction="FIFO" size="100" readOnly="false"/>
@Test
public void TestFirstCache(){
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
session = sqlSessionFactory.openSession();
mapper = session.getMapper(EmployeeMapper.class);
Employee emp = mapper.getEmpInfoById(4);
System.out.println(emp);
session.close();
SqlSession session2 = sqlSessionFactory.openSession();
EmployeeMapper mapper2 = session2.getMapper(EmployeeMapper.class);
Employee emp2 = mapper2.getEmpInfoById(4);
System.out.println(emp2);
session2.close();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有