多参数要加注解
如果要两个参数查询
就要给参数加注解
方法参数
package com.woniuxy.dao;
import com.woniuxy.entity.Student;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author author
* @create 2022-12-2022/12/27 11:47
*/
public interface StudentDao {
//一个参数,对象 #{属性}
void insertStudent(Student student);
void updateStduent(Student student);
//一个参数,基本类型和字符串 占位符 #{idxxx}
void deleteStudentById(int id);
Student selectStudentByid(int id);
List<Student> selectAllStudents();
//一个参数,基本类型和字符串 占位符 #{idxxx}
List<Student> selectStudentsByName(String name);
//一个参数,使用注解 #{name}
List<Student> selectStudentsByName2(@Param("name") String name);
//多个参数必须使用注解,起名 #{name} #{age}
List<Student> selectStudentsByNameAag(@Param("name") String name,
@Param("age") int age);
//一个参数,对象 #{属性}
List<Student> selectStudentsByStudent(Student student);
//一个参数,对象 使用注解 #{注解名.属性}
List<Student> selectStudentsByStudent2(@Param("student") Student student);
//#{name} #{student.name} #{student.age}
List<Student> selectStudentsBy(@Param("name") String name,
@Param("student") Student student);
}
<?xml version="1.0" encoding="UTF-8" ?>标签:要加,name,age,List,Param,参数,student,注解 From: https://www.cnblogs.com/huangjiangyang/p/17011054.html
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:就是个名字
现在可以任意,
以后必须是接口的权限定名
-->
<mapper namespace="com.woniuxy.dao.StudentDao">
<!--映射文件,就是写Dao方法对应的sql语句-->
<!--id就是一个名字,名字现在可以任意,以后必须是接口中对应的方法名
parameterType:参数类型的全限定名
参数是对象,对应列值,就是使用#{属性名}-->
<!--useGeneratedKeys="true" keyProperty="id" 将自增的主键值,赋值给对象的属性-->
<insert id="insertStudent" useGeneratedKeys="true" keyProperty="id">
insert into student values(null,#{name},#{age},#{score})
</insert>
<update id="updateStduent">
update student set name=#{name},age=#{age},score=#{score} where id=#{id}
</update>
<delete id="deleteStudentById">
delete from student where id=#{idxxx}
</delete>
<select id="selectStudentByid" resultType="Student">
select * from student where id=#{id}
</select>
<select id="selectAllStudents" resultType="Student">
select * from student
</select>
<select id="selectStudentsByName" resultType="Student">
<!--error-->
<!--SELECT * FROM student WHERE NAME LIKE '%#{name}%'-->
<!--SELECT * FROM student WHERE NAME LIKE '%' #{name} '%'-->
SELECT * FROM student WHERE NAME LIKE concat('%',#{name},'%')
</select>
<select id="selectStudentsByName2" resultType="Student">
SELECT * FROM student WHERE NAME LIKE concat('%',#{name},'%')
</select>
<select id="selectStudentsByNameAag" resultType="Student">
SELECT * FROM student WHERE NAME LIKE '%' #{name} '%' and age>#{age}
</select>
<select id="selectStudentsByStudent" resultType="Student">
SELECT * FROM student WHERE NAME LIKE '%' #{name} '%' and age>#{age}
</select>
<select id="selectStudentsByStudent2" resultType="Student">
SELECT * FROM student WHERE NAME LIKE '%' #{student.name} '%' and age>#{student.age}
</select>
<select id="selectStudentsBy" resultType="Student">
SELECT * FROM student WHERE NAME LIKE '%' #{name} '%' and age>#{student.age}
</select>
</mapper>