一. MyBatis 多表操作

  • 创建之后用到的表
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    create database db4;
    use db4;

    create table card(
    id int primary key auto_increment,
    number varchar(20),
    pid int,

    constraint cp_fk foreign key (pid) references person(id)

    );
    insert into card values (null,'12345',1);
    insert into card values (null,'23456',2);
    insert into card values (null,'34567',3);
    ==============================================================
    create table person(
    id int primary key auto_increment,
    name varchar(20),
    age int
    );
    insert into person values (null,'张三',23);
    insert into person values (null,'李四',24);
    insert into person values (null,'王五',25);
    ==============================================================
    create table classes(
    id int primary key auto_increment,
    name varchar(20)
    );
    insert into classes values (null,'黑马一班');
    insert into classes values (null,'黑马二班');
    ==============================================================
    create table student(
    id int primary key auto_increment,
    name varchar(30),
    age int,
    cid int,
    constraint cs_fk foreign key (cid) references classes(id)
    );
    insert into student values (null,'张三',23,1);
    insert into student values (null,'李四',24,1);
    insert into student values (null,'王五',25,2);
    insert into student values (null,'马六',26,2);
    ==============================================================
    create table course(
    id int primary key auto_increment,
    name varchar(30)
    );
    insert into course values (null,'语文');
    insert into course values (null,'数学');
    ==============================================================
    create table stu_cr(
    id int primary key auto_increment,
    sid int,
    cid int,
    constraint sc_fk1 foreign key (sid) references student(id),
    constraint sc_fk2 foreign key (cid) references course(id)
    );
    insert into stu_cr values (null,1,1),(null,1,2),(null,2,1),(null,2,2);

获取sqlsession会话对象工具类

  • SqlsessionUtils
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public classSessionUtils {
    public staticSqlSession getSqlsession(){
    SqlSession sqlSession =null;
    try{
    InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
    SqlSessionFactory factory =newSqlSessionFactoryBuilder().build(is);
    is.close();
    sqlSession = factory.openSession(true);
    }catch(IOException e) {
    e.printStackTrace();
    }finally{
    returnsqlSession;
    }
    }
    }

1.一对一

1to1.jpg

Untitled

表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
create table card(
idint primary key auto_increment,
numbervarchar(20),
pidint,

constraintcp_fkforeign key(pid)referencesperson(id)

);
insert intocardvalues(null,'12345',1);
insert intocardvalues(null,'23456',2);
insert intocardvalues(null,'34567',3);

create table person(
id int primary key auto_increment,
name varchar(20),
age int
);
insert into person values (null,'张三',23);
insert into person values (null,'李四',24);
insert into person values (null,'王五',25);

pojo类

  • Person
    1
    2
    3
    privateInteger id;
    privateString name;
    privateInteger age;
  • Card
    1
    2
    3
    4
    privateInteger id;
    privateString number;

    privatePerson p;

interface接口:

1
2
3
4
public interface OtoMapper {
//一对一
List<Card> selectAll();
}

mapper映射:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<mapper namespace="com.itheima.mapper.OtoMapper">

<resultMap id="OneToOne" type="card">
<id column="cid" property="id"/>
<result column="number" property="number"/>

<!-- association:配置被包含对象的映射关系-->
<!-- property:被包含对象变量名-->
<!-- javaType:被包含对象数据类型-->
<association property="p" javaType="person">
<id column="pid" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
</association>
</resultMap>

<!-- List<Card> selectAll();一对一-->
<select id="selectAll" resultMap="OneToOne">
select c.id cid,number,pid,name,age from card c,person p where c.pid=p.id;
</select>
</mapper>

Test类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//一对一
@Test
public voidselectAll(){
SqlSession sqlSession = SessionUtils.getSqlsession();

OtoMapper mapper = sqlSession.getMapper(OtoMapper.class);
List<Card> cards = mapper.selectAll();

for(Card c : cards) {
System.out.println(c);
}

sqlSession.close();
}

查询结果:
Card{id=1, number=’123456’, p=Person{id=1, name=’张三’, age=23}}
Card{id=2, number=’234567’, p=Person{id=2, name=’李四’, age=24}}
Card{id=3, number=’345678 ‘, p=Person{id=3, name=’王五 ‘, age=25}}


2.一对多

onetomany.jpg

Untitled

表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
create table classes(
id int primary key auto_increment,
name varchar(20)
);
insert into classes values (null,'黑马一班');
insert into classes values (null,'黑马二班');

