模拟Mybatis的实现方法
|
所需要用到的其他工具或技术: 项目管理工具 : Maven 测试运行工具 : Junit 数据库 : Derby XML操作工具:Dom4j 继续不废话 Maven Dependencies: <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.derby</groupId> <artifactId>derby</artifactId> <version>10.10.2.0</version> </dependency> <dependency> <groupId>org.apache.derby</groupId> <artifactId>derbyclient</artifactId> <version>10.10.2.0</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> SQL 建表及数据插入(如果在第一节中作过,可以跳过此步): CREATE TABLE USER_TEST_TB( ID INT PRIMARY KEY,USERNAME VARCHAR(20) NOT NULL,PASSWORD VARCHAR(20) NOT NULL,NICKNAME VARCHAR(20) NOT NULL ); INSERT INTO USER_TEST_TB VALUES(1,'1st','111','Jack'); INSERT INTO USER_TEST_TB VALUES(2,'2nd','222','Rose'); INSERT INTO USER_TEST_TB VALUES(3,'3rd','333','Will'); Mybatis配置文件 src/main/resource源目录下
test-mybatis-configuration.xml
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<properties>
<property name="driver" value="org.apache.derby.jdbc.ClientDriver" />
<property name="url"
value="jdbc:derby://localhost:1527/bjpowernode;create=true" />
</properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper class="com.bjpowernode.practice.annotation.UserMapper" />
<mapper resource="com/bjpowernode/practice/xml/UserMapper.xml" />
</mappers>
</configuration>
User.java对象类(src/main/java/com/bjpowernode/practice目录下)
package com.bjpowernode.practice;
/**
*
* User Model
*
*/
public class User
{
private String id;
private String username;
private String password;
private String nickname;
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getNickname()
{
return nickname;
}
public void setNickname(String nickname)
{
this.nickname = nickname;
}
}
Select.java 注解类(src/main/java/com/bjpowernode/practice/annotation目录下)
package com.bjpowernode.practice.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** 标注此注解只能用在方法上 */
@Target(ElementType.METHOD)
/** 标注此注解生命周期是在Runtime运行时 */
@Retention(RetentionPolicy.RUNTIME)
public @interface Select
{
String value();
}
UserMapper.java 基于Annotation的配置类(src/main/java/com/bjpowernode/practice/annotation目录下)
package com.bjpowernode.practice.annotation;
import com.bjpowernode.practice.User;
import java.util.List;
public interface UserMapper
{
@Select("select * from USER_TEST_TB")
public List<User> getUser();
}
Mapper.java 对象类(src/main/java/com/bjpowernode/practice/simulation目录下)
package com.bjpowernode.practice.simulation;
/**
*
* 存储查询结果对象
*
*/
public class Mapper
{
/**
* 返回类型
*/
private String resultType;
/**
* 查询SQL
*/
private String querySql;
public String getResultType()
{
return resultType;
}
public void setResultType(String resultType)
{
this.resultType = resultType;
}
public String getQuerySql()
{
return querySql;
}
public void setQuerySql(String querySql)
{
this.querySql = querySql;
}
}
SQLSelectProxy.java AOP动态代理类(src/main/java/com/bjpowernode/practice/simulation目录下)
package com.bjpowernode.practice.simulation;
import com.bjpowernode.practice.annotation.Select;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
public class SQLSelectProxy implements InvocationHandler
{
@Override
public Object invoke(Object proxy,Method method,Object[] args) throws Throwable
{
/**
* 获得Mapper方法上的Select注解,以此来取得注解中的SQL语句
*/
Select select = method.getAnnotation(Select.class);
if (!method.isAnnotationPresent(Select.class))
{
throw new RuntimeException("缺少@Select注解!");
}
PreparedStatement pstmt = null;
ResultSet rs = null;
Object obj = null;
try
{
pstmt = SqlSessionImpl.connection.prepareStatement(select.value());
rs = pstmt.executeQuery();
/**
* 获得Method的返回对象类型,此处应当作判断处理,当List的时候,当只返回一个对象的时候.
* 为了简单实现功能并与第一节中测试文件不发生冲突起见,此处当作List处理
*/
String returnType = method.getGenericReturnType().toString();//java.util.List<com.bjpowernode.practice.User>
if (returnType.startsWith(List.class.getName()))
{
//去掉我们不需要的字符串,得到List中的类型
returnType = returnType.replace(List.class.getName(),"").replace("<","").replace(">","");
}
else
{
// 返回其他对象应当作其他处理,此处为了简单起见,暂不处理
}
obj = SqlSessionImpl.executeQuery(rs,returnType);
}
finally
{
if (rs != null && !rs.isClosed())
{
rs.close();
}
if (pstmt != null && !pstmt.isClosed())
{
pstmt.close();
}
}
return obj;
}
}
SqlSession.java Mybatis模拟接口(src/main/java/com/bjpowernode/practice/simulation目录下)
package com.bjpowernode.practice.simulation;
import java.util.List;
/**
*
* 模拟SqlSession
*
*/
public interface SqlSession
{
public <T> T getMapper(Class<T> clazz);
public <E> List<E> selectList(String query) throws Exception;
}
SqlSessionFactory.java Mybatis模拟类(src/main/java/com/bjpowernode/practice/simulation目录下)
package com.bjpowernode.practice.simulation;
import java.io.IOException;
import java.io.InputStream;
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.io.SAXReader;
/**
*
* 模拟SqlSessionFactory
*
*/
public class SqlSessionFactory
{
private InputStream configuration;
public SqlSession openSession() throws IOException
{
SqlSessionImpl session = new SqlSessionImpl();
loadConfigurations(session);
return session;
}
/**
*
* 通过Dom4j读取配置文件信息
*
* @param session
* @throws IOException
*/
private void loadConfigurations(final SqlSessionImpl session) throws IOException
{
try
{
Document document = new SAXReader().read(configuration);
Element root = document.getRootElement();
List<Element> mappers = root.element("mappers").elements("mapper");
for (Element mapper : mappers)
{
if (mapper.attribute("resource") != null)
{
session.setXmlSQLs(loadXMLConfiguration(mapper.attribute("resource").getText()));
}
if (mapper.attribute("class") != null)
{
}
}
}
catch (Exception e)
{
System.out.println("读取配置文件错误!");
}
finally
{
configuration.close();
}
}
/**
*
* 通过dom4j读取Mapper.xml中的信息
*
* @param resource
* @return
* @throws DocumentException
* @throws IOException
*/
private Map<String,Mapper> loadXMLConfiguration(String resource) throws DocumentException,IOException
{
Map<String,Mapper> map = new HashMap<String,Mapper>();
InputStream is = null;
try
{
is = this.getClass().getClassLoader().getResourceAsStream(resource);
Document document = new SAXReader().read(is);
Element root = document.getRootElement();
if (root.getName().equalsIgnoreCase("mapper"))
{
String namespace = root.attribute("namespace").getText();
for (Element select : (List<Element>) root.elements("select"))
{
Mapper mapperModel = new Mapper();
mapperModel.setResultType(select.attribute("resultType").getText());
mapperModel.setQuerySql(select.getText().trim());
map.put(namespace + "." + select.attribute("id").getText(),mapperModel);
}
}
}
finally
{
is.close();
}
return map;
}
public InputStream getConfiguration()
{
return configuration;
}
public void setConfiguration(InputStream configuration)
{
this.configuration = configuration;
}
}
(编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
