mybatis-config.xml,配置文件,用於鏈接數據庫。底層使用的是oracle數據庫的emp表java
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 4 <configuration> 5 <environments default="yy">//此處default值能夠任意寫 6 <environment id="yy">//id值也能夠任意寫 7 <transactionManager type="JDBC"></transactionManager>//事務管理器 8 <dataSource type="POOLED">//數據源 9 <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>//鏈接數據庫的驅動 10 11 <property name="url" value="jdbc:oracle:thin:@172.16.17.158:1521:orcl"/>//鏈接到哪個數據庫 12 <property name="username" value="scott"/>//數據庫名 13 <property name="password" value="tiger"/>//數據庫密碼 14 </dataSource> 15 </environment> 16 </environments> 17 <mappers> 18 <mapper resource="com/yh/mb/dao/EmpDao-mapper.xml"/> 19 </mappers> 20 </configuration>
數據源類型有三種: UNPOOLED , POOLED , JNDI 。 sql
UNPOOLED - 這個數據源實現只是在每次請求的時候簡單的打開和關閉一個鏈接。雖然這有點慢,但做爲一些不須要性能和當即響應的簡單應用來講 , 不失爲一種好選擇 。數據庫
POOLED - 這個數據源緩存 JDBC 鏈接對象用於避免每次都要鏈接和生成鏈接實例而須要的驗證時間 。對於併發 WEB 應用,這種方式很是流行由於它有最快的響應時間。 apache
JNDI - 這個數據源實現是爲了準備和 Spring 或應用服務一塊兒使用,能夠在外部也能夠在內部配置這個數據源,而後在 JNDI 上下文中引用它。這個數據源配置只須要兩上屬性:緩存
Emp.java實體類session
package com.yh.mb.entity; import java.io.Serializable; import java.util.Date; public class Emp implements Serializable { //員工姓名 private String empName; //員工編號 private Integer empNo; //員工入職時間 private Date hireDate; //員工職位 private String job; //員工工資 private Double salary; //經理編號 private Integer mgr; //獎金 private Double comm; //部門編號 private Integer deptNo; public Emp() { super(); } public Emp(String empName, Double salary) { super(); this.empName = empName; this.salary = salary; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public Integer getEmpNo() { return empNo; } public void setEmpNo(Integer empNo) { this.empNo = empNo; } public Date getHireDate() { return hireDate; } public void setHireDate(Date hireDate) { this.hireDate = hireDate; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } public Integer getMgr() { return mgr; } public void setMgr(Integer mgr) { this.mgr = mgr; } public Double getComm() { return comm; } public void setComm(Double comm) { this.comm = comm; } public Integer getDeptNo() { return deptNo; } public void setDeptNo(Integer deptNo) { this.deptNo = deptNo; } }
實體類的映射文件EmpDao-mapper.xml。用於書寫sql語句mybatis
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <mapper namespace="com.yh.mb.dao.EmpDao"> 5 <select id="empCout" resultType="int"> 6 select count(1) from emp 8 </select> 10 </mapper>
dao層的接口IEmpDao.java併發
1 package com.yh.mb.dao; 2 3 public interface IEmpDao { 4 public int empCount(); 5 }
dao層的實現類EmpDao.javaoracle
1 package com.yh.mb.dao.impl; 2 3 import java.io.IOException; 4 import java.io.Reader; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 11 import com.yh.mb.dao.IEmpDao; 12 13 public class EmpDao implements IEmpDao { 14 15 public int empCount() { 16 SqlSessionFactory sqlsf=null; 17 SqlSession sqlsession=null; 18 Reader reader=null;//配置文件字符輸入流 19 int count=0; 20 try { 21 reader=Resources.getResourceAsReader("mybatis-config.xml"); 22 sqlsf=new SqlSessionFactoryBuilder().build(reader); 23 sqlsession=sqlsf.openSession(); 24 count=sqlsession.selectOne("com.yh.mb.dao.EmpDao.empCout"); 25 } catch (IOException e) { 26 // TODO Auto-generated catch block 27 e.printStackTrace(); 28 }finally{ 29 if(sqlsession!=null){ 30 sqlsession.close(); 31 } 32 33 } 34 return count; 35 } 36 public static void main(String[] args) { 37 EmpDao empDao=new EmpDao(); 38 System.out.println(empDao.empCount()); 39 } 40 41 }