mybatis簡單示例
阿新 • • 發佈:2018-11-10
mybatis簡單示例
建立mybatis配置檔案:mybatis.xml
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE configuration PUBLIC "mybatis-3-mapper.dtd" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="db.properties"/> <!--資料庫環境,支援配置多個environment default引數為任意environment的id--> <environments default="oracleEnvironment"> <environment id="oracleEnvironment"> <!--配置事務管理器 type可選值:JDBC,MANAGED--> <transactionManager type="JDBC"></transactionManager> <!--配置資料來源物件,type型別可選值:JNDI,POOLED,UNPOOLED--> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${pass}"/> </dataSource> </environment> </environments> <!--配置對映檔案--> <mappers> <mapper resource="switchcfg.xml"/> </mappers> </configuration>
建立資料庫資訊檔案db.properties
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
username=system
pass=123456
建立資料庫操作介面類SwictchCfgDAO
public interface SwictchCfgDAO { /** * 根據引數查詢表資料 * @param switchId * @return * @throws Exception */ public SwitchCfg getSwitchById(String switchId)throws Exception; }
建立介面類對映檔案switch.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd"> <mapper namespace="com.qgni.SwictchCfgDAO"> <select id="getSwitchById" parameterType="java.lang.String" resultType="com.qgni.SwitchCfg"> select * from comm.switch_cfg where switch_id = #{switchId} </select> </mapper>
建立實體類物件SwitchCfg
public class SwitchCfg {
String switch_ID;
String switch_Name;
String switch_Flag;
String switch_Content;
String switch_Class;
String ordinal;
String switch_Class_Code;
String class_Ordinal;
public String getSwitch_ID() {
return switch_ID;
}
public void setSwitch_ID(String switch_D) {
this.switch_ID = switch_D;
}
public String getSwitch_Name() {
return switch_Name;
}
public void setSwitch_Name(String switch_Name) {
this.switch_Name = switch_Name;
}
public String getSwitch_Flag() {
return switch_Flag;
}
public void setSwitch_Flag(String switch_Flag) {
this.switch_Flag = switch_Flag;
}
public String getSwitch_Content() {
return switch_Content;
}
public void setSwitch_Content(String switch_Content) {
this.switch_Content = switch_Content;
}
public String getSwitch_Class() {
return switch_Class;
}
public void setSwitch_Class(String switch_Class) {
this.switch_Class = switch_Class;
}
public String getOrdinal() {
return ordinal;
}
public void setOrdinal(String ordinal) {
this.ordinal = ordinal;
}
public String getSwitch_Class_Code() {
return switch_Class_Code;
}
public void setSwitch_Class_Code(String switch_Class_Code) {
this.switch_Class_Code = switch_Class_Code;
}
public String getClass_Ordinal() {
return class_Ordinal;
}
public void setClass_Ordinal(String class_Ordinal) {
this.class_Ordinal = class_Ordinal;
}
/**
* 重寫toString方法為了測試顯示
* @return
*/
@Override
public String toString() {
return switch_ID+switch_Name;
}
}
編寫測試類MyBatisTest
public class MyBatisTest {
public static void main(String[] args) {
String resource = "mybatis.xml";
Reader reader = null;
SqlSession session = null;
try {
reader = Resources.getResourceAsReader(resource);
//初始化工廠
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
//獲取資料庫操作物件
session= sessionFactory.openSession();
SwictchCfgDAO swictchCfgDAO = session.getMapper(SwictchCfgDAO.class);
SwitchCfg switchCfg = swictchCfgDAO.getSwitchById("inp_sel_023");
System.out.println(switchCfg.toString());
}catch (Exception e){
e.printStackTrace();
}finally {
if(session!=null){
session.close();
}
}
}
}