create table student(
id int primary key auto_increment,
name varchar(30),
age int,
cid int,
constraint cs_fk foreign key (cid) references classes(id)
);
insert into student values (null,'张三',23,1);
insert into student values (null,'李四',24,1);
insert into student values (null,'王五',25,2);
insert into student values (null,'马六',26,2);

pojo类:

  • Classes
    1
    2
    3
    4
    privateInteger id;
    privateString name;

    privateList<Student> students;
  • Student
    1
    2
    3
    4
    5
    privateInteger id;
    privateString name;
    privateInteger age;

    privateList<Course> courses;//多对多时增加一个属性

interface接口:

1
2
3
4
public interfaceOtmMapper {
//一对多
List<Classes> OtmSelectAll();
}

mapper映射:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<mapper namespace="com.itheima.mapper.OtmMapper">
<resultMap id="OneToMany" type="classes">
<id column="cid" property="id"/>
<result column="cname" property="name"/>
<!-- collection配置被包含的集合对象的映射关系
property被包含对象的变量名
ofType被包含对象的实际数据类型-->
<collection property="students" ofType="student">
<id column="sid" property="id"/>
<result column="sname" property="name"/>
<result column="sage" property="age"/>
</collection>
</resultMap>
<select id="OtmSelectAll" resultMap="OneToMany">
select c.id cid,c.name cname,s.id sid,s.name sname,s.age sage
from classes c,student s where s.cid=c.id;
</select>

</mapper>

Test类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//一对多
@Test
public voidOtmSelectAll(){
SqlSession sqlSession = SessionUtils.getSqlsession();

OtmMapper mapper = sqlSession.getMapper(OtmMapper.class);
List<Classes> classes = mapper.OtmSelectAll();

for(Classes c : classes) {
System.out.println(c.getId()+"--"+c.getName());
List<Student> students = c.getStudents();
for(Student s : students) {
System.out.println(s);
}
System.out.println("================");
}

sqlSession.close();
}

执行结果:
1–黑马一班
Student{id=1, name=’张三’, age=23}
Student{id=2, name=’李四’, age=24}
================
2–黑马二班
Student{id=3, name=’王五’, age=25}
Student{id=4, name=’马六’, age=26}
================


3.多对多

mtm.jpg

Untitled

表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
create table student(
id int primary key auto_increment,
name varchar(30),
age int,
cid int,
constraint cs_fk foreign key (cid) references classes(id)
);
insert into student values (null,'张三',23,1);
insert into student values (null,'李四',24,1);
insert into student values (null,'王五',25,2);
insert into student values (null,'马六',26,2);

create table course(
id int primary key auto_increment,
name varchar(30)
);
insert into course values (null,'语文');
insert into course values (null,'数学');

create table stu_cr(
id int primary key auto_increment,
sid int,
cid int,
constraint sc_fk1 foreign key (sid) references student(id),
constraint sc_fk2 foreign key (cid) references course(id)
);
insert into stu_cr values (null,1,1),(null,1,2),(null,2,1),(null,2,2);

pojo类:

  • Student
    1
    2
    3
    4
    5
    privateInteger id;
    privateString name;
    privateInteger age;

    privateList<Course> courses;//多对多时增加一个属性
  • Course
    1
    2
    privateInteger id;
    privateString name;

interface接口:

1
2
3
public interfaceMtmMapper {
List<Student> mtmSelectAll();
}

mapper映射:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<mapper namespace="com.itheima.mapper.MtmMapper">
<resultMap id="manyToMany" type="student">
<id column="sid" property="id"/>
<result column="sname" property="name"/>
<result column="sage" property="age"/>

<collection property="courses" ofType="course">
<id column="cid" property="id"/>
<result column="cname" property="name"/>
</collection>
</resultMap>

<select id="mtmSelectAll" resultMap="manyToMany">
select sc.sid,s.name sname,s.age sage,sc.cid,c.name cname
from student s,course c,stu_cr sc where sc.cid=c.id and sc.sid=s.id;
</select>
</mapper>

Test类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//多对多
@Test
public voidmtmSelectAll(){
SqlSession sqlSession = SessionUtils.getSqlsession();

MtmMapper mapper = sqlSession.getMapper(MtmMapper.class);
List<Student> stu = mapper.mtmSelectAll();

for(Student s : stu) {
System.out.println(s.getId()+"--"+s.getName()+"--"+s.getAge());
List<Course> cos = s.getCourses();
for(Course c : cos) {
System.out.println(c);
}
System.out.println("================");
}

sqlSession.close();
}

