数据库和Java的一些其它操作

146 阅读1分钟

更新

首先,在poem.java中用到这三个函数

image.png

其次,在接口中用到该窗口与sql进行映射

image.png

最后,在测试项中调用这个函数,并通过接口传递信息

image.png

当然,要在html中也进行相当的sql语句设计

image.png

删除

首先,在接口中用到该窗口与sql进行映射

image.png

其次,在测试函数中调用该接口

image.png

最后,在xml文件中设置相应的sql语句

image.png

模糊查询1 R

首先,在接口中用到该窗口与sql进行映射

image.png

其次,在测试函数中调用该接口

image.png

最后,在xml文件中设置相应的sql语句

image.png

//模糊查询2 R
//        String content="%修改%";
//        List<Poem> poems=mapper.selectByContentOne(content);
//        for (Poem poem :poems){
//            System.out.println(poem);
//        }
        //模糊查询3R
//        String content="%修改%";
//        List<Poem> poems=mapper.selectByContentTwo(content);
//        for (Poem poem :poems){
//            System.out.println(poem);
//        }

        //sql攻击注入测试 ${} 危险模式
//        String content1 = "anything' OR 'x '=' x'";
//        List<Poem> poems = mapper.selectByContentThree(content1);
//        for (Poem poem:poems){
//            System.out.println(poem);
//        }

//        sql攻击注入测试 3{} 安全模式
//        String content1 = "anything' OR 'x '=' x'";
//        List<Poem> poems = mapper.selectByContentThree(content1);
//        for (Poem poem:poems){
//            System.out.println(poem);
//        }

//        聚合查询
//        int count = mapper.selectcount();
//        System.out.println(count);

        //新增数据获取id值1
//        Poem poem3 = new Poem();
//        poem3.setTitle("春晓");
//        poem3.setContent("春眠不觉晓,处处闻啼鸟,夜来风雨声,花落知多少");
//        LocalDateTime currentTime = LocalDateTime.now();
//        poem3.createTime(currentTime);
//        poem3.openFlag(0);
//        Long rlt=mapper.insert_one(poem3);
//        sqlSession.commit();
//        System.out.println("返回的id"+rlt);
        //新增数据获取id值2
//        Poem poem = new Poem();
//        poem.setTitle("春晓");
//        poem.setContent("春眠不觉晓,处处闻啼鸟。夜来风雨声,花落知多少。");
//        LocalDateTime currentTime = LocalDateTime.now();
//        poem.createTime(currentTime);
//        poem.openFlag(0);
//        System.out.println("插入数据前,poem:"+poem);
//        Long rlt = mapper.insertTwo(poem);
//        sqlSession.commit();
//        System.out.println("插入数据后,poem:"+poem);
package com.xyu.mapper;
import com.xyu.Poem;

import java.util.List;
//在这个文件中你可以定义sql语句对应类和方法
public interface PoemMapper {
        Poem queryPoemById(Long id);
        //插入
        Integer insert(Poem poem);
        //更新
        Integer update(Poem poem);
        Integer delete(int id);
        List<Poem> selectByContent(String content);
        List<Poem> selectByContentOne(String content);
        List<Poem> selectByContentTwo(String content);
        List<Poem> selectByContentThree(String content);
        int  selectcount();

        Long insertTwo(Poem poem);

        Long insert_one(Poem poem);
        List<Poem> queryAll();

}

image.png

image.png