1.什麼是MyBatis
2. MyBatis和Hibernate比較
1)hibernate是全自動,而mybatis是半自動。hibernate完全可以通過對象關系模型實現對數據庫的操作,擁有完整的JavaBean對象與數據庫的映射結構來自動生成sql。而mybatis僅有基本的字段映射,對象數據以及對象實際關系仍然需要通過手寫sql來實現和管理。
2)hibernate數據庫移植性遠大於mybatis。hibernate通過它強大的映射結構和hql語言,大大降低了對象與數據庫(oracle、mysql等)的耦合性,而mybatis由於需要手寫sql,因此與數據庫的耦合性直接取決於程序員寫sql的方法,如果sql不具通用性而用了很多某數據庫特性的sql語句的話,移植性也會隨之降低很多,成本很高。
3)hibernate擁有完整的日志系統,mybatis則欠缺一些。hibernate日志系統非常健全,涉及廣泛,包括:sql記錄、關系異常、優化警告、緩存提示、髒數據警告等;而mybatis則除了基本記錄功能外,功能薄弱很多。
4)mybatis相比hibernate需要關心很多細節。hibernate配置要比mybatis復雜的多,學習成本也比mybatis高。但也正因為mybatis使用簡單,才導致它要比hibernate關心很多技術細節。mybatis由於不用考慮很多細節,開發模式上與傳統jdbc區別很小,因此很容易上手並開發項目,但忽略細節會導致項目前期bug較多,因而開發出相對穩定的軟件很慢,而開發出軟件卻很快。hibernate則正好與之相反。但是如果使用hibernate很熟練的話,實際上開發效率絲毫不差於甚至超越mybatis。
5)sql直接優化上,mybatis要比hibernate方便很多。由於mybatis的sql都是寫在xml裡,因此優化sql比hibernate方便很多。而hibernate的sql很多都是自動生成的,無法直接維護sql;雖有hql,但功能還是不及sql強大,見到報表等變態需求時,hql也歇菜,也就是說hql是有局限的;hibernate雖然也支持原生sql,但開發模式上卻與orm不同,需要轉換思維,因此使用上不是非常方便。總之寫sql的靈活度上hibernate不及mybatis。 6)Hibernate有更好的二級緩存機制,可以使用第三方緩存。而MyBatis的二級緩存機制不佳。
工程免費下載
1、打開MYsql,新建一個表。
use test; create table t_employeer( employeer_id int not null primary key AUTO_INCREMENT , employeer_name varchar(50) default null, employeer_age int default null, employeer_department varchar(100) default null, employeer_worktype varchar(100) default null )
2、新建一個Java工程,導入需要用的包。mybatis-3.2.8.jar+mysql-connector-java-5.1.22-bin.jar
3、新建一個Mybatis配置文件:
<?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> <!-- 元素允許在主配置文件之外提供一個properties格式對應文件,從而使得主配置文件更加通用。這樣對部署非常有用 --> <!-- <properties resource="mysql.properties" /> --> <!-- 設置別名,一定要放在properties下面 --> <typeAliases> <typeAlias alias="Employeer" type="com.mucfc.model.Employeer" /> </typeAliases> <!-- 配置數據源相關的信息 --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="christmas258@"/> <!--<property name="driver" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> --> </dataSource> </environment> </environments> <!-- 列出映射文件 --> <mappers> <mapper resource="com/mucfc/model/Employeer.xml" /> </mappers> </configuration>數據源的配置方法也可以寫成:
<?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> <!-- 元素允許在主配置文件之外提供一個properties格式對應文件,從而使得主配置文件更加通用。這樣對部署非常有用 --> <properties resource="mysql.properties" /> <!-- 設置別名,一定要放在properties下面 --> <typeAliases> <typeAlias alias="Employeer" type="com.mucfc.model.Employeer" /> </typeAliases> <!-- 配置數據源相關的信息 --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <!-- <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="christmas258@"/> --> <property name="driver" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments> <!-- 列出映射文件 --> <mappers> <mapper resource="com/mucfc/model/Employeer.xml" /> </mappers> </configuration>mysql.properties如下:
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test?charset=utf8 username=root password=christmas258@
3、新建表對應的類:
package com.mucfc.model; import java.io.Serializable; /** * 雇員信息類 *@author linbingwen *@time 2015.5.11 */ public class Employeer { private Integer employeer_id; private String employeer_name; private Integer employeer_age ; private String employeer_department; private String employeer_worktype; public Employeer() { super(); } public Integer getEmployeer_id() { return employeer_id; } public void setEmployeer_id(Integer employeer_id) { this.employeer_id = employeer_id; } public String getEmployeer_name() { return employeer_name; } public void setEmployeer_name(String employeer_name) { this.employeer_name = employeer_name; } public Integer getEmployeer_age() { return employeer_age; } public void setEmployeer_age(Integer employeer_age) { this.employeer_age = employeer_age; } public String getEmployeer_department() { return employeer_department; } public void setEmployeer_department(String employeer_department) { this.employeer_department = employeer_department; } public String getEmployeer_worktype() { return employeer_worktype; } public void setEmployeer_worktype(String employeer_worktype) { this.employeer_worktype = employeer_worktype; } @Override public String toString() { return "Employeer [employeer_id=" + employeer_id + ", employeer_name=" + employeer_name + ", employeer_age=" + employeer_age + ", employeer_department=" + employeer_department + ", employeer_worktype=" + employeer_worktype + "]"; } }4、配置數據映射,這是本文的一個重點內容
5、測試類:insert into `t_employeer`(employeer_name,employeer_age,employeer_department,employeer_worktype) values(#{employeer_name},#{employeer_age},#{employeer_department},#{employeer_worktype}) delete from `t_employeer` where employeer_id = #{employeer_id} update t_employeer set employeer_name = #{employeer_name},employeer_age= #{employeer_age},employeer_department = #{employeer_department} ,employeer_worktype=#{employeer_worktype} where employeer_id = #{employeer_id}
package com.mucfc.test; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.mucfc.model.Employeer; public class MybatisTest { private static SqlSessionFactory sqlSessionFactory; private static Reader reader; static { try { reader = Resources.getResourceAsReader("mybatis-config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } catch (Exception e) { e.printStackTrace(); } } /** * 查找 */ public static void findEmployeerById(int id) { SqlSession session = null; try { session = sqlSessionFactory.openSession(); Employeer employeer = (Employeer) session.selectOne( "com.mucfc.model.EmployeerMapper.findEmployeerByID", 1); if (employeer == null) System.out.println("null"); else System.out.println(employeer); } finally { session.close(); } } /** * 增加 */ public static void addEmployeer(Employeer employeer){ SqlSession session = null; try { session = sqlSessionFactory.openSession(); //返回值是記錄條數 int resultCount = session.insert("com.mucfc.model.EmployeerMapper.addEmployeer", employeer ); System.out.printf("當前插入的employeer_id :%d 當前插入數據庫中條數:%d " , employeer.getEmployeer_id() ,resultCount); //獲取插入對象的id System.out.println(""); session.commit() ; } finally { session.close(); } } /** * 刪除 * */ public static void deleteEmployeer(int id){ SqlSession session = null; try { session = sqlSessionFactory.openSession(); //返回值是記錄條數 int resultCount=session.delete("com.mucfc.model.EmployeerMapper.deleteEmployeer",id); System.out.println("當前刪除數據庫中條數: "+resultCount); //獲取插入對象的id session.commit() ; } finally { session.close(); } } /** * 更改 */ public static void updateEmployeer(Employeer employeer){ SqlSession session = null; try { session = sqlSessionFactory.openSession(); session.update("com.mucfc.model.EmployeerMapper.updateEmployeer",employeer); session.commit() ; } finally { session.close(); } } public static void main(String[] args) { Employeer employeer1=new Employeer(); employeer1.setEmployeer_name("李四"); employeer1.setEmployeer_age(23); employeer1.setEmployeer_department("產品一部"); employeer1.setEmployeer_worktype("開發工程師"); Employeer employeer2=new Employeer(); employeer2.setEmployeer_name("張三"); employeer2.setEmployeer_age(30); employeer2.setEmployeer_department("產品二部"); employeer2.setEmployeer_worktype("測試工程師"); Employeer employeer3=new Employeer(); employeer3.setEmployeer_name("小王"); employeer3.setEmployeer_age(22); employeer3.setEmployeer_department("產品三部"); employeer3.setEmployeer_worktype("數據分析師"); Employeer employeer4=new Employeer(); employeer4.setEmployeer_name("明明"); employeer4.setEmployeer_age(22); employeer4.setEmployeer_department("財會部"); employeer4.setEmployeer_worktype("財務人員"); //插入 addEmployeer(employeer1); addEmployeer(employeer2); addEmployeer(employeer3); addEmployeer(employeer4); //查找 findEmployeerById(1); //刪除 deleteEmployeer(1); //更改 employeer2.setEmployeer_id(2); employeer2.setEmployeer_age(21); employeer2.setEmployeer_department("產品三部"); updateEmployeer(employeer2); } }
(1)插入測試
函數 :
/** * 增加 */ public static void addEmployeer(Employeer employeer){ SqlSession session = null; try { session = sqlSessionFactory.openSession(); //返回值是記錄條數 int resultCount = session.insert("com.mucfc.model.EmployeerMapper.addEmployeer", employeer ); System.out.printf("當前插入的employeer_id :%d 當前插入數據庫中條數:%d " , employeer.getEmployeer_id() ,resultCount); //獲取插入對象的id System.out.println(""); session.commit() ; } finally { session.close(); } }
//插入 addEmployeer(employeer1); addEmployeer(employeer2); addEmployeer(employeer3); addEmployeer(employeer4);
數據庫中結果:
(2)查找測試,接上面插入測試:
函數:
/** * 查找 */ public static void findEmployeerById(int id) { SqlSession session = null; try { session = sqlSessionFactory.openSession(); Employeer employeer = (Employeer) session.selectOne( "com.mucfc.model.EmployeerMapper.findEmployeerByID", 1); if (employeer == null) System.out.println("null"); else System.out.println(employeer); } finally { session.close(); } }
//查找 findEmployeerById(1);結果
(3)刪除測試,接上面
其中函數:
/** * 刪除 * */ public static void deleteEmployeer(int id){ SqlSession session = null; try { session = sqlSessionFactory.openSession(); //返回值是記錄條數 int resultCount=session.delete("com.mucfc.model.EmployeerMapper.deleteEmployeer",id); System.out.println("當前刪除數據庫中條數: "+resultCount); //獲取插入對象的id session.commit() ; } finally { session.close(); } }
調用
deleteEmployeer(1);
結果
第1條記錄被刪除了
(4)修改數據
這是之前
函數:
/** * 更改 */ public static void updateEmployeer(Employeer employeer){ SqlSession session = null; try { session = sqlSessionFactory.openSession(); session.update("com.mucfc.model.EmployeerMapper.updateEmployeer",employeer); session.commit() ; } finally { session.close(); } }
//更改 employeer2.setEmployeer_id(2); employeer2.setEmployeer_age(21); employeer2.setEmployeer_department("產品三部"); updateEmployeer(employeer2);
這是之後
張三的信息成功被改