![](https://i-blog.csdnimg.cn/blog_migrate/075581ab5b98b49d989366bbbc32f9c0.png)
mybatis配置文件 mybatis-config.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.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url"
value="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=homework01" />
<property name="username" value="sa" />
<property name="password" value="******" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="hhuc/mapper/HW01InfoMapper.xml" />
</mappers>
</configuration>
mybatis工具类 MybatisUtils.java
package hhuc.mybatis;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
/*
* author:cenhelm
* mybatis工具类
*/
public class MybatisUtils {
private static final String FILE_CONFIG = "mybatis-config.xml";
private static SqlSessionFactory sqlSessionFactory ;
private MybatisUtils() {}
static {
InputStream inputStream;
try {
//读取mybatis配置文件
inputStream = org.apache.ibatis.io.Resources.getResourceAsStream(FILE_CONFIG);
//创建会话工程(连接工厂)
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取自动提交的会话
* @return
*/
public static SqlSession getSqlSessionAutoCommit() {
SqlSession session=sqlSessionFactory.openSession(true);
return session;
}
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
mapper映射文件 HW01InfoMapper.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="hhuc.mapper.HW01InfoMapper">
<insert id="insertClassInfo"
parameterType="hhuc.javabean.ClassInfo" useGeneratedKeys="true"
keyProperty="classId">
insert into ClassInfo(className)
values ( #{className} )
</insert>
<insert id="insertMethodInfo"
parameterType="hhuc.javabean.MethodInfo" useGeneratedKeys="true"
keyProperty="methodId">
insert into MethodInfo(classId, methodName, returnType)
values ( #{classId}, #{methodName}, #{returnType} )
</insert>
<insert id="insertParamInfo"
parameterType="hhuc.javabean.ParamInfo" useGeneratedKeys="true"
keyProperty="paramId">
insert into ParamInfo(methodId, paramIndex, paramType)
values ( #{methodId}, #{paramIndex}, #{paramType} )
</insert>
<select id="findClassId" resultType="long">
select classId from ClassInfo where className = #{className}
</select>
<select id="findMethodId" resultType="long">
<!-- 这个max()聚集函数是关键,因为我在后面要获取参数表里的methodId这个外键,而通过methodName会检索到多个id,也就是相同方法名有多个方法id,而这个id只要取最大的就一定对了 -->
select max(methodId) from MethodInfo where methodName = #{methodName}
</select>
<select id="selectClassInfo" resultType="hhuc.javabean.ClassInfo">
select * from ClassInfo
</select>
<select id="selectMethodInfo" resultType="hhuc.javabean.MethodInfo">
select * from MethodInfo
</select>
<select id="selectParamInfo" resultType="hhuc.javabean.ParamInfo">
select * from ParamInfo
</select>
</mapper>
定义映射接口 HW01InfoMapper.java
package hhuc.mapper;
import org.apache.ibatis.annotations.Mapper;
import hhuc.javabean.ClassInfo;
import hhuc.javabean.MethodInfo;
import hhuc.javabean.ParamInfo;
/*
* 定义映射接口(mapper为mybatis执行数据库操作的接口类)
*/
@Mapper
public interface HW01InfoMapper {
void insertClassInfo(ClassInfo obj);
void insertMethodInfo(MethodInfo obj);
void insertParamInfo(ParamInfo obj);
long findClassId(String className);
long findMethodId(String methodName);
ClassInfo[] selectClassInfo();
MethodInfo[] selectMethodInfo();
ParamInfo[] selectParamInfo();
}
ClassInfo表的java对象 ClassInfo.java
package hhuc.javabean;
/*
* author:cenhelm
* homework01数据库里面的ClassInfo表,javabean规范
*/
public class ClassInfo {
private String className;
public ClassInfo() {}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
MethodInfo表的java对象 MethodInfo.java
package hhuc.javabean;
/*
* author:cenhelm
* homework01数据库MethodInfo数据库,javabean规范
*/
public class MethodInfo {
private String methodName;
private String returnType;
private long classId;
public MethodInfo() {}
public String getMethodName() {
return methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
public String getReturnType() {
return returnType;
}
public void setReturnType(String returnType) {
this.returnType = returnType;
}
public long getClassId() {
return classId;
}
public void setClassId(long classId) {
this.classId = classId;
}
}
ParamInfo表的java对象 ParamInfo.java
package hhuc.javabean;
/*
* author:cenhelm
* homework01数据库paramInfo表,javabean规范
*/
public class ParamInfo {
private int paramIndex;
private String paramType;
private long methodId;
public ParamInfo() {}
public int getParamIndex() {
return paramIndex;
}
public void setParamIndex(int paramIndex) {
this.paramIndex = paramIndex;
}
public String getParamType() {
return paramType;
}
public void setParamType(String paramType) {
this.paramType = paramType;
}
public long getMethodId() {
return methodId;
}
public void setMethodId(long methodId) {
this.methodId = methodId;
}
}
XML工具类 XMLUtils.java
package hhuc.xmlTransform;
/*
* author:cenhelm
* xml工具类,转化Java对象到xml输出
*/
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class XMLUtils {
private static XStream xStream = null;
// 禁止实例化
private XMLUtils() {}
// 利用静态语句块初始化Xstream对象
static {
xStream = new XStream(new DomDriver("utf-8"));
}
//Object-->xml
public static void saveToFile(String filePath, Object[] objs) {
try {
OutputStream os = new FileOutputStream(filePath);
ObjectOutputStream out = xStream.createObjectOutputStream(os);
for(Object obj : objs)
out.writeObject(obj);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
json工具类 JsonUtils.java
package hhuc.jsonTransform;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/*
* author:cenhelm
* json工具类,转化Java对象到json
*/
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class JsonUtils {
private static Gson gson = null;
private JsonUtils() {}
static {
GsonBuilder builder = new GsonBuilder();
gson = builder.create();
}
//obj-->json字符串
public static void saveToFile(String filePath, Object[] objs) {
try {
OutputStream os = new FileOutputStream(filePath);
for(Object obj : objs) {
String str = gson.toJson(obj, obj.getClass());
os.write(str.getBytes());
}
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
测试类
package hhuc.test;
import java.lang.reflect.Method;
import java.util.Scanner;
import org.apache.ibatis.session.SqlSession;
import hhuc.javabean.ClassInfo;
import hhuc.javabean.MethodInfo;
import hhuc.javabean.ParamInfo;
import hhuc.jsonTransform.JsonUtils;
import hhuc.mapper.HW01InfoMapper;
import hhuc.mybatis.MybatisUtils;
import hhuc.xmlTransform.XMLUtils;
public class Test {
// 插入ClassInfo
public void insertClassInfo(String className) {
// 1-获取会话(连接)
SqlSession session = MybatisUtils.getSqlSession();
// 2-获得映射器对象
HW01InfoMapper mapper = session.getMapper(HW01InfoMapper.class);
// 3-创建图书对象,调用insert方法
ClassInfo obj = new ClassInfo();
obj.setClassName(className);
mapper.insertClassInfo(obj);
System.out.println("类" + obj.getClassName());
// 4-提交数据修改
session.commit();
// 5-关闭会话
session.close();
}
// 找到classId
public long findClassId(String className) {
// 1-获得一个会话
SqlSession session = MybatisUtils.getSqlSession();
// 2-获得映射器对象
HW01InfoMapper mapper = session.getMapper(HW01InfoMapper.class);
// 3-调用查询方法
long id = mapper.findClassId(className);
// 4-关闭会话
session.close();
return id;
}
// 插入MethodInfo
public void insertMethodInfo(long classId, String methodName, String returnType) {
// 1-获取会话(连接)
SqlSession session = MybatisUtils.getSqlSession();
// 2-获得映射器对象
HW01InfoMapper mapper = session.getMapper(HW01InfoMapper.class);
// 3-创建图书对象,调用insert方法
MethodInfo obj = new MethodInfo();
obj.setClassId(classId);
obj.setMethodName(methodName);
obj.setReturnType(returnType);
mapper.insertMethodInfo(obj);
System.out.println("方法" + obj.getClassId() + " " + obj.getMethodName() + " " + obj.getReturnType());
// 4-提交数据修改
session.commit();
// 5-关闭会话
session.close();
}
// 找到methodId
public long findMethodId(String methodName) {
// 1-获得一个会话
SqlSession session = MybatisUtils.getSqlSession();
// 2-获得映射器对象
HW01InfoMapper mapper = session.getMapper(HW01InfoMapper.class);
// 3-调用查询方法
long id = mapper.findMethodId(methodName);
// 4-关闭会话
session.close();
return id;
}
// 插入ParamInfo
public void insertParamInfo(long methodId, int paramIndex, String paramType) {
// 1-获取会话(连接)
SqlSession session = MybatisUtils.getSqlSession();
// 2-获得映射器对象
HW01InfoMapper mapper = session.getMapper(HW01InfoMapper.class);
// 3-创建图书对象,调用insert方法
ParamInfo obj = new ParamInfo();
obj.setMethodId(methodId);
obj.setParamIndex(paramIndex);
obj.setParamType(paramType);
mapper.insertParamInfo(obj);
System.out.println("参数" + obj.getMethodId() + " " + obj.getParamIndex() + " " + obj.getParamType());
// 4-提交数据修改
session.commit();
// 5-关闭会话
session.close();
}
// 查询ClassInfo表中所有数据
public ClassInfo[] selectClassInfo() {
SqlSession session = MybatisUtils.getSqlSession();
HW01InfoMapper mapper = session.getMapper(HW01InfoMapper.class);
ClassInfo[] objs = mapper.selectClassInfo();
return objs;
}
// 查询MethodInfo所有数据
public MethodInfo[] selectMethodInfo() {
SqlSession session = MybatisUtils.getSqlSession();
HW01InfoMapper mapper = session.getMapper(HW01InfoMapper.class);
MethodInfo[] objs = mapper.selectMethodInfo();
return objs;
}
// 查询ParamInfo中所有数据
public ParamInfo[] selectParamInfo() {
SqlSession session = MybatisUtils.getSqlSession();
HW01InfoMapper mapper = session.getMapper(HW01InfoMapper.class);
ParamInfo[] objs = mapper.selectParamInfo();
return objs;
}
// obj-->xml
public void saveToXml(String filePath, Object[] infos) {
XMLUtils.saveToFile(filePath, infos);
}
//obj-->json
public void saveToJson(String filePath, Object[] infos) {
JsonUtils.saveToFile(filePath, infos);
}
public static void main(String[] args) throws Exception {
System.out.println("输入完整类名");
Scanner scanner = new Scanner(System.in);
String className = scanner.nextLine();
scanner.close();
Class cls = Class.forName(className);
System.out.println("装载成功");
System.out.println(cls);
Test test = new Test();
// 插入类名信息
test.insertClassInfo(cls.getName());
java.lang.reflect.Method[] methods = cls.getDeclaredMethods();
for (Method method : methods) {
// 插入方法信息
test.insertMethodInfo(test.findClassId(cls.getName()), method.getName(), method.getReturnType().getName());
Class[] paramTypes = method.getParameterTypes();
for (int i = 1; i <= paramTypes.length; i++) {
// 插入方法参数信息
test.insertParamInfo(test.findMethodId(method.getName()), i, paramTypes[i - 1].getName());
}
}
// 存入xml和js
ClassInfo[] classInfos = test.selectClassInfo();
test.saveToXml("ClassInfos.xml", classInfos);
test.saveToJson("ClassInfos.js", classInfos);
MethodInfo[] methodInfos = test.selectMethodInfo();
test.saveToXml("MethodInfos.xml", methodInfos);
test.saveToJson("MethodInfos.js", methodInfos);
ParamInfo[] paramInfos = test.selectParamInfo();
test.saveToXml("ParamInfos.xml", paramInfos);
test.saveToJson("ParamInfos.js", paramInfos);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/bc31b80a2e1aa9a95a14f03d4f5e1620.png)
https://github.com/Adam-hohai/Java_homework