Mybatis:
ibatis:apache
2010 ibatis--> google Mybatis
MyBatis可以简化JDBC操作,实现数据的持久化。
ORM : Object Relational Mapping
将对象,表 一一映射起来
ORM:概念
Mybatis是ORM的一个实现 另外:Hibernate
orm可以使开发人员,像操作对象一样,操作数据库表。
基础方式的增删改查:
开发mybatis程序步骤:
1.配置mybatis
conf.xml:
配置数据库信息 和 需要加载的映射文件
映射文件 : 类---表
xxxMapper.xml: 增删改查标签
2.测试类: session.selectOne("需要查新的namespace.id","SQL的参数值")
MyBatis约定:输入参数parameterType和输出参数resultType,(形式上)都只能有一个 可以用数组
输入参数:
是简单类型(8个基本类型+String) 可以使用任何占位符#{xxx}
如果是对象类型,只能是对象的属性 (#{stuNo},#{stuName},#{stuAge},#{graName})
输出参数:
如果返回值类型 是对象, 无论返回一个还是多个,都写成该对象类型
注意:
如果使用的事务方式为JDBC 则需要手工提交 session.commit();
mapper动态代理方式crud
(MyBatis接口开发) 基于约定
原则,约定优于配置
硬编码方式
abc.java
Configuration conf = new Configuration();
conf.setName("myProject");
配置方式:
abc.xml
<name>myProject</name>
约定:默认值就是myProject
具体实现步骤:
1.基础环境: mybatis.jar ojdbc.jar conf.xml mapper.xml
2.用约定的方式 可以省略掉statement 直接定位出SQL语句
接口开发: 3个约定
1.方法名和mapper.xml文件中的标签的id值相同
2.法的输入参数和mapper.xml中标签的paramenterType类型一致
3.方法的返回值和mapper.xml中标签的resultType类型一致
除了以上约定,要实现接口中的方法 和 mapper.xml的标签一一对应,
namespace的值,就是接口的全类名(接口--mapper.xml)
匹配过程分析:
1.根据接口名,找到mapper.xml文件 (namespace=接口全类名)
2.根据接口的方法名,找到mapper.xml文件中的SQL标签(方法名=SQL标签中的Id值)
一般默认的约定:
SQL映射文件(mapper.xml) 和接口文件 放在一个包里 (注意修改config.xml中加载mapper文件的路径)
执行:
//反射获取到mapper接口, mapper.接口中的方法
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student = mapper.queryStudentByStuno(1);
优化:
1.可以将配置信息 单独放入db.properties文件中 然后再动态引入
db.properties
k=v
< configuration>
< properties resource="db.properties">< /properties>
< configuration>
引入之后,通过${key} 取出v
2.MyBatis全局参数
在conf.xml设置
<!-- 全局参数的配置
<settings>
<setting name="" value=""/>
<setting name="" value=""/>
</settings>
-->
3.别名: conf.xml中
定义别名
<typeAliases>
<!-- 单个别名 忽略大小写 -->
<typeAlias type="org.mybatis.entity.Student" alias="student"/>
<!-- 批量定义别名 忽略大小写 -->
<!-- 以下定义自动将包中所有类定义别名,别名就是类名 忽略大小写 -->
<package name="org.mybatis.entity"/>
</typeAliases>
除了自定义别名外,MyBatis还内置了一些常见类的别名
4.类型处理器 (类型转换器)
1.MyBatis自带一些常见的类型处理器
int -->number String-->char varchar
2.自定义MyBatis类型处理器
java类型--数据库类型
定义一个类型转换器(boolean–>number)
步骤:
1.创建一个转换器 需要实现TypeHandler接口
此接口有一个实现类,BaseTypeHandler,可以通过继承该实现类间接的实现该接口
2.配置转换器 写在别名的后面
<typeHandlers>
<typeHandler handler="org.mybatis.converter.BooleanAndIntConverter" javaType="Boolean" jdbcType="INTEGER"/>
</typeHandlers>
3.在mapper.xml中配置标签
<select id="queryStudentByStunoWithConverter" parameterType="int" resultMap="studentResult">
select * from student where stuno = #{stuno}
</select>
<resultMap type="student" id="studentResult">
<!-- 分为主键 id 和非主键 result -->
<id property="stuNo" column="stuno" />
<result property="stuName" column="stuname" />
<result property="stuAge" column="stuage" />
<result property="graName" column="graName" />
<result property="stusex" column="stusex" javaType="boolean" jdbcType = "INTEGER" />
</resultMap>
输入参数 parameterType
1.类型为简单类型(8个基本类型+String)
<select id="queryStudentByStuno" parameterType="int" resultType="student">
select * from student where stuno = #{任意值} 或者 ${value}
</select>
#{}:
会自动给String类型加上'';
可以防止SQL注入
${}:
原样输出 但是适合于动态排序(动态字段)
有被注入的风险
获取对象值:
#{}:
select * from student where stuage=#{stuAge} or stuname like #{stuName}
Student student = new Student();
student.setStuAge(20);
student.setStuName("%s%"); //手动加上%%才能进行模糊查询
List<Student> students = mapper.queryStudentBystuageOrstuName(student);
${}:
select * from student where stuage=#{stuAge} or stuname like '%${stuName}%'
Student student = new Student();
student.setStuAge(20);
student.setStuName("s");
List<Student> students = mapper.queryStudentBystuageOrstuName(student);
2.对象类型
#{属性名}
${属性名}
输入对象为HashMap:
用map中的key的值,来匹配占位符 如果匹配成功,就用map的value来替换占位符
两者都可以获取嵌套属性
<select id="queryStudentaddress" parameterType="student" resultType="student">
select * from student where homeaddress=#{address.homeAddress} or schooladdress='${address.schoolAddress}'
</select>
注意: 一般情况下,
参数作为数值时, 用#{}
select * from student where stuno = #{任意值}
参数是一个字段名时,用${}
select * from student order by ${value} desc
输出参数 resultType
1.输出参数为简单类型 queryStudentCount
2.输出参数为对象类型 queryStuByStuno
3.输出参数为对象的集合类型 queryAllStudents 映射文件中还是写对象类型
4.输出参数为HashMap
HashMap本身是一个集合,可以存放多个元素, 但是根据提示发现,返回值为HashMap时,查询的结果只能是1个学生,
一个HashMap对应的是一个学生的多个属性,接收的参数应该是多个HashMap
resultType: 一般情况下都用这个
resultMap:
实体类的属性、数据表的字段、类型、名字不同时,用resultMap
当属性名和字段名不一致时,除了使用resultMap以外,还可以使用resultType+HashMap解决:
resultMap:
<!-- 带转换器的查询 -->
<select id="queryStudentByStunoWithConverter" parameterType="int" resultMap="studentResult">
select * from student where stuno = #{stuno}
</select>
<resultMap type="student" id="studentResult">
<!-- 分为主键 id 和非主键 result column是数据库中表的字段名 -->
<id property="stuNo" column="id" />
<result property="stuName" column="name" />
</resultMap>
resultType+HashMap
<select id="queryStudentByIdWithHashMap" parameterType="int" resultType="HashMap">
select id "stuno",name "stuName" from student where id = #{id}
</select>
注意: 如果表中有10个字段,但有一个字段,查询的结果始终为默认值,(0,0.0,null) ,则可能是表的字段名 和 类的属性名不一致造成的。