Mybatis快速學習(一),Mybatis快速學習(

來源:互聯網
上載者:User

Mybatis快速學習(一),Mybatis快速學習(

   這兩天學習mybatis,寫一下主要的知識點和流程吧,懶得再分類了,暫且把它放在ssh類裡面吧。首先,它是一個ORM架構,效能介於原生JDBC和hibernate之間,需要自己寫sql語句,開發效率比hibernate慢一些,比jdbc快一些,但是對於運行效率比hibernate快一些,比jdbc慢一些,反正也差不多。

  mybatis快速入門步驟:

   第一:引入包:mybatis包和mysql的包。

   第二:寫設定檔:只要是關於jdbc的配置:

<?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"><!-- 事務管理,這裡選擇jdbc --><transactionManager type="JDBC"></transactionManager><!-- 配置數據源 --><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver" /><property name="url"value="jdbc:mysql://127.0.0.1:3306/jsky?useUnicode=true&characterEncoding=utf8" /><property name="username" value="root" /><property name="password" value="123456" /></dataSource></environment></environments><mappers><!-- 對應檔 --> <mapper resource="jsky/dao/Student.xml"/></mappers></configuration>
  第三步:編寫實體類:

   略簡單,不貼代碼了

  第四步“:寫對應的mapper檔案,也就是第二部映射的檔案:

  

<?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="jksy.dao.student">        <select id="select" parameterType="Integer" resultType="jsky.model.Student">        select * from student where id=#{id}        </select>        </mapper>
   第五步:測試


                //讀取設定檔作為輸入流InputStream is=Resources.getResourceAsStream("mybatis.xml");//通過設定檔獲取factorySqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);//通過factory獲取sessionSqlSession session=factory.openSession();//這裡第一個參數是反射到Student.xml上的相應方法上,第二個參數是相關參數Student stu=session.selectOne("jksy.dao.student.select", 1);System.out.println(stu);session.close();
這個例子略簡單,都是最基本的mybatis的操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.