首页 > 其他分享 >整合mybatis实现简单的增删改查

整合mybatis实现简单的增删改查

时间:2023-01-13 00:22:33浏览次数:37  
标签:改查 ibatis 增删 io import mybatis org apache id

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>
    <properties resource="mysql.properties"/>
    <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="${user}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="dao/UserMapper.xml"/>
    </mappers>
</configuration>

工具类

package utilts;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.io.InputStream;

import static org.apache.ibatis.io.Resources.getResourceAsStream;

public class MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;
   static {
       try {
           String resource="mybatis-config.xml";
           InputStream in= Resources.getResourceAsStream(resource);
           sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
       } catch (IOException e) {
           e.printStackTrace();
       }
   }
   public static SqlSession getSqlsession(){
       return sqlSessionFactory.openSession(true);
   }

}

映射实现

<?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.UserMapper">
<insert id="add" parameterType="pojo.User">
    insert into home.user values (#{id},#{name},#{age})
</insert>
<delete id="delete" parameterType="String">
    delete from home.user where id=#{id}
</delete>
    <update id="update" parameterType="pojo.User">
        update user
<set>
    <if test="id!=null and id!=''">id=#{id},</if>
        <if test="name!=null and name!=''" >name=#{name},</if>
    <if test="age!=null and age!=''" >age=#{age}</if>
</set>
where id=#{id}
    </update>
    <select id="select" resultType="pojo.User">
        select * from user
    </select>
</mapper>

此次遇到的问题

servlet映射路径未找到报404

这个问题实属无语

问题的关键在于应用上下文,程序执行之后页面实际地址为http://localhost:8081/add,但是因为tomcat里面的上下文为/MavenSon3_war_exploded,所以事实上得跳转到

http://localhost:8081/MavenSon3_war_exploded/add才可以找到

解决方案将应用程序上下文改为空

其次就是tomcat10和tomcat9的问题了,本人装了两个tomcat,端口分别为8080和8081,本次如果是tomcat9则会报一个异常,而tomcat10则不会

网上查找资料说是jar包和tomcat版本不匹配的问题,由于本人最近才开始使用idea,之前用eclipse并未碰到如此问题,发现idea的版本匹配机制挺迷糊的

之前jdk和maven版本也出现了不匹配问题

另外切记程序在运行之前一定要注册mapper

 

标签:改查,ibatis,增删,io,import,mybatis,org,apache,id
From: https://www.cnblogs.com/liyiyang/p/17034276.html

相关文章

  • 学习笔记——Mybatis中缓存机制
    2023-01-12一、Mybatis中缓存机制1、一级缓存(1)概述:一级缓存(即本地缓存或SqlSession级别缓存)(2)特点:①一级缓存默认开启②不能关闭③可以清空(3)缓存原理①当第一次获......
  • MyBatis4
      表关联=左连接,右连接,全连接,内连接1.准备表Teacher教师表列名类型备注tea_idint主键tea_namevarchar(32)名称tea_professi......
  • 学习笔记——Mybatis动态SQL
    2023-01-12一、Mybatis动态SQL即将SQL动态化同时Mybatis的动态SQL支持OFNL表达式,OGNL(ObjectGraphNavigationLanguage)对象图导航语言。1、先搭建环境(1)创建一个“mav......
  • mybatis的主键自增以及如何获得自增
    转载:https://juejin.cn/post/7168638701784793124#comment获得插入后获得主键:https://juejin.cn/post/7121713604667113485......
  • MyBatis(国税)
    一、MyBatis概要1.1、ORM介绍对象关系映射(ObjectRelationalMapping,简称ORM,或O/RM,或O/Rmapping),用于实现面向对象编程语言里不同类型系统的数据之间的转换。它是创建了一个......
  • mybatis 一级、二级缓存机制
     MyBatis提供了对缓存的支持,分为一级缓存和二级缓存  一级缓存是SqlSession级别的缓存。在操作数据库时需要构造SqlSession对象,在对象中有一个数据结构(HashMap......
  • SpringBoot+Mybatis-plus整合easyExcel批量导入Excel到数据库+导出Excel
    SpringBoot+Mybatis-plus整合easyExcel批量导入Excel到数据库+导出Excel 一、前言今天小编带大家一起整合一下easyExcel,之所以用这个,是因为easyExcel性能比较好,不会......
  • MyBatisPlus
    MyBatisPlus导入坐标SpringBoot并没有收录mybatisplus,所以需要自己导入坐标<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus......
  • 利用Java+Html+Jsp实现简单的MVC分层项目->实现表的增删改查
    graphTDView-->ServiceService-->ControllerService-->ViewController-->Service准备工作:无骨架创建一个maven项目,配置文件目录【增加webapp目录,在webapp下面一级添......
  • 01-mybatis解析XML文件
    <?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTDConfig3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><c......