Mybatis
第三章:
-
动态代理: 使用SqlSession.getMapper(dao接口.class) 获取这个dao接口的对象
-
传入参数: 从java代码中把数据传入到mapper文件的sql语句中。
1)parameterType : 写在mapper文件中的 一个属性。 表示dao接口中方法的参数的数据类型。
例如StudentDao接口
public Student selectStudentById(Integer id)2) 一个简单类型的参数: 简单类型: mybatis把java的基本数据类型和String都叫简单类型。 在mapper文件获取简单类型的一个参数的值,使用 #{任意字符}
接口:public Student selectStudentById(Integer id)
mapper:select id,name, email,age from student where id=#{studentId}
3) 多个参数,使用@Param命名参数
接口 public List<Student> selectMulitParam(@Param("myname") String name, @Param("myage") Integer age)
使用 @Param("参数名") String name
mapper文件:
<select>
select * from student where name=#{myname} or age=#{myage}
</select>
4) 多个参数,使用java对象
语法 #{属性名}
vo: value object , 放一些存储数据的类。比如说 提交请求参数, name ,age
现在想把name ,age 传给一个service 类。
vo: view object , 从servlet把数据返回给浏览器使用的类,表示显示结果的类。
pojo: 普通的有set, get方法的java类。 普通的java对象
Servlet --- StudentService( addStudent( MyParam param) )
entity(domain域): 实体类, 和数据库中的表对应的类,
5) # 和 $
select id,name, email,age from student where id=#{studentId}
# 的结果: select id,name, email,age from student where id=?
select id,name, email,age from student where id=${studentId}
$ 的结果:select id,name, email,age from student where id=1001
String sql="select id,name, email,age from student where id=" + "1001";
使用的Statement对象执行sql, 效率比PreparedStatement低。
$:可以替换表名或者列名, 你能确定数据是安全的。可以使用$
# 和 $区别
1. #使用 ?在sql语句中做站位的, 使用PreparedStatement执行sql,效率高
2. #能够避免sql注入,更安全。
3. $不使用占位符,是字符串连接方式,使用Statement对象执行sql,效率低
4. $有sql注入的风险,缺乏安全性。
5. $:可以替换表名或者列名
-
mybatis的输出结果
mybatis执行了sql语句,得到java对象。1)resultType结果类型, 指sql语句执行完毕后, 数据转为的java对象, java类型是任意的。
resultType结果类型的它值 1. 类型的全限定名称 2. 类型的别名, 例如 java.lang.Integer别名是int处理方式: 1. mybatis执行sql语句, 然后mybatis调用类的无参数构造方法,创建对象。 2. mybatis把ResultSet指定列值付给同名的属性。 <select id="selectMultiPosition" resultType="com.bjpowernode.domain.Student"> select id,name, email,age from student </select> 对等的jdbc ResultSet rs = executeQuery(" select id,name, email,age from student" ) while(rs.next()){ Student student = new Student(); student.setId(rs.getInt("id")); student.setName(rs.getString("name")) }
- 定义自定义类型的别名
1)在mybatis主配置文件中定义,使定义别名
2)可以在resultType中使用自定义别名
- 定义自定义类型的别名
3)resultMap:结果映射, 指定列名和java对象的属性对应关系。
1)你自定义列值赋值给哪个属性
2)当你的列名和属性名不一样时,一定使用resultMap
resultMap和resultType不要一起用,二选一
dao接口中的方法
/**
* 演示使用ResultMap
* @return List<Student>
*/
List<Student> selectStudents();
StudentDao.xml
<!--使用ResultMap 必须先定义ResultMap
可以防止实体类中的属性名与数据库表中的列名的不同的囧境-->
<!--定义ResultMap-->
<resultMap id="StudentMap" type="com.dyw.Domain.Student">
<!--主键列使用 id标签
column 表的列名
property java类型的属性名
-->
<id column="id" property="id"/>
<!--非主键列使用result标签-->
<result column="name" property="name"/>
<result column="age" property="age"/>
</resultMap>
<select id="selectStudents" resultMap="StudentMap">
select id,name,age from student order by id;
</select>
输出:
注意:如果Dao接口使用Map作为返回结果的话 需要在ResultType中配置Map的全限定名称 而不是实体类的名称(如果返回值为List则依旧使用实体类的全限定名称) 并且返回的结果是以列名作为键 列值作为值
第四章动态sql
模糊查询:
**like:**在dao.xml配置文件中 直接使用like即可 当使用#{}拼接时 我们前面说过 他是以占位符?(即preparedStatement)所以格式为
<!--模糊查询的两种方式
1.直接传入一个字符串 类似与%丁%
2.使用字符串拼接
-->
<select id="selectStudentsByLike" resultType="com.dyw.Domain.Student">
select id,name,age from student where name like "%" #{name} "%"
</select>
底层为
<!--Preparing: select id,name,age from student where name like "%" ? "%"-->
但是使用${}时不一样它是使用Statement即字符串拼接 所以格式为
<select id="selectStudentsByLike" resultType="com.dyw.Domain.Student">
select id,name,age from student where name like "%${name}%"
</select>
底层
<!--Preparing: select id,name,age from student where name like "%丁%"-->
动态sql: sql的内容是变化的,可以根据条件获取到不同的sql语句。
主要是where部分发生变化。
动态sql的实现,使用的是mybatis提供的标签, ,,
1)是判断条件的,
语法
部分sql语句
<!--使用标签<if>可以判断条件 如果条件满足要求则将条件拼接在where后 可以防止sql运行时报错-->
<!-- 多个if拼接的时候一般在where后面加一个1=1 保证拼接时不会出错
注意使用动态sql语句 一定要传java对象作为参数
-->
<select id="selectStudentsByLike" resultType="com.dyw.Domain.Student">
select id,name,age from student
where 1=1
<if test="name !=null and name != ''">
and name like #{name}
</if>
<if test="age > 0">
and age > #{age}
</if>
</select>
但这样有弊端,就是为了避免拼接错误,需要使用or 1=1, 但是这样又会导致另一个错误,那就是可以会将所有的内容都查出来,这是我们不想看到的。
2) 用来包含 多个的, 当多个if有一个成立的, 会自动增加一个where关键字,
并去掉 if中多余的 and ,or等。
<!--使用where标签时就不会出现拼接错误现象 底层会自动判别然后进行拼接 可以自动去除and or等连接词比直接用<if>安全性好-->
<select id="selectStudentsByLike" resultType="com.dyw.Domain.Student">
select id,name,age from student
<where>
<if test="name!=null and name !=''">
and name like #{name}
</if>
<if test="age > 0">
and age >#{age}
</if>
</where>
</select>
使用< where>标签就不会出现上述< if>的情况 < where>标签可以智能帮助我们去除and or 等 防止拼接错误
3) 循环java中的数组,list集合的。 主要用在sql的in语句中。(用于当传入的参数为集合时 为了获取里面的数据)
学生id是 1001,1002,1003的三个学生
select * from student where id in (1001,1002,1003)
public List<Student> selectFor(List<Integer> idlist)
List<Integer> list = new ...
list.add(1001);
list.add(1002);
list.add(1003);
dao.selectFor(list)
<foreach collection="" item="" open="" close="" separator="">
#{xxx}
</foreach>
collection:表示接口中的方法参数的类型, 如果是数组使用array , 如果是list集合使用list
item:自定义的,表示数组和集合成员的变量
open:循环开始是的字符
close:循环结束时的字符
separator:集合成员之间的分隔符
4)sql代码片段, 就是复用一些语法
步骤
1.先定义 sql语句, 表名,字段等
2.再使用,