所須要jar包:mybatis-3.x.x.jar 、假設須要和spring整合,還須要增加相關的包
1:看專案資料夾 紅顏色不要關心
2:依照步驟:
1:增加jar包
2:建立資料來源(configuration.xml)建立資料庫(此處省略)
<?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>
<!--給實體類起一個別名 user -->
<typeAlias type="com.forum.po.User" alias="User" />
</typeAliases>
<!--資料來源配置 這塊用 BD2資料庫 -->
<environments default="development">
<environment id="development">
<transactionManager type="jdbc" />
<dataSource type="POOLED">
<property name="driver" value="com.ibm.db2.jcc.DB2Driver" />
<property name="url" value="jdbc:db2://localhost:50000/forum" />
<property name="username" value="DB2ADMIN" />
<property name="password" value="admin" />
</dataSource>
</environment>
</environments>
<mappers>
<!--userMapper.xml裝載進來 同等於把“dao”的實現裝載進來 -->
<mapper resource="myBatis/userMapper.xml" />
</mappers>
</configuration>
3:建立實體類USER
package com.forum.po;
/**
* 使用者類
*
* @author db2admin
*
*/
public class User extends Entity {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User() {
}
}
4:建立dao介面、能夠說是Mapper介面:UserMapper
package com.forum.dao;
import com.forum.po.User;
public interface UserMapper {
public User findById(String Id);
}
5:建立dao的實現,不同於hibernarte的此處的實現為一個xml檔案,也就是userMapper.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">
<!--這塊等於dao介面的實現 namespace必須和介面的類路徑一樣-->
<mapper namespace="com.forum.dao.UserMapper">
<!-- findById必須和介面中的方法名一樣 返回一個User 就是剛才的別名 假設不弄別名要連類路徑一起寫 麻煩-->
<select id="findById" parameterType="HashMap" resultType="User">
select
* from butone.student where id=#{id}
</select>
</mapper>
6:測試類,MyBatisTest
package com.forum.test;
import java.io.IOException;
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.forum.dao.UserMapper;
import com.forum.po.User;
/**
* myBatis資料庫連線測試
*
* @author db2admin
*
*/
public class MyBatisTest {
/**
* 獲得MyBatis SqlSessionFactory
* SqlSessionFactory負責建立SqlSession,一旦建立成功,就能夠用SqlSession例項來執行對映語句,commit,rollback,close等方法。
* @return
*/
private static SqlSessionFactory getSessionFactory() {
SqlSessionFactory sessionFactory = null;
String resource = "configuration.xml";
try {
sessionFactory = new SqlSessionFactoryBuilder().build(Resources
.getResourceAsReader(resource));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sessionFactory;
}
public static void main(String[] args) {
SqlSession sqlSession = getSessionFactory().openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.findById("1");
System.out.println(user.getName());
}
}