mybatis的xml配置文件以及mybatis使用

server/2025/2/12 3:53:30/

数据库配置文件.db(放在resource下):

Mb.driver = com.mysql.cj.jdbc.Driver
Mb.url = jdbc:mysql://127.0.0.1:3306/数据库名
Mb.username = root
Mb.password = 密码

mybatis-config的xml文件(放在resource下):

<?xml version="1.0" encoding="UTF-8" ?>
<!--mybatis约束-->
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--    连接数据库环境--><properties resource="mysql.db"></properties>
<!--    驼峰映射开启-->
<!--    将mapUnderscoreToCamelCase配置为true即可自动将数据库中的user_name转换为实体类中的userName--><settings><setting name="mapUnderscoreToCamelCase" value="true" /></settings><!--    设置别名,以类名为别名--><typeAliases><package name=""/></typeAliases><environments default="mybatis"><environment id="mybatis"><transactionManager type="JDBC"></transactionManager><dataSource type="POOLED"><property name="driver" value="${Mb.driver}"/><property name="url" value="${Mb.url}"/><property name="username" value="${Mb.username}"/><property name="password" value="${Mb.password}"/></dataSource></environment></environments>
<!--    引入映射文件--><mappers><mapper resource="com/dt/dao/UserMapper.xml"></mapper>
<!-- 如果mapper太多时,可以通过包引入,此时不用逐一关联,name是mapper所在的包路径,可以通过复制引用来复制粘贴。-->
<!--        <package name="x.x.dao"/>--></mappers>
</configuration>

Mapper:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dt.dao.UserMapper"><!--    主键回填useGeneratedKeys="true" keyProperty="id"-->
<!--    <insert id="">-->
<!--     -->
<!--    </insert>--><!--    <update id="">-->
<!--    -->
<!--    </update>--><!--    <delete id="">-->
<!--      -->
<!--    </delete>--><!--  resultType 返回值类型--><select id="mapper里的函数名" resultType="实体类(全路径名)">sql搜索语句语句</select>
</mapper>

创建Mapper:

public interface XxxMapper {类型 xxx();
}

使用:

String resource = "mybaits-config.xml";//加载配置文件InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory  = new SqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession=  sqlSessionFactory.openSession();XxxrMapper xxMapper = sqlSession.getMapper(XxxMapper(接口).class);xxMapper.接口里面的方法();


http://www.ppmy.cn/server/39204.html

相关文章

声明式导航-跳转传参

目录 一、概念解释 二、查询参数传参 三、动态路由传参 四、两种传参方式的区别 五、动态路由参数可选符 一、概念解释 声明式导航&#xff1a;<router-link>标签 跳转传参&#xff1a;在跳转路由时&#xff0c;进行传值。 二、查询参数传参 语法&#xff1a;<…

信息技术自主可控的意义,针对国产化替换,服务器虚拟化或比公有云更具优势

我们之前在文章《博通收购VMware后&#xff0c;经销商和用户如何应对&#xff1f;新出路&#xff1a;虚拟化国产替代&#xff0c;融入信创云生态》中提到&#xff1a; 从信创整体发展和政策标准来看&#xff0c;供应商必须满足两个条件&#xff1a;一是融入国产信息技术生态&am…

git学习指南

文章目录 一.版本控制1.认识版本控制2.版本控制功能3.集中式版本控制4.分布式版本控制 二.Git的环境安装搭建1.Git的安装2.Git配置分类3.Git配置选项 三.Git初始化本地仓库1. git init/git clone-获取Git仓库2. 本地仓库文件的划分3. git status-检测文件的状态4. git add-文件…

MySQL没有初始化配置文件设置属性

情况描述 安装mysql的时候&#xff0c;为了速度&#xff0c;并没有配置my.ini或者my.cnf文件&#xff0c;数据库因为断电&#xff0c;导致数据都看不见了&#xff0c;一直提示不存在&#xff0c;这时候需要修改配置文件&#xff0c;将innodb_force_recovery设置为0到6的值&…

【Qt】界面定制艺术:光标(cursor)、字体(font)、提示(toolTip)、焦点(focusPolicy)与样式表(styleSheet)的深度探索

文章目录 前言&#xff1a;1. cursor: 设置按钮的光标2. front&#xff1a;设置字体3. toolTip: 鼠标悬停提示4. focusPolicy&#xff1a;设置控件获取到焦点的策略5. styleSheet : 样式表总结&#xff1a; 前言&#xff1a; 在现代软件开发中&#xff0c;用户界面(UI)的设计和…

vue原生div做触底加载

第一种&#xff1a; 触底加载和图片懒加载的思路一样&#xff0c;屏幕的高度加上滚动的高度快要大于最后一个元素距离顶部的高度的时候就开始加载数据&#xff1b; &#xff08;1&#xff09;clientHeight&#xff1a;屏幕的高度&#xff1b; &#xff08;2&#xff09;scro…

WPF之自定义绘图

1&#xff0c;创建自定义控件类 class CustomDrawnElement:FrameworkElement{public static readonly DependencyProperty BackgroundColorProperty;static CustomDrawnElement(){FrameworkPropertyMetadata meta new FrameworkPropertyMetadata(Colors.SkyBlue);meta.Affects…

[muduo网络库]——muduo库的Reactor模型(剖析muduo网络库核心部分、设计思想)

一、前言 在学习 C 服务端的过程中&#xff0c;必不可少的一项就是熟悉一个网络库&#xff0c;包括网络库的应用和其底层实现。我们熟知的网络库有 libevent、libev、muduo、Netty 等&#xff0c;其中 muduo 是由陈硕大佬个人开发的 TCP 网络库&#xff0c;最近跟着课程正在深…