zoukankan      html  css  js  c++  java
  • Mybatis(0)——基础入门,hello,Mybatis! (使用IDEA)

    首先,介绍Mybatis是什么:

    然后,创建一个hello,Mybatis! 需要什么:

      两个XML文件,一个是 mybatis-config.xml全局配置文件;一个是:StudentMapper.xml映射文件,用来将数据库的内容映射成一个个对象。

      由全局配置文件可以获得SqlSessionFactory对象,再由这个对象可以获得SqlSession对象,SqlSession对象相当于Connection对象,就可查询到数据库的内容。

    0)基础介绍:https://www.jianshu.com/p/c77e3691867d

                https://www.jianshu.com/p/374bcb90083d

                javaee搭建环境:https://blog.csdn.net/FJJ543/article/details/81064891

      Mybatis官方下载链接:https://github.com/mybatis/mybatis-3/releases

        注:Github上的mybatis库上面有一个官方说明文档,里面有一个about页面,可以选择中文,刚刚试了一下,说明文档链接打不开,-_-||

    1)环境搭建:

        首先:导入最基本两个包 :mysql-connector-java-8.0.16.jar 和 mybatis-3.5.1.jar

        然后 :创建一个mybatis-config.xml文件模板,方便使用

            注:新增模板,是点击+号      https://www.jianshu.com/p/374bcb90083d

            模板内容可以在jar包解压缩后的说明文档里找到,其中的连接资源,根据自己环境调整

    <?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>
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                    <!--jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT-->
                    <property name="url" value="jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT"/>
                    <property name="username" value="root"/>
                    <property name="password" value="123456"/>
                </dataSource>
            </environment>
        </environments>
        <mappers>
            <!--!!!记得将SQl映射文件注册到全局配置文件中-->
            <mapper resource="config/StudentMapper.xml"/>
        </mappers>
    </configuration>
    

        注:

          1.显示不了: &符合, 使用 &amp;  代替

          2.记得将SQl映射文件注册到全局配置文件中

        <mappers>
            <!--!!!记得将SQl映射文件注册到全局配置文件中-->
            <mapper resource="config/StudentMapper.xml"/>
        </mappers>
    

        再然后:创建映射文件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="config/StudentMapper.xml">
        <select id="selectStudent" resultType="mybatis.model.Student">
        select * from student where id = #{id}
        </select>
    </mapper>
    

         注:为这个属性创建一个学生对象,相对应的内容应该根据数据库编写相应的属性。resultType="mybatis.model.Student"

    //get(),set()方法,tostring()记得实现
    public class Student {
        private Integer id;
        private String name;
        private String address;
        private String phone;
    
        public Student() {
        }
    }
    

    2)根据全局配置文件获取SqlSessionFactory对象

            String resource = "config/mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory =  new SqlSessionFactoryBuilder().build(inputStream);
    

     获取sqlSession对象

            SqlSession session = sqlSessionFactory.openSession();
            try {
                //第一个参数为StudentMapper.xml文件中的select的ID,第二个参数是传给SQL的标识符
                Student student= session.selectOne(    "selectStudent", 1);
                //打印对象
                System.out.println(student);
            } finally {
                session.close();
            }
    

    这个时候运行会出现错误,原因是:config文件夹没有被当做资源文件夹,所以会找不到mybatis-config.xml,应该将config设置为resource文件夹

        参考:https://blog.csdn.net/qq_37503483/article/details/81223968  

        参考2:https://blog.csdn.net/u014723529/article/details/78050725  各个文件夹的介绍:https://blog.csdn.net/xiaohei_neko/article/details/79353605

    这时还会报错,因为:

        默认情况下,工程编译后,resources中的文件和文件夹会被放置在和源码编译后相同的文件夹中,所以如果在源码中以相对路径读取resources中的配置文件时,可认为src中的java文件夹和resources为同一个根目录。

    解决方法:config/mybatis-config.xml的config/去掉,你要明白,全局配置文件注册的 <mapper resource="config/StudentMapper.xml"/> 也要去掉config。

    成功运行:

    因为上面的代码还不够优化, 所以在现有的基础上对代码进行升级:使用接口式编程

    1)新建一个学生接口:

    public interface StudentMapper {
        Student getStudentById(int id);
    }
    

     2)对sql映射文件进行修改: 相当于接口的实现类

    <!--使用接口进行编程-->
    <!--namespase 对应接口的全类名-->
    <mapper namespace="mybatis.StudentMapper">
        <!--id 对应里边的方法-->
        <select id="getStudentById" resultType="mybatis.model.Student">
        select * from student where id = #{id}
        </select>
    </mapper>
    

     3)获取学生对象:通过得到接口对象,再得到学生对象。之前是直接得到学生对象:session.selectOne( "selectStudent", 1);   <-----第一个参数使得代码不够简洁

    前面的代码不变。

           /*使用接口进行编程*/
            try {
                //得到接口对象
                StudentMapper studentMapper= session.getMapper(StudentMapper.class);
                Student student= studentMapper.getStudentById(1);
                //打印对象
                System.out.println(student);
            } finally {
                session.close();
            }
    

    总结:

    SqlSession 对象表示对数据库的一次会话。
    SqlSession 和connection一样,非线程安全,每次使用完要进行关闭 close()
    两个重要的配置文件:
        全局配置文件
        映射文件
     
  • 相关阅读:
    结构体struct和typedef后面接指针的含义
    C++中关于指针初始化和使用NULL的理解
    (虚)继承类的内存占用大小
    为什么构造函数不能为虚函数
    C++中变量自动初始化的问题
    CY7C68013A的一点总结
    Altium designer总结
    在Linux系统上限制远程登录的IP
    linux系统如何限制其他用户登录
    使用秘钥对登录Linux系统
  • 原文地址:https://www.cnblogs.com/Lemonades/p/11070409.html
Copyright © 2011-2022 走看看