Mybatis--初见mybatis

news/2024/5/18 23:27:29 标签: java, intellij-idea, jdbc, mysql

Mybatis

ssm中连接数据库的框架

底层是jdbc

mybatis是将大量jdbc语句封装起来的框架

Mybatis前言 :

准备工作:

使用mybatis要导入mybatis依赖包和jdbc依赖包

maven配置:

 <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.7</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.26</version>
    </dependency>

创建一个resours目录作为资源文件夹 在其中创建mybatis的全局文件

<?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>
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="org/mybatis/example/BlogMapper.xml"/>
  </mappers>
</configuration>

在dao文件下 创建

一个接口用来编写所要查询的java语句 

并在同一目录下 实现sql语句的xml文件

<?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="dao.daoStudent">
    <select id="selectStudents" resultType="com.demo.Student">
       select id,name,age,email from Student order by id;
    </select>
    <update id="UpdataStudent" >
        UPDATE Student set name= #{name} where id=#{id};
    </update>
    <insert id="InsertStudent" >
        insert into Student VALUES (#{id},#{name},#{age},#{email});
    </insert>
</mapper>

namespace:关联的接口类

id:最好是写接口类中实现的方法名

resultType:全限定名称返回的对象类名


http://www.niftyadmin.cn/n/1297837.html

相关文章

ybt1281 最长上升子序列

ybt1281 最长上升子序列 时空限制 1000ms/64MB 【题目描述】 一个数的序列bi&#xff0c;当b1<b2<...<bS的时候&#xff0c;我们称这个序列是上升的。对于给定的一个序列(a1,a2,...,aN)&#xff0c;我们可以得到一些上升的子序列(ai1,ai2,...,aiK)&#xff0c;这…

2021——网络流初步

首先是一些概念&#xff0c;容量&#xff0c;流量&#xff0c;饱和弧&#xff0c;非饱和弧&#xff0c;零弧&#xff0c;非零弧&#xff0c;增广路&#xff0c;残量&#xff0c;残量网络 1.Edmonds—Karp算法 这个方法的时间复杂度比较差 为 一直bfs找增广路&#xff0c;直到…

Mybatis--使用Mybatis

Mybatis的使用步骤 //设置mybatis路径String config"mybatis.xml";//读取mybatisInputStream inResources.getResourceAsStream(config);//创建工厂制造者SqlSessionFactoryBuilder fbnew SqlSessionFactoryBuilder();//创造工厂SqlSessionFactory factoryfb.build(i…

luogu1091 合唱队形(NOIP2004提高组第3题)

luogu1091 合唱队形&#xff08;NOIP2004提高组第3题&#xff09; 时空限制 1000ms/128MB 题目描述 N位同学站成一排&#xff0c;音乐老师要请其中的(N-K)位同学出列&#xff0c;使得剩下的K位同学排成合唱队形。 合唱队形是指这样的一种队形&#xff1a;设K位同学从左…

Mybatis--高级Mybatis

定义别名 在Mybatis 的全局配置文件中给返回对象值 全限定类型对象类名 第一种方式&#xff1a; 给类下别名 <typeAliases><typeAlias type"com.demo.Student" alias"stu" /></typeAliases>第二种方式&#xff1a; <typeAliases&g…

2021——网络流例题

1.hdu3987 Harry Potter and the Forbidden Forest 这道题目有双向边和单向边&#xff0c;破坏一条边的代价为Wi&#xff0c;求破坏的总代价最小的情况下&#xff0c;最少要破坏几条道路&#xff0c;使得城市0无法到达城市n-1 就是求0-&#xff08;n-1&#xff09;的最小割 这…

luogu1020 导弹拦截(NOIP1999提高组第1题)

luogu1020 导弹拦截&#xff08;NOIP1999提高组第1题&#xff09; 时空限制 1000ms/128MB 题目描述 某国为了防御敌国的导弹袭击&#xff0c;发展出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷&#xff1a;虽然它的第一发炮弹能够到达任意的高度&#xff0c;但是以…

2021——二分图匹配

1.最小点覆盖最大匹配 eg.1 poj1325 Machine Schedule 题意&#xff1a;有两个机器A和B&#xff0c;A机器有n个模式&#xff0c;B机器有m个模式,两个机器最初在0模式。有k个作业&#xff0c;每个作业有三个参数i&#xff0c;a&#xff0c;b&#xff0c;其中i代表作业编号&…