您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

了解Mybatis的基础

2025/10/11 2:15:06发布15次查看
免费学习推荐:mysql视频教程
mybatis
mybatis-config.xml详细配置(配置时要把多余的属性删除 不能有中文 否则报错!)
<?xml version="1.0" encoding="utf-8" ?><!doctype configuration public "-//mybatis.org//dtd config 3.0//en" "http://mybatis.org/dtd/mybatis-3-config.dtd"><!--configuration核心配置 配置文件的根元素 --><configuration>    <!-- 属性:定义配置外在化 -->    <properties></properties>    <!-- 设置:定义mybatis的一些全局性设置 -->    <settings>        <!-- 具体的参数名和参数值 -->        <setting name="" value=""/>    </settings>    <!-- 类型名称:为一些类定义别名 -->    <typealiases>        <!-- 实体类少 建议 第一种取别名方式-->        <typealias type="包路径" alias="别名"></typealias>        <!--实体类多 建议 第二种取别名方式 默认情况下用这种方式 别名为类名 首字母最好小写 -->        <package name="包名"/>    </typealiases>    <!-- 类型处理器:定义java类型与数据库中的数据类型之间的转换关系 -->    <typehandlers></typehandlers>    <!-- 对象工厂 -->    <objectfactory type=""></objectfactory>    <!-- 插件:mybatis的插件,插件可以修改mybatis的内部运行规则 -->    <plugins>        <plugin interceptor=""></plugin>    </plugins>    <!-- 环境:配置mybatis的环境 -->    <environments default="development">        <!-- 环境变量:可以配置多个环境变量,比如使用多数据源时,就需要配置多个环境变量 -->        <environment id="development">            <!-- 事务管理器 -->            <transactionmanager type="jdbc"/>            <!-- 数据源 配置连接我的数据库-->            <datasource type="pooled">                <property name="driver" value="com.mysql.jdbc.driver"/>                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?servertimezone=gmt%2b8&amp;usessl=true&amp;useunicode=true&amp;characterencoding=utf-8"/>                <property name="password" value="123"/>                <property name="username" value="root"/>            </datasource>        </environment>    </environments>    <!-- 数据库厂商标识 -->    <databaseidprovider type=""></databaseidprovider>    <!-- 映射器:指定映射文件或者映射类 -->    <mappers>        <mapper resource="com/kang/w/dao/impl/usermapper.xml"></mapper>    </mappers></configuration>
分页
减少数据访问量
limt实现分页
sql语句: select * from 表名 limt 0,5;
0:数据开始的位置5:数据的长度第一种:使用mybatis
1接口
  list<user> getuserbylimit(map<string, object> map);
2mapeer.xml
   <select id="getuserbylimit" parametertype="map" resulttype="user">        select *        from mybatis.user        limit ${starindex},${pagesize}    </select>
2-1结果集映射
<resultmap id="map" type="user">        <result property="pwd" column="password"></result>    </resultmap>
3测试
 @test    public void getuserbylimittest() {        sqlsession sqlsession = mybatisutils.getsqlsession ();        usermapper mapper = sqlsession.getmapper (usermapper.class);        hashmap hashmap = new hashmap<string, object> ();        hashmap.put (starindex, 1);        hashmap.put (pagesize, 2);        list userbylimit = mapper.getuserbylimit (hashmap);        for (object o : userbylimit) {            system.out.println (o);        }        sqlsession.close ();    }
