1.什么是Mybatis?
--一个开源的持久层框架
--封装了全部的JDBC代码和参数以及及国际的检索
--使用简单的XML或注解作配置和定义映射关系
2.与jdbc及hibernate的对比
--jdbc: 速度快 代码量大 须要写sql。
--hibernate: 不用写sql 代码量少 速度慢 学习难度大。
会生成复杂的sql,很差优化。
--mybatis:速度适中,须要写sql,代码量少,学习难度小。
3.编程步骤
step1. 导包。--mybatis,jdbc
step2. 添加配置文件。
注:链接池的配置,映射文件的位置。
step3. 写一个JavaBean实体类。
注:属性名必须与数据库字段名一致
step4. 写一个映射文件。
注:里面主要是sql语句。
step5. 调用SqlSession提供的方法来访问数据库。
4.MyBatis主配置文件
----在src下建立MyBatis主配置文件SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC
"-//ibatis.apache.org//DTD Config 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<environments default="environment">
<environment id="environment">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.OracleDriver" />
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="username" value="lhh" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/tarena/dao/EmpMapper.xml" />
</mappers>
</configuration>
----建立映射文件EmpMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC
"-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.tarena.dao.EmpMapper">
<select id="findAll"
resultType="com.tarena.entity.Emp">
select * from t_emp
</select>
<select id="findById"
parameterType="integer"
resultType="com.tarena.entity.Emp">
select * from t_emp where empno=#{id}
</select>
<insert id="save"
parameterType="com.tarena.entity.Emp">
insert into t_emp values(
emp_seq.nextval,
#{ename},
#{job},
#{mgr},
#{hiredate},
#{sal},
#{comm},
#{deptno}
)
</insert>
<update id="update"
parameterType="com.tarena.entity.Emp">
update t_emp set
ename=#{ename},
job=#{job},
mgr=#{mgr},
hiredate=#{hiredate},
sal=#{sal},
comm=#{comm},
deptno=#{deptno}
where empno=#{empno}
</update>
<delete id="delete"
parameterType="integer">
delete from t_emp where empno=#{id}
</delete>
</mapper>java
4. 使用Mapper映射器
--Mapper映射器使用注意事项:
Mapper接口名和对应的映射文件中的namespace必须一致
Mapper接口中的方法必须和映射文件中的对应的SQL元素ID一致sql
1)建立Mapper映射器
import java.util.List;
import java.util.Map;
import com.tarena.entity.Emp;
public interface EmpMapper {
List<Emp> findAll();
Emp findById(int id);
void save(Emp e);
void update(Emp e);
void delete(int id);
List<Map<String, Object>> findByDeptNo(int deptno);
}数据库
2)测试
/**
* 使用Mapper映射器
*/
public void test1() {
SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
SqlSessionFactory ssf = ssfb.build(TestCase.class.getClassLoader().getResourceAsStream("SqlMapConfig.xml"));
// SqlSession是执行sql的容器。
SqlSession session = ssf.openSession();
//Mybatis 依据Mapper映射器(接口要求),返回一个符合该接口要求的对象
EmpMapper em = session.getMapper(EmpMapper.class);
Map<String,Object> data = em.findByID(1);
System.out.println(data.get("name".toUpperCase()));
}apache
5.使用ResultMap映射结果
--使用ResultMap映射结果,解决表中字段和实体类中属性不一样名的问题。
--经过在映射文件中配置<resultMap>标记,
将表中字段和实体对象中属性映射。
--建立映射文件DeptMapper.xml
<mapper namespace="com.tarena.dao.DeptMapper">
<select id="findAll" resultMap="deptMap">
select * from t_dept
</select>
<resultMap type="com.tarena.entity.Dept" id="deptMap">
<result property="id" column="deptno"/>
<result property="name" column="dname"/>
<result property="loc" column="loc"/>
</resultMap>
</mapper>编程