image.png
    configuration.xml

    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <!DOCTYPE configuration
    3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
    5. <configuration>
    6. <environments default="development">
    7. <environment id="development">
    8. <transactionManager type="JDBC"></transactionManager>
    9. <dataSource type="POOLED">
    10. <property name="driver" value="com.mysql.jdbc.Driver"/>
    11. <property name="url" value="jdbc:mysql://localhost:3306/newtest?useSSL=false&amp;characterEncoding=UTF-8"/>
    12. <property name="username" value="root"/>
    13. <property name="password" value="cy414ljh212,,,"/>
    14. </dataSource>
    15. </environment>
    16. </environments>
    17. <mappers>
    18. <mapper resource="mapper/StudentMapper.xml"></mapper>
    19. </mappers>
    20. </configuration>

    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">
    
        <select id="selectOne" resultType="domain.Student">
            select * from newstudent where id = 1;
        </select>
    
        <select id="selectAll" resultType="domain.Student">
            select * from newstudent;
        </select>
    
        <select id="selectCount" resultType="int">
            select count(*) as ct from newstudent;
        </select>
    
        <select id="selectNameById" resultType="String">
            select name from newstudent where id =3;
        </select>
    
        <select id="selectCountBySex" resultType="HashMap">
            select sex,count(*) as ct from newstudent group by sex;
        </select>
    </mapper>
    

    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;
    
    public class StudentDao {
    
        //设计一个方法,根据编sex分组,统计每组人数
        public List<Map<String,Object>> selectCountBySex(){
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            InputStream inputStream=Thread.currentThread().getContextClassLoader().getResourceAsStream("configuration.xml");
            SqlSessionFactory factory= builder.build(inputStream);
            SqlSession sqlSession=factory.openSession(true);
            return sqlSession.selectList("selectCountBySex");
        }
    
        //设计一个方法,根据编号查询对应人名
        public String selectNameById(){
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            InputStream inputStream=Thread.currentThread().getContextClassLoader().getResourceAsStream("configuration.xml");
            SqlSessionFactory factory= builder.build(inputStream);
            SqlSession sqlSession=factory.openSession(true);
            return sqlSession.selectOne("selectNameById");
        }
    
        //设计一个方法,查询表格中记录的个数
        public int selectCount(){
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            InputStream inputStream=Thread.currentThread().getContextClassLoader().getResourceAsStream("configuration.xml");
            SqlSessionFactory factory= builder.build(inputStream);
            SqlSession sqlSession=factory.openSession(true);
            return sqlSession.selectOne("selectCount");
        }
    
        //设计一个方法,查询表格中全部记录
        public List<Student> selectAll(){
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            InputStream inputStream=Thread.currentThread().getContextClassLoader().getResourceAsStream("configuration.xml");
            SqlSessionFactory factory= builder.build(inputStream);
            SqlSession sqlSession=factory.openSession(true);
            return sqlSession.selectList("selectAll");//SQL语句的id、SQL上问号信息、告知每一行记录存储的类型
        }
    
        //设计一个方法,查询单条记录 读操作,携带返回值
        public Student selectOne(){//此处模拟用用固定记录来进行演示
            /*
            原先的框架底层:
            JDBC+SQL,告知SQL--告知SQL上问号信息(非必须有)---告知容器(一行记录)类型
            加载驱动--获取连接--状态参数(conn.preparedStatement(sql))--
            将SQL和问号信息拼接完整--执行操作--将查询结果取出后装入新容器(数组、集合、domain对象)--关闭结果集--
            关闭操作--返回新的容器
             */
    
            //找寻SqlSession对象
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            InputStream inputStream=Thread.currentThread().getContextClassLoader().getResourceAsStream("configuration.xml");
            //第二种获取输入流的方式,有几点需要注意
            //InputStream inputStream1= Resources.getResourceAsStream("configuration.xml");
            //1.Resources这个类是Mybatis框架提供的,依赖度很高
            //2.方法名与我们之前用到的ClassLoader加载方法一致,且框架提供的这个方法必须处理异常
            SqlSessionFactory factory=builder.build(inputStream);
            SqlSession sqlSession=factory.openSession(true);
            //让SqlSession对象帮我们做事
            return sqlSession.selectOne("selectOne");//1.SQL语句的id、SQL语句上的问号信息(暂无)、查询结果装入什么容器
        }
    
    }