完整代码链接:点击打开链接
SSM框架在项目开发中经常使用到,相比于SSH框架,它在仅几年的开发中运用的更加广泛。
的type属性、其有三种取值:
package com.yee.mybatis;
public class DataSource {
private String driver;
private String url;
private String user;
private String password;
@Override
public String toString() {
return "DataSource [driver=" + driver + ", url=" + url + ", user=" + user + ", password=" + password + "]";
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
为了模拟实现 我封装了一个MapperInfo对象:
package com.yee.mybatis;
public class MapperInfo {
private String parameterType;
private String resultType;
private String sql;
private boolean isUpdate=false;
@Override
public String toString() {
return "MapperInfo [parameterType=" + parameterType + ", resultType=" + resultType + ", sql=" + sql
+ ", isUpdate=" + isUpdate + "]";
}
public String getParameterType() {
return parameterType;
}
public void setParameterType(String parameterType) {
this.parameterType = parameterType;
}
public String getResultType() {
return resultType;
}
public void setResultType(String resultType) {
this.resultType = resultType;
}
public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
public boolean isUpdate() {
return isUpdate;
}
public void setUpdate(boolean isUpdate) {
this.isUpdate = isUpdate;
}
}
insert into dept values(#{deptno},#{dname},#{loc})
MapperInfo对象就是保存这个映射文件的基本信息的,下面开始解析DeptMapper.xml文件并进行处理。
读取并解析映射文件 DeptMapper.xml 获取MapperInfo对象
package com.yee.mybatis;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
public class SqlSessionFactory {
private MyBatisConfig config;
private Map mapperInfos=new HashMap();
public SqlSessionFactory(MyBatisConfig config){
this.config=config;
try {
parseXml();
} catch (DocumentException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
private void parseXml() throws DocumentException{
List mappers=config.getMappers();
if(mappers!=null && mappers.size()>0){
SAXReader saxReader=new SAXReader();
for(String mapper:mappers){
Document doc=saxReader.read(this.getClass().getClassLoader().getResourceAsStream(mapper));
XPath xpath=doc.createXPath("//mapper/*");
List ops=xpath.selectNodes(doc);
MapperInfo info=null;
String opname=null;
for(Element el:ops){
info=new MapperInfo();
opname=el.getName(); //获取操作类型 即元素名 insert
if("select".equals(opname)){
info.setUpdate(false); //不为更新操作
}
info.setParameterType(el.attributeValue("parameterType"));
info.setResultType(el.attributeValue("resultType"));
info.setSql(el.getTextTrim());
mapperInfos.put(el.attributeValue("id"),info);
}
}
}
}
public MyBatisConfig getConfig() {
return config;
}
public Map getMapperInfos() {
return mapperInfos;
}
}
sqlSession对象封装了传统的DBHelper实现数据层的功能,为上层提供更为简洁易懂的接口。
package com.yee.mybatis;
import java.util.List;
public class SqlSession {
private SqlSessionFactory factory;
private DBHelper db;
public SqlSession(SqlSessionFactory factory){
this.factory=factory;
db=new DBHelper(factory.getConfig().getDataSource());
}
/**
* 按要求返回查找的数据
* @param sqlId
* @param params
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public List selectList(String sqlId,Object ... params){
//获取sql
//获取参数列表
MapperInfo mapperInfo=factory.getMapperInfos().get(sqlId); //键值对存储可以快速获取 addDept findAll 和 对应的属性 parameterType resultMap
if(mapperInfo!=null){
try {
String sql=mapperInfo.getSql();
if(mapperInfo.isUpdate()){//如果是更新
return null;
}else{ //为查询
String className=mapperInfo.getResultType();
Class c=Class.forName(className);
//调用数据层返回结果
return db.find(sql, c, params);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return null;
}