Python 官方文档:入门教程 => 点击学习
目录一、mybatis流程简介二、手写简化版Mybatis设计思路2.1简化后的思路2.2读取XML文件,建立连接2.3创建sqlSession,搭建Configuration和Ex
最近在看 Mybatis 的源码,大致了解整个框架流程后便手写了一个特别简单的SimpMybatis
的小Demo,来巩固这整个框架的学习。下图是我所画的框架大致执行流程:
💊对上图分析后得出结论:
1.Mybatis 的配置文件分为两种,且这两个配置文件会被封装到 Configuration 中
2.通过 Mybatis 配置文件得到 SqlSessionFactory ;
3.通过 SqlSessionFactory 得到 SqlSession,它就相当于 Request 请求;
4.SqlSession 调用底层的 Executor 执行器来操作数据库,同时执行器有两类实现
5.解析传入的参数,对其进行封装,执行并返回结果;
以上就是我梳理的 Mybatis 大致流程,看似简单,却很精妙。
从图中可以看出,MyConfig 负责与人交互。待读取xml后,将属性和连接数据库的操作封装在 MyConfig 对象中供后面的组件调用。本项目将使用 dom4j 来读取xml文件,它具有性能优异和非常方便使用的特点。
从流程图中的箭头可以看出,MySqlSession 的成员变量中必须得有 MyExecutorImpl 和 MyConfig 去集中做调配。一个Session仅拥有一个对应的数据库连接。类似于一个前段请求Request,它负责直接调用对应 execute(sql) 来做 CRUD 操作。
MyExecutor 是一个执行器,负责SQL语句的生成和查询缓存的维护,也就是 Jdbc 的代码将在这里完成,不过本文只实现了单表,查询缓存并未实现。
只是希望对指定的接口生成一个对象,使得执行它的时候能运行一句 sql,而接口无法直接调用方法,所以这里使用动态代理生成对象,在执行时还是回到 MySqlSession 中调用查询,最终由 MyExecutorImpl 做 JDBC查询。这样设计是为了单一职责,可扩展性更强。
这次会将其打成 Jar 包,并将其导入项目实现,做一个 Mybatis 的还原。
工程文件及目录:
Maven 导入如下:
<!-- https://mvnrepository.com/artifact/org.dom4j/dom4j -->
<!-- xml解析 -->
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.3</version>
</dependency>
<!-- Mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
public class MyConfig {
private static final ClassLoader loader = ClassLoader.getSystemClassLoader();
public Connection build() {
// Mybatis主配置文件名
String resource = "mybatis-config.xml";
// 获取文件根节点
Element root = parseXML(resource);
// 获取文件对应的信息
Map<String, String> jdbcMap = parsenodes(root);
try {
Class.forName(jdbcMap.get("driverClassName"));
} catch (ClassNotFoundException e) {
throw new RuntimeException("驱动器未找到,请重新检查!");
}
Connection connect = null;
try {
connect = DriverManager.getConnection(jdbcMap.get("url"), jdbcMap.get("username"), jdbcMap.get("passWord"));
} catch (SQLException throwables) {
throw new RuntimeException("数据库连接错误,请检查路径、用户名、密码是否输入正确!");
}
return connect;
}
public static Element parseXML(String resource) {
try {
// 返回用于读取指定资源的输入流
InputStream stream = loader.getResourceAsStream(resource);
// 使用dom4j解析XML
SAXReader reader = new SAXReader();
// 使用SAX从给定流中读取文件
Document doc = reader.read(stream);
// 获取文件的根节点
return doc.getRootElement();
} catch (DocumentException e) {
throw new RuntimeException("解析 XML 时发生错误!" + resource);
}
}
private Map<String, String> parseNodes(Element node) {
// 判断根标签名称
if (!node.getName().equals("database")) {
throw new RuntimeException("数据库配置文件根标签名称必须为【database】");
}
// 存放配置文件取得的值
Map<String, String> map = new HashMap<String, String>();
map.put("driverClassName", null);
map.put("url", null);
map.put("username", null);
map.put("password", null);
// 读取property的属性内容
for (Element item : node.elements()) {
// 获取标签中存放的值,并删除其前导和结尾的空格
String value = getValue(item);
// 获取标签中 name 的名称
String name = item.attributeValue("name");
// 如果name或value为空则有对应值未输入
if (name == null || "".equals(value)) {
throw new RuntimeException("[database]: <property> 中应该包含名称和值");
}
switch (name) {
case "driverClassName" : map.put("driverClassName", value); break;
case "url" : map.put("url", value); break;
case "username" : map.put("username", value); break;
case "password" : map.put("password", value); break;
default: throw new RuntimeException("[database]: <property> 中有未知属性");
}
}
return map;
}
private static String getValue(Element node) {
return node.hasContent() ? node.getText().trim() : node.attributeValue("value").trim();
}
@SuppressWarnings(value = "rawtypes")
public MappingBean readMapper(String path) {
MappingBean bean = new MappingBean();
try {
InputStream stream = loader.getResourceAsStream(path);
SAXReader reader = new SAXReader();
Document doc = reader.read(stream);
Element root = doc.getRootElement();
// 把mapper节点的nameSpace值存为接口名
bean.setInterfaceName(root.attributeValue("nameSpace").trim());
// 用来存储方法的List
List<Mapping> list = new ArrayList<Mapping>();
//遍历根节点下所有子节点
for(Iterator rootIter = root.elementIterator(); rootIter.hasNext();) {
// 存储一条方法的信息
Mapping fun = new Mapping();
Element e = (Element) rootIter.next();
String sqlType = e.getName().trim();
String funcName = e.attributeValue("id").trim();
String sql = e.getText().trim();
String resultType = e.attributeValue("resultType").trim();
fun.setSqlType(sqlType);
fun.setFuncName(funcName);
Object newInstance = null;
try {
newInstance = Class.forName(resultType).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e1) {
e1.printStackTrace();
}
fun.setResultType(newInstance);
fun.setSql(sql);
list.add(fun);
}
bean.setList(list);
} catch (DocumentException e) {
e.printStackTrace();
}
return bean;
}
public MappingBean parseMapper(Element element) {
MappingBean bean = new MappingBean();
String namespace = element.attributeValue("namespace");
if (namespace == null) {
throw new RuntimeException("映射文件namespace不存在");
}
bean.setInterfaceName(namespace);
List<Mapping> list = new ArrayList<>();
Iterator<Element> it = element.elementIterator();
while (it.hasNext()) {
Element ele=(Element) it.next();
Mapping mapping =new Mapping();
String funcName =ele.attributeValue("id");
if (funcName==null){
throw new RuntimeException("mapper映射文件中id不存在");
}
String sqlType = ele.getName();
String paramType = ele.attributeValue("parameterType");
String resultType=ele.attributeValue("resultType");
String sql=ele.getText().trim();
mapping.setFuncName(funcName);
mapping.setSqlType(sqlType);
mapping.setParameterType(paramType);
mapping.setSql(sql);
Object object=null;
try {
object=Class.forName(resultType).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
}
mapping.setResultType(object);
list.add(mapping);
}
bean.setList(list);
return bean;
}
}
⚡由 MyConfig类 代码可以得知:
mybatis-config.xml
;<database></database>
;namespace
;resultType
(个人偷懒,没做判断);... ...MySqlSession 肯定不会自己去执行,因为不能写死所以使用动态代理来使代理类去实现具体方法。
public class MySqlSession {
private final MyExcutor excutor= new MyExcutorImpl();
private final MyConfig config = new MyConfig();
public <T> T selectValue(Mapping statement, List<Object> parameter){
return excutor.queryValue(statement, parameter);
}
public <T> T selectNull(Mapping statement){
return excutor.queryNull(statement);
}
public int deleteValue(Mapping statement, List<Object> parameter) {
return excutor.deleteValue(statement, parameter);
}
public int updateValue(Mapping statement, List<Object> parameter) {
return excutor.updateValue(statement, parameter);
}
public int insertValue(Mapping mapping, List<Object> parameter) {
return excutor.insertValue(mapping, parameter);
}
@SuppressWarnings("unchecked")
public <T> T getMapper(Class<T> clas){
//动态代理调用
return (T) Proxy.newProxyInstance(clas.getClassLoader(),new Class[]{clas},
new MySqlSessionProxy(config,this));
}
}
编写代理类,把mapper映射文件解析进来
public class MySqlSessionProxy implements InvocationHandler {
private MyConfig config;
private MySqlSession sqlSession;
public MySqlSessionProxy(MyConfig config, MySqlSession sqlSession) {
this.config = config;
this.sqlSession = sqlSession;
}
@Override
public Object invoke(Object proxy, Method method,Object[] args) {
String name = method.getDeclarinGClass().getName();
String mapperName = name.substring(name.lastIndexOf(".")+1);
MappingBean bean=config.parseMapper(MyConfig.parseXML(mapperName+".xml"));
if (bean!=null && (bean.getList()!=null && bean.getList().size()>0)){
for (Mapping mapping : bean.getList()){
if (mapping.getFuncName().equals(method.getName())) {
// 判断是否为查询语句
if ("select".equals(mapping.getSqlType().toLowerCase())) {
System.out.println("执行查询方法:" + mapping.getSql());
if (args!=null) {
System.out.println("参数:"+ Arrays.toString(args));
return sqlSession.selectValue(mapping, Arrays.asList(args));
} else {
System.out.println("参数:null");
return sqlSession.selectNull(mapping);
}
}
// 判断是否为删除语句
if ("delete".equals(mapping.getSqlType().toLowerCase())){
System.out.println("执行查询方法:"+mapping.getSql());
System.out.println("参数:"+ Arrays.toString(args));
return sqlSession.deleteValue(mapping, Arrays.asList(args));
}
// 判断是否为更新语句
if ("update".equals(mapping.getSqlType().toLowerCase())) {
System.out.println("执行查询方法:"+mapping.getSql());
System.out.println("参数:"+ Arrays.toString(args));
return sqlSession.updateValue(mapping, Arrays.asList(args));
}
// 判断是否为插入语句
if ("insert".equals(mapping.getSqlType().toLowerCase())) {
System.out.println("执行查询方法:" + mapping.getSql());
System.out.println("参数:" + Arrays.toString(args));
return sqlSession.insertValue(mapping, Arrays.asList(args));
}
}
}
}
return null;
}
}
⚡注意:通过上段代码可知,映射文件必须和接口名称保持一致。
a. 接口实体类
public class MappingBean {
private String interfaceName;
private List<Mapping> list;
// setter、getter略
}
b. 映射文件中 Sql 的实体类
public class Mapping {
private String sqlType;
private String funcName;
private String sql;
private Object resultType;
private String parameterType;
// setter、getter略
}
MyExcutor 接口
public interface MyExcutor {
// 无参查询
<T> T queryNull(Mapping mapping);
// 有参查询
<T> T queryValue(Mapping mapping, List<Object> params);
// 删除
int deleteValue(Mapping mapping, List<Object> params);
// 更新
int updateValue(Mapping mapping, List<Object> params);
// 插入
int insertValue(Mapping mapping, List<Object> params);
}
MyExcutorImpl 实现类
这里通过反射将结果转换成对象
public class MyExcutorImpl implements MyExcutor {
private MyConfig config = new MyConfig();
@Override
public <T> T queryNull(Mapping mapping) {
Connection conn = config.build();
PreparedStatement preparedStatement;
ResultSet resultSet;
Object obj;
List<Object> list = new ArrayList<>();
try {
preparedStatement=conn.prepareStatement(mapping.getSql());
if (mapping.getResultType() == null){
throw new RuntimeException("返回的映射结果不能为空!");
}
resultSet = preparedStatement.executeQuery();
int row = 0;
ResultSetMetaData rd = resultSet.getMetaData();
while (resultSet.next()){
obj=resultToObject(resultSet,mapping.getResultType());
row++;
list.add(obj);
}
System.out.println("记录行数:"+row);
} catch (SQLException e) {
e.printStackTrace();
}
return (T) list;
}
@Override
public <T> T queryValue(Mapping mapping, List<Object> params) {
Connection conn = config.build();
PreparedStatement preparedStatement;
ResultSet resultSet;
Object obj;
List<Object> list = new ArrayList<>();
try {
preparedStatement=conn.prepareStatement(mapping.getSql());
for (int i=0; i<params.size(); i++) {
preparedStatement.setString(i+1, params.get(i).toString());
}
if (mapping.getResultType() == null){
throw new RuntimeException("返回的映射结果不能为空!");
}
resultSet = preparedStatement.executeQuery();
int row = 0;
ResultSetMetaData rd = resultSet.getMetaData();
while (resultSet.next()){
obj=resultToObject(resultSet,mapping.getResultType());
row++;
list.add(obj);
}
System.out.println("记录行数:"+row);
} catch (SQLException e) {
e.printStackTrace();
}
return (T) list;
}
@Override
public int deleteValue(Mapping mapping, List<Object> params) {
Connection conn = config.build();
int rows = 0;
PreparedStatement preparedStatement=null;
try {
preparedStatement = conn.prepareStatement(mapping.getSql());
for (int i=0; i<params.size(); i++) {
preparedStatement.setString(i+1, params.get(i).toString());
}
rows = preparedStatement.executeUpdate();
if (rows != 0) {
System.out.println("删除成功,受影响行数:"+rows);
} else {
System.out.println("删除失败,数据库无相应数据...");
}
} catch (SQLException e) {
e.printStackTrace();
}
return rows;
}
@Override
public int updateValue(Mapping mapping, List<Object> params) {
Connection conn = config.build();
int rows = 0;
PreparedStatement preparedStatement=null;
try {
preparedStatement = conn.prepareStatement(mapping.getSql());
for (int i=0; i<params.size(); i++) {
preparedStatement.setString(i+1, params.get(i).toString());
}
rows = preparedStatement.executeUpdate();
if (rows != 0) {
System.out.println("修改成功,受影响行数:"+rows);
} else {
System.out.println("修改失败,数据库无相应数据...");
}
} catch (SQLException e) {
e.printStackTrace();
}
return rows;
}
@Override
public int insertValue(Mapping mapping, List<Object> params) {
Connection conn = config.build();
int rows = 0;
PreparedStatement preparedStatement=null;
try {
preparedStatement = conn.prepareStatement(mapping.getSql());
for (int i=0; i<params.size(); i++) {
preparedStatement.setString(i+1, params.get(i).toString());
}
try {
rows = preparedStatement.executeUpdate();
if (rows != 0) {
System.out.println("插入成功,受影响行数:"+rows);
} else {
System.out.println("插入失败...");
}
} catch (SQLException throwables) {
throw new RuntimeException("插入重复 \"Key\" 值数据");
}
} catch (SQLException e) {
e.printStackTrace();
}
return rows;
}
private <T> T resultToObject(ResultSet rs, Object object) {
Object obj=null;
try {
Class<?> cls = object.getClass();
obj=cls.newInstance();
//获取结果集元数据(获取此 ResultSet 对象的列的编号、类型和属性。)
ResultSetMetaData rd=rs.getMetaData();
for (int i = 0; i < rd.getColumnCount(); i++) {
//获取列名
String columnName=rd.getColumnLabel(i+1);
//组合方法名
String methodName="set"+columnName.substring(0, 1).toUpperCase()+columnName.substring(1);
//获取列类型
int columnType=rd.getColumnType(i+1);
Method method=null;
switch(columnType) {
case java.sql.Types.VARCHAR:
case java.sql.Types.CHAR:
method=cls.getMethod(methodName, String.class);
method.invoke(obj, rs.getString(columnName));
break;
case java.sql.Types.INTEGER:
method=cls.getMethod(methodName, Integer.class);
method.invoke(obj, rs.getInt(columnName));
break;
default:
break;
}
}
} catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException | SQLException e) {
e.printStackTrace();
}
return (T) obj;
}
}
<!-- Https://mvnrepository.com/artifact/org.dom4j/dom4j -->
<!-- xml解析 -->
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.3</version>
</dependency>
<!-- Mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<!-- 自己写的Mybatis,首先要将其放入本地仓库 -->
<dependency>
<groupId>top.kk233</groupId>
<artifactId>SimpMybatis</artifactId>
<version>1.0.0</version>
</dependency>
⚡Maven导入本地Jar包方法自行百度,这里就不赘述。
这里提供一个我测试的,你们可以自行创建其他的
CREATE DATABASE IF NOT EXISTS `test`;
USE `test`;
CREATE TABLE `user` (
`id` INT ( 10 ) NOT NULL,
`sex` VARCHAR ( 2 ) NOT NULL,
`password` VARCHAR ( 255 ) DEFAULT NULL,
`username` VARCHAR ( 255 ) DEFAULT NULL,
PRIMARY KEY ( `id` )
) ENGINE = INNODB AUTO_INCREMENT = 2 DEFAULT CHARSET = utf8;
INSERT INTO `test`.`user` ( `id`, `sex`, `password`, `username` )
VALUES
( 1, '男', '12344', '五六' ),
( 2, '女', '12643', '张三' ),
( 3, '男', '1245453', '李四' );
public class User {
private Integer id;
private String sex;
private String password;
private String username;
// setter、getter略
}
public interface UserMapper {
List<User> getUsers();
List<User> getUserBySexAndName(String sex, String username);
int deleteUserById(Integer id);
int updateUserByName(String username, String password);
int insertUser(int id, String sex, String password, String username);
}
<?xml version="1.0" encoding="UTF-8"?>
<mapper namespace="top.kk233.mapper.UserMapper">
<select id="getUsers" resultType="top.kk233.pojo.User">
SELECT * FROM user
</select>
<select id="getUserBySexAndName" resultType="top.kk233.pojo.User">
select * from user where sex=? and username=?
</select>
<delete id="deleteUserById" resultType="top.kk233.pojo.User">
delete from user where id=?
</delete>
<update id="updateUserByName" resultType="top.kk233.pojo.User">
update user set password=? where username=?
</update>
<insert id="insertUser" resultType="top.kk233.pojo.User">
insert into user values(?,?,?,?)
</insert>
</mapper>
<?xml version="1.0" encoding="UTF-8"?>
<database>
<property name="driverClassName">com.mysql.jdbc.Driver</property>
<property name="url">jdbc:mysql://localhost:3306/test?useSSL=false</property>
<property name="username">root</property>
<property name="password">124760</property>
</database>
public class app {
public static void main(String[] args) {
MySqlSession sql = new MySqlSession();
UserMapper mapper = sql.getMapper(UserMapper.class);
List<User> users = mapper.getUsers();
users.forEach(System.out::println);
System.out.println("==========================");
List<User> users1 = mapper.getUserBySexAndName("女", "张三");
users1.forEach(System.out::println);
System.out.println("==========================");
mapper.deleteUserById(1);
System.out.println("==========================");
mapper.updateUserByName("五六", "女");
System.out.println("==========================");
mapper.insertUser(10, "男", "123123", "五七");
}
}
👌测试成功,这就是本人所手写的Mybatis,虽然比较简单,但还是学习到了很多东西。
💥项目放在 gitee 上有需要自行下载,觉得可以还请点个Star
到此这篇关于Mybatis实现动态增删改查功能的示例代码的文章就介绍到这了,更多相关mybatis增删改查内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Mybatis实现动态增删改查功能的示例代码
本文链接: https://lsjlt.com/news/122759.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0