2011年6月iBatis 更名為MyBatis,從iBatis 到MyBatis,不只是名稱上的變化,MyBatis 提供了更為強大的功能,同時並沒有損失其易用性,相反,在很多地方都藉助於JDK 的泛型和註解特性進行了簡化。所以你懂的,開始學習就從MyBatis吧.
範例使用MyBatis+MySQL實作一個使用者表的查詢,如下:
#①資料庫 在MySQL中,test資料庫下,履歷user表,欄位:id,name,password ,建表語句略。
②需導入的套件 只有兩個: mybatis-3.0.3.jar mysql-connector-java-5.1.16-bin.jar(JDBC套件)
③目錄結構採用最簡單的結構,com.mybatis套件下有且僅有四個檔案configuration.xml user.xml User.java Test.java
④mybatis設定檔configuration.xml,
[html]
<?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> <typeAliases> <typeAlias alias="User" type="com.mybatis.User"></typeAlias> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <mappers> <mapper resource="user.xml"/> </mappers> </configuration>
⑤User表sql檔User.xml,
[html]
<?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="User"> <select id="selectUser" parameterType="int" resultType="User"> SELECT * FROM user WHERE id = #{id} </select> <select id="selectUsers" resultType="User"> SELECT * FROM user </select> </mapper>
⑥表結構檔User .java,
[java]
package com.mybatis; public class User { private int id; private String name; private String password; public User(){} public User(int id, String name) { this.id = id; this.name = name; } public int getId() { return this.id; } public void setId(int id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [id=" + this.id + ", name=" + this.name + ", password=" + this.password + "]"; } }
⑦測試案例Test.java
[java]
package com.mybatis; import java.io.IOException; import java.io.Reader; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class Test { public static void main(String[] args) throws IOException { String resource = "configuration.xml"; Reader reader = Resources.getResourceAsReader(resource); SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader); SqlSession session = ssf.openSession(); try{ User user = session.selectOne("selectUser", "1"); System.out.println(user.getName()); System.out.println(user); System.out.println("--------------分隔线---------------"); List<User> users = session.selectList("selectUsers"); for(int i=0; i<users.size(); i++) { System.out.println(users.get(i).getName()); } } catch (Exception e) { e.printStackTrace(); } finally { session.close(); } } }
以上是在Java中怎麼使用Mybatis的詳細內容。更多資訊請關注PHP中文網其他相關文章!