ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
1.UserMapper.java ~~~ //删除,是否返回影响行数 int delete(int id); ~~~ 2.UserMapper.xml ~~~ <!--删除语句 返回影响行数--> <delete id="delete"> delete from t_user where id = #{id} </delete> ~~~ 3.Test.java ~~~ //定义配置文件路径 String resource = "mybatis-config.xml";//不能加 根目录 / InputStream inputStream = null; try {//加载配置文件 inputStream = Resources.getResourceAsStream(resource); } catch (IOException e) { e.printStackTrace(); }//根据配置文件创建SessionFactory对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //获得SqlSession SqlSession session = sqlSessionFactory.openSession(); //获得UserMapper接口的实现类 UserMapper mapper = session.getMapper(UserMapper.class); //调用UserMapper方法,执行xml中定义的SQL语句 int row = mapper.delete(22); //需要显式提交session session.commit(); session.close(); System.out.println(row); ~~~