运行结果:
1–张三–23
Course{id=1, name=’语文’}
Course{id=2, name=’数学’}
================
2–李四–24
Course{id=1, name=’语文’}
Course{id=2, name=’数学’}


4.小结

Untitled

Untitled

注意:本多表操作以查询作为演示,表中的外键字段并没有在实体类中体现。


二. MyBatis 注解实现多表操作

1.一对一

Untitled

  • pojo类 Card
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    public classCard {
    privateInteger id;
    privateString number;

    privatePerson p;

    publicCard() {
    }

    publicCard(Integer id, String number, Person p) {
    this.id = id;
    this.number = number;
    this.p = p;
    }

    publicInteger getId() {
    returnid;
    }

    public voidsetId(Integer id) {
    this.id = id;
    }

    publicString getNumber() {
    returnnumber;
    }

    public voidsetNumber(String number) {
    this.number = number;
    }

    publicPerson getP() {
    returnp;
    }

    public voidsetP(Person p) {
    this.p = p;
    }

    @Override
    publicString toString() {
    return"Card{" +
    "id=" + id +
    ", number='" + number + '\'' +
    ", p=" + p +
    '}';
    }
    }

    Person
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    public classPerson {
    privateInteger id;
    privateString name;
    privateInteger age;

    publicPerson() {
    }

    publicPerson(Integer id, String name, Integer age) {
    this.id = id;
    this.name = name;
    this.age = age;
    }

    publicInteger getId() {
    returnid;
    }

    public voidsetId(Integer id) {
    this.id = id;
    }

    publicString getName() {
    returnname;
    }

    public voidsetName(String name) {
    this.name = name;
    }

    publicInteger getAge() {
    returnage;
    }

    public voidsetAge(Integer age) {
    this.age = age;
    }

    @Override
    publicString toString() {
    return"Person{" +
    "id=" + id +
    ", name='" + name + '\'' +
    ", age=" + age +
    '}';
    }
    }

interface接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public interface CardMapper {
@Select("select * from card")
@Results({
@Result(column = "id",property = "id"),
@Result(column = "number",property = "number"),
@Result(
property = "p", //被包含对象的变量名
javaType = Person.class, //被包含对象的实际数据类型
column = "pid", //根据查询出的card表中的pid字段来查询person表
/**
* one、@One 一对一固定写法
* select属性 指定调用哪个接口中的哪个方法
*/
one=@One(select = "com.itheima.mapper.PersonMapper.selectById")
)
})
List<Card> selectAll();
}

public interfacePersonMapper {
@Select("select * from person where id=#{id}")
Person selectById(Integer id);
}

mapper映射文件(省略)

Test类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//注解一对一查询所有
@Test
public voidselectAll(){
SqlSession sqlSession = SessionUtils.getSqlsession();

CardMapper mapper = sqlSession.getMapper(CardMapper.class);
List<Card> cards = mapper.selectAll();

for(Card c : cards) {
System.out.println(c);
}

sqlSession.close();
}

查询结果:

Card{id=1, number=’12345’, p=Person{id=1, name=’张三’, age=23}}
Card{id=2, number=’23456’, p=Person{id=2, name=’李四’, age=24}}
Card{id=3, number=’34567’, p=Person{id=3, name=’王五’, age=25}}


2.一对多