第二种:使用rowbounds方法
1.接口
list getuserlist();
2.实现接口
<select id="getuserlist" resulttype="user">        select *        from mybatis.user    </select>
3.测试:
 /**     * 测试使用rowbounds实现分页     */@test    public void getuserbylimitrowboundstest() {        sqlsession sqlsession = mybatisutils.getsqlsession ();        rowbounds rowbounds = new rowbounds (0, 2);        list<user> userlist = sqlsession.selectlist (com.kuang.w.dao.usermapper.getuserlist, null, rowbounds);        for (user user : userlist) {            system.out.println (user);        }        //关闭        sqlsession.close ();    }
第三种:使用mybatis的分页插件 pageheiper
sql 多对一处理
数据库 :
pojo
数据库中teacher-table表 对应实体类 teacher
package com.kuang.w.pojo;import lombok.data;/** * @author w */@datapublic class teacher {    private int tid;    private string tname;}
数据库中user表 对应 实体类student
package com.kuang.w.pojo;import lombok.data;/** * @author w */@datapublic class student {    private int id;    private int tid;    private string name;    private string password;    private teacher teacher;}
1.接口
   list<student> getstudentlist();
2.xml配置实现接口
  <!-- 多对一查询 1 子查询 mysql 通过一个表里是数据 与另一个表的一个数据相的情况下 查询另一个的数据 一起显示 -->    <select id="getstudentlist" resultmap="studentteacher">        select *        from mybatis.user;    </select>    <resultmap id="studentteacher" type="student">        <!-- 复杂属性 对象用 :association 集合用:collection-->        <!--column 数据库中的字段 property 实体类中的属性-->        <result property="id" column="id"/>        <result property="name" column="name"/>        <result property="password" column="password"/>        <!--javatype 一个 java 类的全限定名 ,或一个类型别名(关于内置的类型别名,可以参考上面的表格)。 如果你映射到一个 javabean,mybatis 通常可以推断类型。 然而,如果你映射到的是 hashmap, 那么你应该明确地指定 javatype 来保证行为与期望的相一致。-->        <association property="teacher" column="tid" javatype="teacher" select="getteacher"></association>    </resultmap>    <select id="getteacher" resulttype="teacher">        select *        from mybatis.teacher_table        where tid = #{id};    </select>
<!--2 多表联查-->    <select id="getstudentlist" resultmap="studentlist">        select u.id       uid,               u.name     uname,               u.password upassword,               u.tid      utid,               t.tname        from mybatis.user u,             mybatis.teacher_table t        where t.tid = u.tid;    </select>     <!-- 映射-->    <resultmap id="studentlist" type="student">        <result column="uid" property="id"/>        <result column="utid" property="tid"/>        <result column="uname" property="name"/>        <result column="upassword" property="password"/>        <association property="teacher" javatype="teacher">            <result property="tname" column="tname"></result>        </association>    </resultmap>
mybatis-config.xm配置
<?xml version="1.0" encoding="utf8" ?><!doctype configuration public "-//mybatis.org//dtd config 3.0//en" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>    <properties resource="db.properties"/>    <settings>        <setting name="logimpl" value="stdout_logging"/>    </settings>    <typealiases>        <typealias type="com.kuang.w.pojo.teacher" alias="teacher"/>        <typealias type="com.kuang.w.pojo.student" alias="student"/>    </typealiases>    <environments default="development">        <environment id="development">            <transactionmanager type="jdbc"/>            <datasource type="pooled">                <property name="driver" value="${driver}"/>                <property name="url" value="${url}"/>                <property name="password" value="${password}"/>                <property name="username" value="${username}"/>            </datasource>        </environment>    </environments>    <mappers>        <!-- <mapper resource="com/kuang/w/dao/teachermapper.xml"></mapper>           <mapper resource="com/kuang/w/dao/studentmapper.xml"></mapper>-->        <mapper class="com.kuang.w.dao.studentmapper"></mapper>        <mapper class="com.kuang.w.dao.teachermapper"></mapper>    </mappers></configuration>
3 测试
 @test    public void getstudentlisttest() {        sqlsession sqlsession = mybatisutils.getsqlsession ();        studentmapper mapper = sqlsession.getmapper (studentmapper.class);        list<student> studentlist = mapper.getstudentlist ();        for (student student : studentlist) {            system.out.println (student);        }        sqlsession.commit ();        sqlsession.close ();    }
sql 一对多处理
数据表结构 对应的实体类 不变
第一种方式: 多表联查
1接口
    list<teacher> getteacher(int tid);

2.1 xml实现接口
  <select id="getteacher" resultmap="teacherstudent">        select t.tid, t.tname, u.id, u.name, u.password        from mybatis.user u,             mybatis.teacher_table t        where t.tid = u.tid          and t.tid = #{tid};    </select>
2.2映射配置
<resultmap id="teacherstudent" type="teacher">        <result property="tname" column="tname"/>        <result property="tid" column="tid"/>        <!-- 复杂属性 对象用 :association 集合用:collection-->        <collection property="students" oftype="student">            <!--javatype 指定属性类型 一个 java 类的全限定名-->            <result column="id" property="id"></result>            <result column="name" property="name"></result>            <result column="password" property="password"></result>            <result column="tid" property="tid"></result>        </collection>    </resultmap>
3测试
 /*测试一对多*/    @test    public void getteachertest2() {        sqlsession sqlsession = mybatisutils.getsqlsession ();        teachermapper mapper = sqlsession.getmapper (teachermapper.class);        list<teacher> teacher = mapper.getteacher (1);        for (teacher teacher1 : teacher) {            system.out.println (teacher1);        } //提交事务   架子  这里可以不要        sqlsession.commit ();        // 关闭        sqlsession.close ();    }
结果
com.intellij.rt.junit.junitstarter -ideversion5 -junit4 com.kuang.w.dao.mytest,getteachertest2logging initialized using 'class org.apache.ibatis.logging.stdout.stdoutimpl' adapter.pooleddatasource forcefully closed/removed all connections.pooleddatasource forcefully closed/removed all connections.pooleddatasource forcefully closed/removed all connections.pooleddatasource forcefully closed/removed all connections.opening jdbc connectioncreated connection 164974746.setting autocommit to false on jdbc connection [com.mysql.cj.jdbc.connectionimpl@9d5509a]==>  preparing: select t.tid, t.tname, u.id, u.name, u.password from mybatis.user u, mybatis.teacher_table t where t.tid = u.tid and t.tid = ?; ==> parameters: 1(integer)<==    columns: tid, tname, id, name, password<==        row: 1, 狂神, 1, 天王盖地虎, 111<==        row: 1, 狂神, 2, 小波, 123<==        row: 1, 狂神, 3, 雷神, 922<==        row: 1, 狂神, 5, 马儿扎哈, 123<==      total: 4teacher(tid=1, tname=狂神, students=[student(id=1, tid=1, name=天王盖地虎, password=111), student(id=2, tid=1, name=小波, password=123), student(id=3, tid=1, name=雷神, password=922), student(id=5, tid=1, name=马儿扎哈, password=123)])resetting autocommit to true on jdbc connection [com.mysql.cj.jdbc.connectionimpl@9d5509a]closing jdbc connection [com.mysql.cj.jdbc.connectionimpl@9d5509a]returned connection 164974746 to pool.process finished with exit code 0
第二种方式: 子查询
1接口
    list<teacher> getteacher(int tid);

2 实现接口
 <!--第二种方式: 子查询-->    <select id="getteacher3" resultmap="teacherstudent3">        select *        from mybatis.teacher_table        where tid = #{tid};    </select>    <resultmap id="teacherstudent3" type="teacher">        <!-- 复杂属性 对象用 :association 集合用:collection 我们需要单独处理对象: association 集合: collection javatype=""指定属性的类型! 集合中的泛型信息,我们使用oftype 获取 -->        <result column="tid" property="tid"/>        <result column="tname" property="tname"/>        <collection property="students" javatype="arraylist" oftype="student" select="getstudentbyteacherid" column="tid">        </collection>    </resultmap>    <select id="getstudentbyteacherid" resulttype="student">        select *        from mybatis.user        where tid = #{tid};    </select>
3测试 同上
。。。。
相关免费学习推荐:mysql数据库(视频)
以上就是了解mybatis的基础的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product