视频观看地址:http://edu.51cto.com/course/14674.html?source=so
1、动态sql
1.1、if
需求:查询全部用户,如果用户填写了姓名,按照姓名的模糊查询,如果用户填写了姓名和年龄,则按姓名和年龄一起查找
1、接口定义
public List querybyUser(User vo) throws Exception;
2、mapper.xml配置
3、测试方法
@Test
public void testIf()throws Exception{
User user = new User();
user.setUserName(null);
user.setAge(17);
List list = userMapper.querybyUser(user);
for (User u : list) {
System.out.println(u);
}
}
4、日志
DEBUG - Opening JDBC Connection
DEBUG - Created connection 276327391.
DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@10786bdf]
DEBUG - ==> Preparing: select * from tb_user where 1=1 and age =?
DEBUG - ==> Parameters: 17(Integer)
DEBUG - <== Total: 1
User [userid=10, userName=阿珂3, pwd=123456, age=17, sex=女, birthday=Mon Aug 13 14:57:50 CST 2018]
DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@10786bdf]
DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@10786bdf]
DEBUG - Returned connection 276327391 to pool.
1.2、where
在前面的sql中的我们认为的定义了一个where 1=1这样的东西,此时我们可以通过mybatis的where标签来搞定
where :自动根据需求生成,并且可以剔除不需要and
直接测试即可
1.3、foreach
另外一个动态 SQL 通用的必要操作是迭代一个集合, 通常是构建在 IN 条件中的。
接口方法定义
public List querybyIn(QueryUser vo) throws Exception;
mapper.xml
测试方法
@Test
public void testforeach()throws Exception{
QueryUser queryUser= new QueryUser();
queryUser.setUserids(new int[]{8,9,2,3});
List list = userMapper.querybyIn(queryUser);
for (User u : list) {
System.out.println(u);
}
}
日志
DEBUG - Opening JDBC Connection
DEBUG - Created connection 1060925979.
DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@3f3c721b]
DEBUG - ==> Preparing: select * from tb_user WHERE userid in ( ? , ? , ? , ? )
DEBUG - ==> Parameters: 8(Integer), 9(Integer), 2(Integer), 3(Integer)
DEBUG - <== Total: 4
User [userid=8, userName=阿珂, pwd=123456, age=18, sex=女, birthday=Mon Aug 13 14:06:14 CST 2018]
User [userid=9, userName=阿珂2, pwd=123456, age=18, sex=女, birthday=Mon Aug 13 14:56:45 CST 2018]
User [userid=2, userName=张三, pwd=123456, age=10, sex=男, birthday=Mon Aug 13 10:07:55 CST 2018]
User [userid=3, userName=李四, pwd=123456, age=10, sex=男, birthday=Mon Aug 13 10:07:55 CST 2018]
DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@3f3c721b]
DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@3f3c721b]
DEBUG - Returned connection 1060925979 to pool.
1.4、Set
set 元素可以被用于动态包含更新的列,而不包含不需更新的(动态更新)
接口定义
public int dycUpdate(User vo) throws Exception;
mapper.xml定义
update tb_user
user_name=#{userName},
pwd=#{pwd},
age=#{age},
sex=#{sex},
birthday=#{birthday},
where userid=#{userid}
测试方法
@Test
public void testdycUpdate()throws Exception{
User user = new User();
user.setUserName("hello");
user.setSex("男");
user.setBirthday(new Date());
user.setUserid(8);
userMapper.dycUpdate(user);
sqlSession.commit();
}
日志
DEBUG - Opening JDBC Connection
DEBUG - Created connection 340839523.
DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@1450cc63]
DEBUG - ==> Preparing: update tb_user SET user_name=?, sex=?, birthday=? where userid=?
DEBUG - ==> Parameters: hello(String), 男(String), 2018-08-14 10:19:19.311(Timestamp), 8(Integer)
DEBUG - <== Updates: 1
DEBUG - Committing JDBC Connection [oracle.jdbc.driver.T4CConnection@1450cc63]
DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@1450cc63]
DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@1450cc63]
DEBUG - Returned connection 340839523 to pool.
1.5、动态添加
接口定义
public int dycInsert(User vo) throws Exception;
mapper.xml文件
insert into tb_user (
userid,
user_name,
pwd,
age,
sex,
birthday,
) values(
seq_user.nextval,
#{userName},
#{pwd},
#{age},
#{sex},
#{birthday},
)
测试方法
@Test
public void testdycInsert()throws Exception{
User user = new User();
user.setUserName("yui");
user.setSex("男");
user.setBirthday(new Date());
user.setUserid(8);
userMapper.dycInsert(user);
sqlSession.commit();
}
日志
DEBUG - Opening JDBC Connection
DEBUG - Created connection 1341177488.
DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@4ff0be90]
DEBUG - ==> Preparing: insert into tb_user ( userid, user_name, sex, birthday ) values( seq_user.nextval, ?, ?, ? )
DEBUG - ==> Parameters: yui(String), 男(String), 2018-08-14 10:31:06.973(Timestamp)
DEBUG - <== Updates: 1
DEBUG - Committing JDBC Connection [oracle.jdbc.driver.T4CConnection@4ff0be90]
DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@4ff0be90]
DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@4ff0be90]
DEBUG - Returned connection 1341177488 to pool.
1.6、choose、when、otherwise
有时我们不想应用所有的条件, 相反我们想选择很多情况下的一种。 Java 中的 switch 和 语句相似,MyBatis 提供 choose 元素。
需求:查询所有用户
1.如果用户填写了性别和姓名,则按姓名查找
2.如果用户填写了姓名,按姓名查找
3.如果用户未填写姓名,但是填写了性别,则按性别查找
接口声明
public List queryDyc(QueryUser vo) throws Exception;
mapper.xml文件
测试方法
@Test
public void testIfelse()throws Exception{
QueryUser queryUser= new QueryUser();
queryUser.setName(null);
queryUser.setSex(null);
List list = userMapper.queryDyc(queryUser);
for (User u : list) {
System.out.println(u);
}
}
日志
DEBUG - Opening JDBC Connection
DEBUG - Created connection 480779844.
DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@1ca81e44]
DEBUG - ==> Preparing: select * from tb_user WHERE age>5
DEBUG - ==> Parameters:
DEBUG - <== Total: 7
User [userid=8, userName=hello, pwd=123456, age=18, sex=男, birthday=Tue Aug 14 10:19:19 CST 2018]
User [userid=9, userName=阿珂2, pwd=123456, age=18, sex=女, birthday=Mon Aug 13 14:56:45 CST 2018]
User [userid=10, userName=阿珂3, pwd=123456, age=17, sex=女, birthday=Mon Aug 13 14:57:50 CST 2018]
User [userid=2, userName=张三, pwd=123456, age=10, sex=男, birthday=Mon Aug 13 10:07:55 CST 2018]
User [userid=3, userName=李四, pwd=123456, age=10, sex=男, birthday=Mon Aug 13 10:07:55 CST 2018]
User [userid=4, userName=王五, pwd=123456, age=10, sex=男, birthday=Mon Aug 13 10:07:55 CST 2018]
User [userid=5, userName=赵六, pwd=123456, age=10, sex=男, birthday=Mon Aug 13 10:07:55 CST 2018]
DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@1ca81e44]
DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@1ca81e44]
DEBUG - Returned connection 480779844 to pool.
1.7、sql片段
sql片段是为了将一些常用的sql语句保存起来,在需要的时候进行引用即可
如果sql片段和statement在同一个命名空间下,则直接引用
select * from tb_user
如果sql片段和statement不在同一个命名空间下,则直接则使用命名空间.sqlid的形式进行引用