Untitled

  • pojo类 Classes
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    public classClasses {
    privateInteger id;
    privateString name;

    privateList<Student> students;

    publicClasses() {
    }

    publicClasses(Integer id, String name, List<Student> students) {
    this.id = id;
    this.name = name;
    this.students = students;
    }

    publicInteger getId() {
    returnid;
    }

    public voidsetId(Integer id) {
    this.id = id;
    }

    publicString getName() {
    returnname;
    }

    public voidsetName(String name) {
    this.name = name;
    }

    publicList<Student> getStudents() {
    returnstudents;
    }

    public voidsetStudents(List<Student> students) {
    this.students = students;
    }

    @Override
    publicString toString() {
    return"Classes{" +
    "id=" + id +
    ", name='" + name + '\'' +
    ", students=" + students +
    '}';
    }
    }
    Student
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    public classStudent {
    privateInteger id;
    privateString name;
    privateInteger age;

    //private List<Course> courses; //多对多时增加一个属性

    publicStudent() {
    }

    publicStudent(Integer id, String name, Integer age) {
    this.id = id;
    this.name = name;
    this.age = age;
    }

    /*public Student(Integer id, String name, Integer age, List<Course> courses) {
    this.id = id;
    this.name = name;
    this.age = age;
    this.courses = courses;
    }*/

    /*public List<Course> getCourses() {
    return courses;
    }

    public void setCourses(List<Course> courses) {
    this.courses = courses;
    }*/

    publicInteger getId() {
    returnid;
    }

    public voidsetId(Integer id) {
    this.id = id;
    }

    publicString getName() {
    returnname;
    }

    public voidsetName(String name) {
    this.name = name;
    }

    publicInteger getAge() {
    returnage;
    }

    public voidsetAge(Integer age) {
    this.age = age;
    }

    @Override
    publicString toString() {
    return"Student{" +
    "id=" + id +
    ", name='" + name + '\'' +
    ", age=" + age +
    '}';
    }
    }

interface接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public interfaceClassesMapper {
//查询全部
@Select("select * from classes")
@Results({
@Result(column = "id",property = "id"),
@Result(column = "name",property = "name"),
@Result(
property = "students",//被包含对象的变量名
javaType = List.class,//被包含对象的实际数据类型
column = "id",//根据classes查询出的id字段来查询student表
/**
* many、@Many一对多固定写法
* select属性 指定调用哪个接口的哪个方法
*/
many =@Many(select = "com.itheima.mapper.StudentMapper.selectByCid")
)
})
List<Classes> otmSelectAll();
}

public interface StudentMapper {
//根据cid查询student表
@Select("select * from student where cid = #{cid}")
List<Student> selectByCid(Integer cid);
}

mapper映射文件(省略)

Test类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//注解一对多查询所有
@Test
public voidselectAll2() {
SqlSession sqlSession = SessionUtils.getSqlsession();

ClassesMapper mapper = sqlSession.getMapper(ClassesMapper.class);
List<Classes> classes = mapper.otmSelectAll();

for(Classes c : classes) {
System.out.println(c.getId()+"--"+c.getName());
List<Student> students = c.getStudents();
for(Student s : students) {
System.out.println("\t"+s);
}
}

sqlSession.close();
}

查询结果:
1–黑马一班
Student{id=1, name=’张三’, age=23}
Student{id=2, name=’李四’, age=24}
2–黑马二班
Student{id=3, name=’王五’, age=25}
Student{id=4, name=’马六’, age=26}


3.多对多

Untitled

  • pojo类 Course
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    public classCourse {
    privateInteger id;
    privateString name;

    publicCourse() {
    }

    publicCourse(Integer id, String name) {
    this.id = id;
    this.name = name;
    }

    publicInteger getId() {
    returnid;
    }

    public voidsetId(Integer id) {
    this.id = id;
    }

    publicString getName() {
    returnname;
    }

    public voidsetName(String name) {
    this.name = name;
    }

    @Override
    publicString toString() {
    return"Course{" +
    "id=" + id +
    ", name='" + name + '\'' +
    '}';
    }
    }

    Student
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    public classStudent {
    privateInteger id;
    privateString name;
    privateInteger age;

    privateList<Course> courses;//多对多时增加一个属性

    publicStudent() {
    }

    publicStudent(Integer id, String name, Integer age) {
    this.id = id;
    this.name = name;
    this.age = age;
    }

    /*public Student(Integer id, String name, Integer age, List<Course> courses) {
    this.id = id;
    this.name = name;
    this.age = age;
    this.courses = courses;
    }*/

    /*public List<Course> getCourses() {
    return courses;
    }

    public void setCourses(List<Course> courses) {
    this.courses = courses;
    }*/

    publicInteger getId() {
    returnid;
    }

    public voidsetId(Integer id) {
    this.id = id;
    }

    publicString getName() {
    returnname;
    }

    public voidsetName(String name) {
    this.name = name;
    }

    publicInteger getAge() {
    returnage;
    }

    public voidsetAge(Integer age) {
    this.age = age;
    }

    @Override
    publicString toString() {
    return"Student{" +
    "id=" + id +
    ", name='" + name + '\'' +
    ", age=" + age +
    '}';
    }
    }

