MyBatis学习笔记

  1. 框架概述

三层架构:页面(用户数据jsp、html、servlet)+业务逻辑(接受界面数据,逻辑计算,调用数据库)+数据访问层(访问数据库,增删改查,持久层)

  1. 入门

1.新建数据库表

2.加入maven依赖,mybatis和mysql驱动

3.创建实体类

4.创建持久dao接口

5.创建mybatis配置文件,sql映射文件,一般一个表一个

6.创建mybatis主配置文件,一个项目一个,数据库连接信息和sql映射的位置信息

7.使用mybatis类

ResultType

Sql语句数据要转的java对象,mybatis执行sql,调用无参构造创建对象,将列值封装对象

ResultMap/Sql语句列别名与实体类字段名保持一致

<!--    定义resultMap-->
<resultMap id="myStudentMap" type="mystudent">
    <!--定义列名与字段映射管,主键列用id标签-->
    <id column="id" property="myId"></id>
    <result column="name" property="myName"></result>
    <result column="email" property="myEmail"></result>
    <result column="age" property="myAge"></result>
</resultMap>

<!--列别名解决列名与实体类字段名不一致问题-->
<select id="selectMyStudens" resultType="myStudent">
    select id myId, name myname, email myEmail, age myAge
    from student
</select>

  1. Dao代理
  2. 动态SQL

Select * from student where name like “%” #{name} “%”

使用标签<if><where><foreach>

<if test=”java对象属性值”>部分sql</if>

当多个if有一个成立<where>会自动加where关键字

<foreach>放在sql的in语句中

<foreach collection="array" separator="," index="index" item="id" open="(" close=")">

Collection:数组array,集合list固定的

Item:自定义

Open:开始字符

Close:结束字符

Seperator:集合成员的分隔符

<select id="selectForEachStu" resultType="Student">
    select * from student
    <where>
        <if test="array!=null and array.length>0">
            id in <foreach collection="array" separator="," index="index" item="id" open="(" close=")">#{id}</foreach>
        </if>
    </where>
</select>

<select id="selectForEachList" resultType="Student">
    select * from student
    <where>
        <if test="list!=null and list.size>0">
            id in <foreach collection="list" separator="," index="index" item="id" open="(" close=")">#{id}</foreach>
        </if>
    </where>
</select>

<select id="selectForEachStuList" resultType="Student">
    select * from student
    <where>
        <if test="list!=null and list.size>0">
            id in <foreach collection="list" separator="," index="index" item="stu" open="(" close=")">#{stu.id}</foreach>
        </if>
    </where>
</select>

Sql片段

<sql id="stduentSQL"> select * from student</sql>

<include refid="stduentSQL"></include>

  1. 配置文件

Jdbc.properties

<properties resource="Jdbc.properties"/>

<typeAliases>
    <!--        <typeAlias type="org.example.pojo.Student" alias="StudenT"></typeAlias>-->
    <package name="org.example.pojo"/>
</typeAliases>

<environments default="development">
    <environment id="development">
        <!--JDBC调用jdbcConnection,MANAGED委托给其他容器,可以是服务器tomcat或者框架-->
        <transactionManager type="JDBC"/>
        <!-- 实现了DataSource接口的都是数据源   ,能获取Connection对象的
                POOLED
                UNPOOLED不使用池,创建UnpooledDataSource完了旧关闭
                JNDI java命名目录,很少使用-->
        <dataSource type="POOLED">
            <property name="driver" value="${conn.driver}"/>
            <property name="url" value="${conn.url}"/>
            <property name="username" value="${conn.username}"/>
            <property name="password" value="${conn.password}"/>
        </dataSource>
    </environment>
</environments>

  1. 扩展

pageHelper数据分页,可以支持mysql、oracle、sqlserver、sqlite、postg

  1. 引入pageHelper的jar包

<dependency>
  <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper</artifactId>
  <version>5.2.1</version>
</dependency>

2.主配置文件加分页插件

<!--    分页插件-->
<plugins>
    <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>

使用PageHelper.startPage(6,3);

@Test
    public void test06() {
//        Student s = new Student();
//        s.setId(1001);
//        s.setName("%%");
        SqlSession session = MyBatisUtils.getSqlSession();
        StudentDao studentDao = session.getMapper(StudentDao.class);
//        pageNum第几页,pageSize一页的笔数
        PageHelper.startPage(6,3);
        List<Student> list = studentDao.selectAll();
        for (Student ss:list){
            System.out.println(ss);
        }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值