动态sql的内容是变化的,可以根据条件获取到不同的sql语句,主要是where部分发生变化,实现是使用mybaits提供的标签,,,
条件判断 语法:
1 2 3 <if test ="判断Java对象的属性值" > sql语句 </if >
1 2 3 4 5 6 7 如果表中数据含name不为空,那么我就查询name为张三的人 <select id ="selectStudent" resultType ="com.zss.pojo.Student" > select * from student where <if test ="name!=null and name!=''" > name=#{name} </if > </select >
1 2 3 4 5 6 public static void main ( String[] args ) throws IOException { SqlSession sqlSession= MyBaitsUtils.getSqlSession(); StudentDAO dao=sqlSession.getMapper(StudentDAO.class); List<Student> student=dao.selectStudent("张三" ); System.out.println(student); }
标签 where标签可以用来包含if,当多个if有一个成立时,会自动增加一个where关键字,并且会去除and,or等语句,防止报错。如果多个都不符合,where语句也会自动删除
1 2 3 4 5 6 7 8 9 10 11 12 <select id ="selectStudent" resultType ="com.zss.pojo.Student" > select * from student <where > <if test ="name!=null and name!=''" > name=#{name} </if > <if test ="age>0" > and age=#{age} </if > </where > </select >
循环标签 主要使用在sql的in语句的
1 select * from where id in (1001 ,1002 ,1003 );
那么怎么传输后面的值就成为了问题
1 2 3 4 5 6 7 8 <select id ="selectForeach" resultType ="com.zss.pojo.Student" > select * from student where id in <foreach collection ="list" item ="myid" open ="(" close =")" separator ="," > #{myid} </foreach > </select >
1 2 3 4 5 6 7 8 9 10 11 public static void main ( String[] args ) throws IOException { SqlSession sqlSession= MyBaitsUtils.getSqlSession(); StudentDAO dao=sqlSession.getMapper(StudentDAO.class); List<Integer> list=new ArrayList <>(); list.add(1 ); list.add(1003 ); List<Student> studentList=dao.selectForeach(list); for (Student student:studentList){ System.out.println(student); } }
当然我们也可以传输对象,但是里面的只要
1 2 3 <foreach collection ="list" item ="myid" open ="(" close =")" separator ="," > #{student.myid} </foreach >
当然里面的的小括号也可以自己完成
1 2 3 4 5 6 7 <select id ="selectForeach" resultType ="com.zss.pojo.Student" > select * from student where id in( <foreach collection ="list" item ="myid" separator ="," > #{myid} </foreach > ) </select >
代码片段 有一些sql语句需要我们需要重复使用,一次又一次去写又太过于麻烦
先定义sql语句,表名,字段等等
在使用
1 2 3 4 5 6 7 8 9 10 11 <sql id ="studentSql" > select * from student </sql > <select id ="selectForeach" resultType ="com.zss.pojo.Student" > <include refid ="studentSql" > </include > where id in( <foreach collection ="list" item ="myid" separator ="," > #{myid} </foreach > ) </select >
数据库属性配置文件 将数据库的信息保存在一个单独的文件中,和mybaits主配置文件分开,便于修改,保存。
在resources目录中定义一个属性配置文件,xx.properties,
在配置文件中,定义数据格式为key=value
key一般使用.做多级目录的,例如jdbc.mysql.cj.
在mybaits主配置文件中使用 指定位置
记录:
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 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration > <properties resource ="jdbc.properties" /> <settings > <setting name ="logImpl" value ="STDOUT_LOGGING" /> </settings > <environments default ="development" > <environment id ="development" > <transactionManager type ="JDBC" /> <dataSource type ="POOLED" > <property name ="driver" value ="${driver}" /> <property name ="url" value ="${url}" /> <property name ="username" value ="${username}" /> <property name ="password" value ="${password}" /> </dataSource > </environment > </environments > <mappers > <mapper resource ="com/zss/dao/Student.xml" /> </mappers > </configuration >
指定多个mapper配置文件 如果有多个需要写很多行
1 2 3 4 <mappers > <mapper resource ="com/zss/dao/Student.xml" /> </mappers >
但是我们还是可以扫描包,扫描包的时候需要注意:
mapper文件名称需要和接口名称一样,区分大小写,mapper文件和dao接口在同一目录下
1 2 3 <mappers > <package name ="com.zss.dao" /> </mappers >
PageHelper 数据分页
导入jar包 1 2 3 4 5 <dependency > <groupId > com.github.pagehelper</groupId > <artifactId > pagehelper-spring-boot-starter</artifactId > <version > 1.2.13</version > </dependency >
配置插件 1 2 3 4 5 6 7 8 9 <settings > <setting name ="logImpl" value ="STDOUT_LOGGING" /> </settings > <plugins > <plugin interceptor ="com.github.pagehelper.PageInterceptor" /> </plugins >
java文件配置 1 2 3 4 5 6 7 8 9 10 public static void main ( String[] args ) throws IOException { SqlSession sqlSession= MyBaitsUtils.getSqlSession(); StudentDAO dao=sqlSession.getMapper(StudentDAO.class); PageHelper.startPage(1 ,2 ); List<Student> studentList=dao.selectStudents(); System.out.println(studentList); }
而我们最终也可以得到最终的行数等等信息