interface接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public interface MtmStudentMapper {
//查询student所有
@Select("select distinct s.id,s.name,s.age from student s,stu_cr sc where sc.sid = s.id")
@Results({
@Result(column = "id",property = "id"),
@Result(column = "name",property = "name"),
@Result(column = "age",property = "age"),
@Result(
property = "courses", //被包含对象的变量名
javaType = List.class, //被包含对象的实际数据类型
column = "id", //根据查询出的student的id作为关联条件 ,去查询中间表和课程表
/**
* many、@Many 一对多固定写法 (中间表一对多)
* select属性 指定调用哪个接口中的哪个方法
*/
many=@Many(select = "com.itheima.mapper.CourseMapper.selectById")
),
})
List<Student> mtmSelectAll();
}

public interface CourseMapper {
//根据查询出的学生id查询课程信息
@Select("select c.id,c.name from stu_cr sc,course c where sc.cid = c.id and sc.sid = #{id}")
List<Course> selectById(Integer id);
}

mapper映射文件(省略)

Test类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//注解多对多查询所有
@Test
public voidselectAll3() {
SqlSession sqlSession = SessionUtils.getSqlsession();

MtmStudentMapper mapper = sqlSession.getMapper(MtmStudentMapper.class);
List<Student> stu = mapper.mtmSelectAll();

for(Student s : stu) {
System.out.println(s.getId()+"--"+s.getName()+"--"+s.getAge());
List<Course> courses = s.getCourses();
for(Course c: courses) {
System.out.println("\t"+c);
}
}

sqlSession.close();
}

查询结果:

       1--张三--2
       Course{id=1, name='语文'}
       Course{id=2, name='数学'}
        2--李四--24
       Course{id=1, name='语文'}
       Course{id=2, name='数学'}

4.小结

Untitled

Untitled

注意:本多表操作以查询作为演示,表中的外键字段并没有在实体类中体现。


三. 构建SQL

1.基本介绍

Untitled

2.代码示例:

Untitled

  • pojo类:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    public classStudent {
    privateInteger id;
    privateString name;
    privateInteger age;

    publicStudent() {
    }

    publicStudent(Integer id, String name, Integer age) {
    this.id = id;
    this.name = name;
    this.age = age;
    }

    publicInteger getId() {
    returnid;
    }

    public voidsetId(Integer id) {
    this.id = id;
    }

    publicString getName() {
    returnname;
    }

    public voidsetName(String name) {
    this.name = name;
    }

    publicInteger getAge() {
    returnage;
    }

    public voidsetAge(Integer age) {
    this.age = age;
    }

    @Override
    publicString toString() {
    return"Student{" +
    "id=" + id +
    ", name='" + name + '\'' +
    ", age=" + age +
    '}';
    }
    }

SqlReturn类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public classSqlReturn {
//返回查询SQL语句
publicString getSelectAll(){
return newSQL(){
{
SELECT("*");
FROM("student");
}
}.toString();
}

//返回插入SQL语句
publicString getInsert(){
return newSQL(){
{
INSERT_INTO("student");
INTO_VALUES("#{id},#{name},#{age}");
}
}.toString();
}

//返回修改SQL语句
//加条件判断 达到动态修改的效果
publicString getUpdate(Student stu){
return newSQL(){
{
UPDATE("student");

//加条件判断 达到动态修改的效果
if(stu.getName()!=null){
SET("name=#{name}");
}
if(stu.getAge()!=null){
SET("age=#{age}");
}

WHERE("id=#{id}");
}
}.toString();
}

//返回删除SQL语句
publicString getDelete(){
return newSQL(){
{
DELETE_FROM("student");
WHERE("id=#{id}");
}
}.toString();
}
}

接口StudentMapper类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public interfaceStudentMapper {
//@Select("select * from student")
@SelectProvider(type = SqlReturn.class,method = "getSelectAll")
List<Student> getSelectAll();

//@Insert("insert into student values(#{id},#{name},#{age})")
@InsertProvider(type = SqlReturn.class,method = "getInsert")
Integer inster(Student stu);

//@Update("update student set name=#{name},age=#{age} where id=#{id}")
@UpdateProvider(type = SqlReturn.class,method = "getUpdate")
Integer update(Student stu);

//@Delete("delete from student where id=#{id}")
@DeleteProvider(type = SqlReturn.class,method = "getDelete")
Integer delete(Integer id);
}

3.小结

2.jpg