美文网首页
原生的查询单条多条

原生的查询单条多条

作者: _FireFly_ | 来源:发表于2020-11-07 09:36 被阅读0次

Student

package domain;

import java.sql.Date;

public class Student {

    private Integer id;
    private String name;
    private Integer sex;
    private Integer birth;
    private Date ctime;

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex=" + sex +
                ", birth=" + birth +
                ", ctime=" + ctime +
                '}';
    }

    public Student() {
    }

    public Student(Integer id, String name, Integer sex, Integer birth, Date ctime) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.birth = birth;
        this.ctime = ctime;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public Integer getBirth() {
        return birth;
    }

    public void setBirth(Integer birth) {
        this.birth = birth;
    }

    public Date getCtime() {
        return ctime;
    }

    public void setCtime(Date ctime) {
        this.ctime = ctime;
    }
}

StudentDao

package dao;

import domain.Student;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

@SuppressWarnings("all")
public class StudentDao {

    //设计一个方法 根据sex分组 每一个组中的人数
    public List<Map<String,Object>> selectCountBySex(){
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(Thread.currentThread().getContextClassLoader().getResourceAsStream("configuration.xml"));
        SqlSession sqlSession = factory.openSession(true);
        List<Map<String,Object>> result = sqlSession.selectList("selectCountBySex");
        return result;
    }

    //设计一个方法 根据编号查询对应的人名
    public String selectNameById(){//缺少一个参数id
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(Thread.currentThread().getContextClassLoader().getResourceAsStream("configuration.xml"));
        SqlSession sqlSession = factory.openSession(true);
        String name = sqlSession.selectOne("selectNameById");
        return name;
    }

    //设计一个方法 查询表格中记录的个数
    public int selectCount(){
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(Thread.currentThread().getContextClassLoader().getResourceAsStream("configuration.xml"));
        SqlSession sqlSession = factory.openSession(true);
        int ct = sqlSession.selectOne("selectCount");
        return ct;
    }


    //设计一个方法 查询表格中的全部内容
    public List<Student> selectAll(){
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(Thread.currentThread().getContextClassLoader().getResourceAsStream("configuration.xml"));
        SqlSession sqlSession = factory.openSession(true);
        List<Student> studentList = sqlSession.selectList("selectAll");//SQL语句的id  SQL上面的问号信息  告知每一行记录存储的类型
        return studentList;
    }







    //设计一个方法 查询单条记录     读操作 携带返回值
    public Student selectOne() {//应该需要参数  先固定
        //JDBC+SQL      告知SQL  告知SQL上面的问号信息(可以有 可以没有)  告知容器(一行记录)类型
        //加载驱动
        //获取连接
        //状态参数   conn.prepareStatement(sql);
        //将SQL和问号信息拼接完整
        //执行操作   ResultSet = executeQuery();
        //结果集中查询出来的信息 取出来存在一个新的容器内(数组 List Set Map domain)  结果集关掉啦
        //关闭操作   rs.close()   pstat.close()   conn.close();
        //返回那个新的容器

        //找寻sqlSession对象就可以啦
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("configuration.xml");
        //InputStream inputStream = Resources.getResourceAsStream("configuration.xml");
        //下面第二种方式获取输入流  几个点需要大家注意
        //  1.Resources这个类是MyBatis框架提供   依赖度比较高
        //  2.方法与之前我们利用ClassLoader加载的方法名一致      框架提供的方法有异常必须处理
        SqlSessionFactory factory = builder.build(inputStream);
        SqlSession sqlSession = factory.openSession(true);
        //让sqlSession帮我们做事
        Student student = sqlSession.selectOne("selectOne");//1.SQL语句的id  SQL语句上面的问号信息(暂时没有)  查询完毕的结果装到什么容器里
        return student;
    }









    //设计一个方法 新增一条学生记录
    public void insert(){
        //JDBC流程----不需要我们写啦
        //MyBatis帮我们做操作

        //创建工人对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("configuration.xml");
        //获取工厂对象
        SqlSessionFactory factory = builder.build(inputStream);//图纸 核心配置文件
        //获取提供的那个对象SqlSession
        SqlSession sqlSession = factory.openSession(true);//自动开启事务 自动提交  默认false 自动开启事务  不提交
        //帮我们执行数据库操作    增删改
        sqlSession.insert("insert");//加载驱动 获取连接(连接池) 创建状态参数

        //sqlSession.commit();
    }

}

StudentMapper.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.StudentDao">
    <insert id="insert">
        insert into student values(10,'zzt',1,1993,'2019-10-24')
    </insert>

    <select id="selectOne" resultType="domain.Student">
        select * from student where id = 1
    </select>

    <select id="selectAll" resultType="domain.Student">
        select * from student
    </select>

    <select id="selectCount" resultType="int">
        select count(*) as ct from student    <--语句不用带;号-->
    </select>

    <select id="selectNameById" resultType="string"> <--这里运用了别名-->
        select name from student where id = 1;
    </select>

    <select id="selectCountBySex" resultType="hashmap">
        select sex,count(*) as ct from student group by sex
    </select>
</mapper>

TestMain

public class TestMain {

    public static void main(String[] args) {
        StudentDao dao = new StudentDao();

        //dao.insert();

//        Student student = dao.selectOne();
//        System.out.println(student);

//        List<Student> studentList = dao.selectAll();
//        for(Student student : studentList){
//            System.out.println(student);
//        }

//        int count = dao.selectCount();
//        System.out.println(count);

//        String name = dao.selectNameById();
//        System.out.println(name);

        List<Map<String,Object>> result = dao.selectCountBySex();
        for(Map<String,Object> map : result){
            System.out.println(map);
        }
    }
}

相关文章

网友评论

      本文标题:原生的查询单条多条

      本文链接:https://www.haomeiwen.com/subject/rczovktx.html