二、与 JDBC 相比,减小了 50%以上的代码量,消除了 JDBC 大量冗余的代码,不须要手动开关链接; 程序员
三、很好的与各类数据库兼容(由于 MyBatis 使用 JDBC 来链接数据库,因此只要JDBC 支持的数据库 MyBatis 都支持)。 面试
四、可以与 Spring 很好的集成;spring
五、提供映射标签,支持对象与数据库的 ORM 字段关系映射;提供对象关系映射标签,支持对象关系组件维护。 sql
二、SQL 语句依赖于数据库,致使数据库移植性差,不能随意更换数据库。 数据库
二、对性能的要求很高,或者需求变化较多的项目,如互联网项目,MyBatis 将是不错的选择
编程
二、Mybatis 直接编写原生态 sql,能够严格控制 sql 执行性能,灵活度高,很是适合对关系数据模型要求不高的软件开发,由于这类软件需求变化频繁,一但需求变化要求迅速输出成果。可是灵活的前提是 mybatis 没法作到数据库无关性,若是须要实现支持多种数据库的软件,则须要自定义多套 sql 映射文件,工做量大。缓存
三、Hibernate 对象/关系映射能力强,数据库无关性好,对于关系模型要求高的软件,若是用 hibernate 开发能够节省不少代码,提升效率。 安全
Mybatis 在处理#{}时,会将 sql 中的#{}替换为?号,调用 PreparedStatement 的set 方法来赋值; bash
使用#{}能够有效的防止 SQL 注入,提升系统安全性。
<select id=”selectorder” parametertype=”int” resultetype=”
me.gacl.domain.order”>
select order_id id, order_no orderno ,order_price price form
orders where order_id=#{id};
</select> 复制代码
<select id="getOrder" parameterType="int"
resultMap="orderresultmap">
select * from orders where order_id=#{id}
</select>
<resultMap type=”me.gacl.domain.order” id=”orderresultmap”>
<!–用 id 属性来映射主键字段–>
<id property=”id” column=”order_id”>
<!–用 result 属性来映射非主键字段,property 为实体类属性名,column
为数据表中的属性–>
<result property = “orderno” column =”order_no”/>
<result property=”price” column=”order_price” />
</reslutMap>复制代码
string wildcardname = “%smi%”;
list<name> names = mapper.selectlike(wildcardname);<select id=”selectlike”>
select * from foo where bar like #{value}
</select>复制代码
string wildcardname = “smi”;
list<name> names = mapper.selectlike(wildcardname);
<select id=”selectlike”>
select * from foo where bar like "%"#{value}"%"
</select>复制代码
举例:com.mybatis3.mappers^tudentDao.findStudentByld,能够惟 —找到 namespace 为 co m.mybatis3. map pe rs.Stud ent Dao 下面 id 为 findStudentByld MapperStatement.
Mapper接口里的方法,是不能重载的,由于是使用全限名+方法名的保存和寻找策略。Mapper授口的工做原理是JDK动态代理,Mybatis运行时会使用JDK 动态代理为Mapper接口生成代理对象proxy,代理对象会拦截接口方法,转而 执行MapperStatement所表明的sql,而后将sql执行结果返回.
Mybatis使用RowBounds对象讲行分页,它是针对ResultSet结果集执行的内存分页,而非物理分页.能够在sql内直接书写带有物理分页的参数来完成物理分页功能,也可使用分页播件来完成物理分页.
分页播件的基本原理是使用Mybatis提供的插件接口,实现自定义插件,在插件的栏截方法内拦截方法内拦截待执行的sql,而后重写sql, 根据dialect方言,添加对应的物理分页语句和物理分页参数.
第二种是使用 sql 列的别名功能,将列的别名书写为对象属性名。
<insert id=”insertname”>
insert into names (name) values (#{value})
</insert> 复制代码
list < string > names = new arraylist();
names.add(“fred”);
names.add(“barney”);
names.add(“betty”);
names.add(“wilma”);
// 注意这里 executortype.batch
sqlsession sqlsession =
sqlsessionfactory.opensession(executortype.batch);
try {
namemapper mapper = sqlsession.getmapper(namemapper.class);
for (string name: names) {
mapper.insertname(name);
}
sqlsession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
throw e;
}
finally {
sqlsession.close();
}复制代码
若是采用自增加策略,自动生成的键值在 insert 方法执行完后能够被设置到传入的参数对象中。
<insert id=”insertname” usegeneratedkeys=”true” keyproperty=”
id”>
insert into names (name) values (#{name})
</insert>
name name = new name();
name.setname(“fred”);
int rows = mapper.insertname(name);
// 完成后,id 已经被设置到对象中
system.out.println(“rows inserted = ” + rows);
system.out.println(“generated key value = ” + name.getid()); 复制代码
public UserselectUser(String name,String area);
对应的 xml,#{0}表明接收的是 dao 层中的第一个参数,#{1}表明 dao 层中第二
参数,更多参数一致日后加便可。 复制代码
<select id="selectUser"resultMap="BaseResultMap">
select * fromuser_user_t whereuser_name = #{0}
anduser_area=#{1}
</select> 复制代码
public interface usermapper {
user selectuser(@param(“username”) string
username,@param(“hashedpassword”) string hashedpassword);
} 复制代码
<select id=”selectuser” resulttype=”user”>
select id, username, hashedpassword
from some_table
where username = #{username}
and hashedpassword = #{hashedpassword}
</select> 复制代码
try {
//映射文件的命名空间.SQL 片断的 ID,就能够调用对应的映射文件中的
SQL
//因为咱们的参数超过了两个,而方法中只有一个 Object 参数收集,所以
咱们使用 Map 集合来装载咱们的参数
Map < String, Object > map = new HashMap();
map.put("start", start);
map.put("end", end);
return sqlSession.selectList("StudentID.pagination", map);
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
throw e;
} finally {
MybatisUtil.closeSqlSession();
} 复制代码
<mapper namespace="com.lcb.mapping.userMapper">
<!--association 一对一关联查询 --><select id="getClass" parameterType="int"
resultMap="ClassesResultMap">
select * from class c,teacher t where c.teacher_id=t.t_id and
c.c_id=#{id}
</select>
<resultMap type="com.lcb.user.Classes" id="ClassesResultMap">
<!-- 实体类的字段名和数据表的字段名映射 -->
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher"
javaType="com.lcb.user.Teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
</resultMap>
<!--collection 一对多关联查询 -->
<select id="getClass2" parameterType="int"
resultMap="ClassesResultMap2">
select * from class c,teacher t,student s where c.teacher_id=t.t_id
and c.c_id=s.class_id and c.c_id=#{id}
</select>
<resultMap type="com.lcb.user.Classes" id="ClassesResultMap2">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher"
javaType="com.lcb.user.Teacher">
<id property="id" column="t_id"/><result property="name" column="t_name"/>
</association>
<collection property="student"
ofType="com.lcb.user.Student">
<id property="id" column="s_id"/>
<result property="name" column="s_name"/>
</collection>
</resultMap>
</mapper> 复制代码
一、在 sqlMapConfig.xml 中配置 mapper.xml 的位置
<mappers>
<mapper resource="mapper.xml 文件的地址" />
<mapper resource="mapper.xml 文件的地址" />
</mappers> 复制代码
<bean id=" " class="mapper 接口的实现">
<property name="sqlSessionFactory"
ref="sqlSessionFactory"></property>
</bean>复制代码
<mappers>
<mapper resource="mapper.xml 文件的地址" />
<mapper resource="mapper.xml 文件的地址" />
</mappers>复制代码
<bean id="" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="mapper 接口地址" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>复制代码
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper 接口包地址 "></property>
<property name="sqlSessionFactoryBeanName"
value="sqlSessionFactory"/>
</bean>复制代码
有关于mybatis的面试详解就到这里了,有机会在后面的备战春招系列专题里面会给你们分享微服务,ZooKeeper ,Dubbo,kafka 等面试专题。全部的专题我也总结到一个pdf文档了
整个系列大概有1000道面试真题详解,须要源文件可关注微信公众号:Java程序员汇集地。