Mybatis 基础学习

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>

4. 使用Mapper映射器
    --Mapper映射器使用注意事项:
        Mapper接口名和对应的映射文件中的namespace必须一致
        Mapper接口中的方法必须和映射文件中的对应的SQL元素ID一致

    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()));
        }

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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值