MyBatis的学习(五):动态Sql语句及其他拓展
动态Sql
使用的是mybatis的标签,<if>,<\where>,<foreach>
<if>
当test的值为true的时候,会将其包含的sql片段拼接到所在的sql语句中。
语法:
<if test='判断java对象的属性值'>
部分sql语句
</if>
举例子:
- 创建dao
/**
* 使用if语句
* @param student
* @return
*/
List<Student> selectStuInIf(Student student);
- mapper文件
<!-- 动态sql的if-->
<!--第二种模糊查询-->
<select id="selectStuInIf" resultType="com.liang.entity.Student">
select id,name,email,age from student
<!--加上id > 0是为了防止age条件不满足造成语法缺失-->
where id > 0
<if test="name != null and name != ''">
name = #{name}
</if>
<if test="age > 0">
and age > #{age}
</if>
</select>
- 测试方法
@Test
public void testSelectStuInIf(){
//获取SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//动态代理获取dao的实体类
StudentDao dao = sqlSession.getMapper(StudentDao.class);
//执行语句
Student stu = new Student();
stu.setName("李四");
stu.setAge(18);
List<Student> students = dao.selectStuInIf(stu);
//输出结果
for (Student s:students) {
System.out.println("查询的学生是:"+s);
}
sqlSession.close();
}
- 测试结果
<where>
<!--
语法格式:<where><if>...</if></where>
-->
<select id="selectStuInIf" resultType="com.liang.entity.Student">
select id,name,email,age from student
<where>
<if test="name != null and name != ''">
name = #{name}
</if>
<if test="age > 0">
and age > #{age}
</if>
</where>
</select>
如果条件符合会直接拼接,如果不符合条件,会去掉多余的拼接或者无效字符。
<foreach>
用来循环Java中的数组。list集合的,主要用在sql的in语句。
语法格式:
<foreach collection="" item="" open="" separator="">
</foreach>
- collection:表示接口中的方法参数的类型,如果是数组就用arry,如果是list集合就用list
- item:自定义的,表示数组和集合成员的变量
- open:循环开始时候的字符
- separator:集合成员之间的分隔符
举例子:
- dao接口
/**
* 使用foreach
* @param integerList
* @return
*/
List<Student> selectStuForEach(List<Integer> integerList);
- mapper文件
<!--foreach的使用-->
<select id="selectStuForEach" resultType="com.liang.entity.Student">
select * from student
<foreach collection="list" item="myid" open="(" close=")" separator=",">
#{myid}
</foreach>
</select>
- 测试方法
@Test
public void testSelectStuForEach(){
//获取SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//动态代理获取dao的实体类
StudentDao dao = sqlSession.getMapper(StudentDao.class);
//执行语句
List<Integer> list = new ArrayList<>();
list.add(1001);
list.add(1002);
list.add(1003);
List<Student> students = dao.selectStuForEach(list);
//输出结果
for (Student s:students) {
System.out.println("查询的学生是:"+s);
}
sqlSession.close();
}
- 测试结果
除了可以直接赋值,还可以传入一个对象进行赋值!
其中stu是一个Student的对象。
那么赋值的时候使用stu.id进行赋值!
<!--foreach的使用-->
<select id="selectStuForEach" resultType="com.liang.entity.Student">
select * from student
<foreach collection="list" item="stu" open="(" close=")" separator=",">
#{stu.id}
</foreach>
</select>
代码片段
主要就是用来复用一些语法:
步骤:
-
先定义
-
再使用
<!--定义sql片段-->
<sql id="studentSql">
select id,name,email,age from student
</sql>
<!--使用sql的复用-->
<select id="" resultType="com.liang.entity.Student">
<include refid="studentSql"></include>
where id in
<foreach collection="list" item="myid" open="(" close=")" separator=",">
#{myid}
</foreach>
</select>
Mybatis配置文件(了解)
-
<settings>:设置mybatis的行为
-
<typeAliases>:设置别名
-
<environments>:一个数据库的信息配置,环境
-
<transactionManager>:mybatis的事务类型
type : 表示事务处理的类型- JDBC:JDBC(表示使用的Connection对象的commit,rollback事务处理)
- MANAGED:把mybatis的事务处理交给其他容器(一个服务软件,或者一个框架)
-
<dataSource>:表示数据源,连接数据库的
type:表示数据类型- POOLED表示使用连接池,mybatis会创建PooledDataSources类
- UPOOLED:不使用连接池,在每次执行sql语句,先创建连接,执行sql语句,再关闭连接,mybatis会创建一个UPooledSDataSource,管理Connection对象的使用
数据库的属性配置文件
把数据库连接信息放到一个单独的文件中,和mybatis主配置文件分开。
目的是便于修改,保存,处理多个数据库的信息。
-
在resources目录下定义一个属性配置文件,xxx.properties
属性配置文件中,定义数据,格式是key=value
key:一般使用.做多级目录
例如:jdbc.driver=com.mysql.jdbc.Driver
-
在mybatis的主配置文件中使用<proprtty>指定文件的位置,在需要值的地方,${key}
举例子:
- 创建properties文件
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/springdb?characterEncoding=UTF8&useSSL=false
jdbc.user = root
jdbc.password = 001204
- 在主配置文件下的configuration中添加
<!--指定数据库文件位置-->
<properties resource="db.properties"/>
- 代替值
<property name="driver" value="${jdbc.driver}"/>
<!--连接数据库的url-->
<property name="url" value="${jdbc.url}"/>
<!--用户名-->
<property name="username" value="${jdbc.user}"/>
<!--密码-->
<property name="password" value="${jdbc.password}"/>
指定多种mapper
方法一:在mapper中编写
<mappers>
<!--一个mapper标签指定一个文件的位置,从类路径开始的路径信息-->
<mapper resource="com/liang/dao/StudentDao.xml"/>
<mapper resource="com/liang/dao/OrderDao.xml"/>
</mappers>
方式二:使用包名
<mappers>
<!--方式二:使用包名,mapper文件所在的包,这个包的所有xml文件会一次性导入
要求:1. mapper文件的名称需要和接口名称一样,区分大小写
2. mapper文件和dao接口需要在同一个目录
-->
<package name="com.liang.dao"/>
</mappers>
PageHelper
帮助进行数据分页的!!!
- 在maven的pom文件中加入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
- 在主配置文件的environment前加上
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
- dao接口
/**
* 使用分页
* @return
*/
List<Student> selectAll();
- mapper
<!--分页插件的使用-->
<select id="selectAll" resultType="com.liang.entity.Student">
select * from student order by id
</select>
- 测试方法
@Test
public void testSelectAll(){
//获取SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//动态代理获取dao的实体类
StudentDao dao = sqlSession.getMapper(StudentDao.class);
//加入分页方法
//pageNum:第几页,第一页开始
//pageSize:一页中有多少行数据
PageHelper.startPage(1,3);
List<Student> students = dao.selectAll();
//输出结果
for (Student s:students) {
System.out.println("查询的学生是:"+s);
}
sqlSession.close();
}