动态sql作用:动态sql就相当于是在拼接SQL语句。
动态sql常用的标签:
if
choose(when otherwise)
trim where set
foreach
实体类属性:
private int stuId;
private String name;
private String email;
private String phone;
private int teaId;
配置文件中添加类型别名:
1.IF:通过IF来判断是否需要拼接剩余的sql语句
select * from student where tea_id=#{teaId} and name=#{name}
执行这条sql语句时,传入的参数是对象类型,通过这个对象中的tea_id参数和name参数来查询符合条件的一行或多行。不能通过在接口中方法的重载来实现动态sql。
接口中:
public List selectStu(Student student);
1
main方法中部分代码:
Student student=new Student();
student.setTeaId(2);//根据老师的id来查询学生
List list=studentMapper.selectStu(student);
for(Student stu:list)
{
System.out.println(stu);
}
因为没有设置student中的name属性,String类型的默认中是null,因此在xml文件中的if条件不成立。所有在执行是不会添加后半段sql。
运行结果:
Student{stuId=4, name=‘lisi’, email=‘[email protected]’, phone=‘1522921920x’, teaId=2}
Student{stuId=5, name=‘swj’, email=‘[email protected]’, phone=‘132016562xx’, teaId=2}
Student{stuId=6, name=‘mmq’, email=‘[email protected]’, phone=‘136987456’, teaId=2}
Student{stuId=7, name=‘lfl’, email=‘[email protected]’, phone=‘178888880’, teaId=2}
1
2
3
4
在参数student中设置name属性:
Student student=new Student();
student.setTeaId(2);//设置teaId
student.setName(“swj”);//设置name
List list=studentMapper.selectStu(student);//根据teaId和name属性来查询
for(Student stu:list)
{
System.out.println(stu);
}
运行结果:
Student{stuId=5, name=‘swj’, email=‘[email protected]’, phone=‘132016562xx’, teaId=2}
1
2.CHOOSE:类似于java中的switch-case语句。首先看数据库的查询结果
mysql> select * from student where tea_id=2;
±-------±-------±---------------±------------±-------+
| stu_id | name | email | phone | tea_id |
±-------±-------±---------------±------------±-------+
| 4 | lisi | [email protected] | 1522921920x | 2 |
| 5 | swj | [email protected] | 132016562xx | 2 |
| 6 | mmq | [email protected] | 136987456 | 2 |
| 7 | lfl | [email protected] | 178888880 | 2 |
| 9 | 静静 | [email protected] | 1522921920x | 2 |
±-------±-------±---------------±------------±-------+
5 rows in set (0.00 sec)
xml中的sql语句:
select * from student where tea_id=2 and name=#{name} and phone=#{phone} and email=#{email} limit 0,2
如果设置了多个属性,则相应的sql也会被拼接上。类似于没有break的switch-case。
Student student=new Student();
student.setPhone(“1522921920x”);
//student.setName(“静静”);
List list=studentMapper.selectStuChoose(student);
for(Student stu:list)
{
System.out.println(stu);
}
只设置一个电话属性时查出两条数据:
Student{stuId=4, name=‘lisi’, email=‘[email protected]’, phone=‘1522921920x’, teaId=2}
Student{stuId=9, name=‘静静’, email=‘[email protected]’, phone=‘1522921920x’, teaId=2}
1
2
如果取消设置姓名的注释,则在choose中会执行两条。查询结果如下:
Student{stuId=9, name=‘静静’, email=‘[email protected]’, phone=‘1522921920x’, teaId=2}
1
如果两条设置属性的语句都被注释,则会执行otherwise中的sql。在tea_id等于2的查询结果集中显示前两条数据。
Student{stuId=4, name=‘lisi’, email=‘[email protected]’, phone=‘1522921920x’, teaId=2}
Student{stuId=5, name=‘swj’, email=‘[email protected]’, phone=‘132016562xx’, teaId=2}
1
2
3.WHERE:
select * from student tea_id=#{teaId} and name=#{name} and stu_id=#{stuId}
如果三个条件中有一个条件成立,就会在sql语句中插入where子句;如果第一个条件没有成立,后面的条件有成立的,where标签会自动去除and
可以通过trim标签来定制和where元素一样的功能:
select * from student tea_id=#{teaId} and name=#{name} and stu_id=#{stuId}
当有一个条件成立时,插入前缀where;如果第一个不成立,后面有成立的,会去除and
SET:
在数据库中插入一条数据,接下来修改此条记录
mysql> insert into student values(10,‘dabaicai’,‘[email protected]’,‘110’,2);
1
现在要修改这条记录:
update student
name=#{name},
email=#{email},
phone=#{phone},
tea_id=#{teaId}
where stu_id=#{stuId}
当有一个if成立时,会插入set子句,如果最后一个if不成立时,会自动去除后面的,。
main方法:
Student student=new Student();
student.setStuId(10);//要修改id为10的信息
student.setName(“xiaobai”);//只修该名字
student.setPhone(“120”);
studentMapper.updateStu(student);
sqlSession.commit();
注意:增加、删除、修改时要加commit。否则不会执行。
同理,也可以通过trim来定制和set元素一样的功能。
update student
name=#{name},
email=#{email},
phone=#{phone},
tea_id=#{teaId},
where stu_id=#{stuId}
如果if中有一个成立,则会自动加上set子句。回去除最后一个,。
4.FOREACH:传入的参数是一个list,在sql中遍历这个list,拼接成sql。 如以下的查询语句:
select * from student where stu_id in item=“item” index=“index”> #{item}
对应的接口:
public List selectStuByIdUseForeach(List list);
1
main方法:
List list=new ArrayList(5);
list.add(1);
list.add(4);
list.add(7);
List listStu=studentMapper.selectStuByIdUseForeach(list);//查询id为1、4、7的信息
for(Student stu:listStu)
{
System.out.println(stu);
}
运行结果:
Student{stuId=1, name=‘张三’, email=‘[email protected]’, phone=‘1354641’, teaId=3}
Student{stuId=4, name=‘lisi’, email=‘[email protected]’, phone=‘1522921920x’, teaId=2}
Student{stuId=7, name=‘lfl’, email=‘[email protected]’, phone=‘178888880’, teaId=2}
插入多条数据时,也可以用foreach。foreach可以遍历所有可以叠底的对象。可以迭代的对象有集合和数组。foreach和for的区别。
插入多条数据:
insert into student(name,email,phone,tea_id) values
(#{stu.name},#{stu.email},#{stu.phone},#{stu.teaId})
接口中:
public void insertStuUseForeach(Student[] stus);
1
main方法中:
Student[] stus=new Student[3];
stus[0]=new Student(“XiaoYang”,"[email protected]",“1837619xxx4”,3);
stus[1]=new Student(“DaTou”,"[email protected]",“183xx19xxx4”,2);
stus[2]=new Student(“XiaoWang”,"[email protected]",“185xx619xxx4”,1);
studentMapper.insertStuUseForeach(stus);
sqlSession.commit();//提交事务一定要加,不然不会执行
沈阳性病医院哪家好:http://yyk.39.net/sy/zhuanke/fc844.html