MyBaits基本概念和原理
阿新 • • 發佈:2017-09-14
mybatis
什麽是MyBatis?
MyBatis 是一款優秀的持久層框架,它支持定制化 SQL、存儲過程以及高級映射。MyBatis 避免了幾乎所有的 JDBC 代碼和手動設置參數以及獲取結果集。MyBatis 可以使用簡單的 XML 或註解來配置和映射原生信息,將接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java對象)映射成數據庫中的記錄。
MyBatis源碼對應的結構流程
SqlMapConfig.xml,此文件作為mybatis的全局配置文件,配置了mybatis的運行環境等信息。mapper.xml文件即sql映射文件,文件中配置了操作數據庫的sql語句,此文件需要在SqlMapConfig.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> <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/seckill?useUnicode=true&characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="mysql123" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/louis/mybatis/domain/EmployeesMapper.xml"/> </mappers> </configuration>
mapper.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" > <mapper namespace="com.louis.mybatis.dao.EmployeesMapper" > <resultMap id="BaseResultMap" type="com.louis.mybatis.model.Employee" > <id column="EMPLOYEE_ID" property="employeeId" jdbcType="DECIMAL" /> <result column="FIRST_NAME" property="firstName" jdbcType="VARCHAR" /> <result column="LAST_NAME" property="lastName" jdbcType="VARCHAR" /> <result column="EMAIL" property="email" jdbcType="VARCHAR" /> <result column="SALARY" property="salary" jdbcType="DECIMAL" /> </resultMap> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, SALARY from LOUIS.EMPLOYEES where EMPLOYEE_ID = #{employeeId,jdbcType=DECIMAL} </select> </mapper>
本文出自 “數據挖掘工程師的成長歷程” 博客,請務必保留此出處http://qianqiansun.blog.51cto.com/13271301/1965203
MyBaits基本概念和原理