diff --git a/code/IPersistence/src/main/java/com/lagou/config/BoundSql.java b/code/IPersistence/src/main/java/com/lagou/config/BoundSql.java index 1066b5746f898c2cf5bec195d9aadb64b53dbb77..1e428aabb00ba6d4f95ec5a3d0595f2b05bfe76b 100644 --- a/code/IPersistence/src/main/java/com/lagou/config/BoundSql.java +++ b/code/IPersistence/src/main/java/com/lagou/config/BoundSql.java @@ -31,4 +31,12 @@ public class BoundSql { public void setParameterMappingList(List parameterMappingList) { this.parameterMappingList = parameterMappingList; } + + @Override + public String toString() { + return "BoundSql{" + + "sqlText='" + sqlText + '\'' + + ", parameterMappingList=" + parameterMappingList + + '}'; + } } diff --git a/code/IPersistence/src/main/java/com/lagou/config/XMLMapperBuilder.java b/code/IPersistence/src/main/java/com/lagou/config/XMLMapperBuilder.java index 26e5c92c49c30f0a8f991eb05f9737c044b0b59a..d69d0cebbda2314ca99a5dfd27e2f099ae7ac7d5 100644 --- a/code/IPersistence/src/main/java/com/lagou/config/XMLMapperBuilder.java +++ b/code/IPersistence/src/main/java/com/lagou/config/XMLMapperBuilder.java @@ -26,6 +26,10 @@ public class XMLMapperBuilder { String namespace = rootElement.attributeValue("namespace"); List list = rootElement.selectNodes("//select"); + addList(rootElement, list, "//insert"); + addList(rootElement, list, "//delete"); + addList(rootElement, list, "//update"); + for (Element element : list) { String id = element.attributeValue("id"); String resultType = element.attributeValue("resultType"); @@ -43,5 +47,12 @@ public class XMLMapperBuilder { } + private void addList(Element rootElement, List list, String xpathExpression) { + List insertList = rootElement.selectNodes(xpathExpression); + if (insertList !=null && insertList.size() >0){ + list.addAll(insertList); + } + } + } diff --git a/code/IPersistence/src/main/java/com/lagou/sqlSession/DefaultSqlSession.java b/code/IPersistence/src/main/java/com/lagou/sqlSession/DefaultSqlSession.java index de089b9fc91d707142715480eb93eb37e863474e..5e93082f01c63401c85f9fd646dedc06ef9b8021 100644 --- a/code/IPersistence/src/main/java/com/lagou/sqlSession/DefaultSqlSession.java +++ b/code/IPersistence/src/main/java/com/lagou/sqlSession/DefaultSqlSession.java @@ -5,6 +5,7 @@ import com.lagou.pojo.MappedStatement; import java.lang.reflect.*; import java.util.List; +import java.util.Locale; public class DefaultSqlSession implements SqlSession { @@ -14,6 +15,13 @@ public class DefaultSqlSession implements SqlSession { this.configuration = configuration; } + @Override + public int update(String statementid, Object... params) throws Exception { + simpleExecutor simpleExecutor = new simpleExecutor(); + MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementid); + return simpleExecutor.update(configuration, mappedStatement, params); + } + @Override public List selectList(String statementid, Object... params) throws Exception { @@ -33,8 +41,6 @@ public class DefaultSqlSession implements SqlSession { }else { throw new RuntimeException("查询结果为空或者返回结果过多"); } - - } @Override @@ -52,17 +58,32 @@ public class DefaultSqlSession implements SqlSession { String statementId = className+"."+methodName; - // 准备参数2:params:args - // 获取被调用方法的返回值类型 - Type genericReturnType = method.getGenericReturnType(); - // 判断是否进行了 泛型类型参数化 - if(genericReturnType instanceof ParameterizedType){ - List objects = selectList(statementId, args); - return objects; + MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId); + if (mappedStatement == null){ + throw new RuntimeException("方法未定义!"); } - - return selectOne(statementId,args); - + String sql = mappedStatement.getSql(); + if (sql.toLowerCase(Locale.ROOT).startsWith( "select" )){ + // 准备参数2:params:args + // 获取被调用方法的返回值类型 + Type genericReturnType = method.getGenericReturnType(); + // 判断是否进行了 泛型类型参数化 + if(genericReturnType instanceof ParameterizedType){ + List objects = selectList(statementId, args); + return objects; + } + return selectOne(statementId,args); + } + else if (sql.toLowerCase(Locale.ROOT).startsWith( "insert" )){ + return update(statementId , args); + } + else if (sql.toLowerCase(Locale.ROOT).startsWith( "delete" )){ + return update(statementId , args); + } + else if (sql.toLowerCase(Locale.ROOT).startsWith( "update" )){ + return update(statementId , args); + } + return null; } }); diff --git a/code/IPersistence/src/main/java/com/lagou/sqlSession/Executor.java b/code/IPersistence/src/main/java/com/lagou/sqlSession/Executor.java index d73a568d02f42ff1752da1ec560f8870cf1e3258..19547a77d9db22f7b79a68f20e791f7eab67e214 100644 --- a/code/IPersistence/src/main/java/com/lagou/sqlSession/Executor.java +++ b/code/IPersistence/src/main/java/com/lagou/sqlSession/Executor.java @@ -3,10 +3,14 @@ package com.lagou.sqlSession; import com.lagou.pojo.Configuration; import com.lagou.pojo.MappedStatement; +import java.beans.IntrospectionException; +import java.lang.reflect.InvocationTargetException; +import java.sql.SQLException; import java.util.List; public interface Executor { public List query(Configuration configuration,MappedStatement mappedStatement,Object... params) throws Exception; + int update(Configuration configuration, MappedStatement mappedStatement, Object[] params) throws SQLException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException, IntrospectionException, InvocationTargetException; } diff --git a/code/IPersistence/src/main/java/com/lagou/sqlSession/SqlSession.java b/code/IPersistence/src/main/java/com/lagou/sqlSession/SqlSession.java index 758e1fa74090b367cacd1de51e01191c6129e152..f7d9d5b1de7b2648921dd1021c95b76a4e019777 100644 --- a/code/IPersistence/src/main/java/com/lagou/sqlSession/SqlSession.java +++ b/code/IPersistence/src/main/java/com/lagou/sqlSession/SqlSession.java @@ -10,6 +10,7 @@ public interface SqlSession { //根据条件查询单个 public T selectOne(String statementid,Object... params) throws Exception; + public int update(String statementid,Object... params) throws Exception; //为Dao接口生成代理实现类 public T getMapper(Class mapperClass); diff --git a/code/IPersistence/src/main/java/com/lagou/sqlSession/simpleExecutor.java b/code/IPersistence/src/main/java/com/lagou/sqlSession/simpleExecutor.java index 62a19678e09f0e2ea7dd6545354858e9073b3b14..5be4c967fdfb30309886c4085d3056ab1b41e1e9 100644 --- a/code/IPersistence/src/main/java/com/lagou/sqlSession/simpleExecutor.java +++ b/code/IPersistence/src/main/java/com/lagou/sqlSession/simpleExecutor.java @@ -8,26 +8,24 @@ import com.lagou.utils.GenericTokenParser; import com.lagou.utils.ParameterMapping; import com.lagou.utils.ParameterMappingTokenHandler; +import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; +import java.sql.*; import java.util.ArrayList; import java.util.List; public class simpleExecutor implements Executor { - - @Override //user - public List query(Configuration configuration, MappedStatement mappedStatement, Object... params) throws Exception { + @Override + public int update(Configuration configuration, MappedStatement mappedStatement, Object[] params) throws SQLException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException, IntrospectionException, InvocationTargetException { // 1. 注册驱动,获取连接 Connection connection = configuration.getDataSource().getConnection(); // 2. 获取sql语句 : select * from user where id = #{id} and username = #{username} - //转换sql语句: select * from user where id = ? and username = ? ,转换的过程中,还需要对#{}里面的值进行解析存储 + //转换sql语句: select * from user where id = ? and username = ? ,转换的过程中,还需要对#{}里面的值进行解析存储 String sql = mappedStatement.getSql(); BoundSql boundSql = getBoundSql(sql); @@ -35,24 +33,34 @@ public class simpleExecutor implements Executor { PreparedStatement preparedStatement = connection.prepareStatement(boundSql.getSqlText()); // 4. 设置参数 - //获取到了参数的全路径 - String paramterType = mappedStatement.getParamterType(); - Class paramtertypeClass = getClassType(paramterType); + //获取到了参数的全路径 + handleParam(mappedStatement, boundSql, preparedStatement, params[0]); - List parameterMappingList = boundSql.getParameterMappingList(); - for (int i = 0; i < parameterMappingList.size(); i++) { - ParameterMapping parameterMapping = parameterMappingList.get(i); - String content = parameterMapping.getContent(); + // 5. 执行sql + int res = preparedStatement.executeUpdate(); + return res; - //反射 - Field declaredField = paramtertypeClass.getDeclaredField(content); - //暴力访问 - declaredField.setAccessible(true); - Object o = declaredField.get(params[0]); + } - preparedStatement.setObject(i+1,o); + @Override //user + public List query(Configuration configuration, MappedStatement mappedStatement, Object... params) throws Exception { + // 1. 注册驱动,获取连接 + Connection connection = configuration.getDataSource().getConnection(); + + // 2. 获取sql语句 : select * from user where id = #{id} and username = #{username} + //转换sql语句: select * from user where id = ? and username = ? ,转换的过程中,还需要对#{}里面的值进行解析存储 + String sql = mappedStatement.getSql(); + BoundSql boundSql = getBoundSql(sql); + + // 3.获取预处理对象:preparedStatement + PreparedStatement preparedStatement = connection.prepareStatement(boundSql.getSqlText()); + // 4. 设置参数 + Object param = null; + if(params !=null && params.length > 0){ + param = params[0]; } + handleParam(mappedStatement, boundSql, preparedStatement, param); // 5. 执行sql @@ -88,6 +96,31 @@ public class simpleExecutor implements Executor { } + private void handleParam(MappedStatement mappedStatement, BoundSql boundSql, PreparedStatement preparedStatement, Object param) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, SQLException { + //获取到了参数的全路径 + String paramterType = mappedStatement.getParamterType(); + Class paramtertypeClass = getClassType(paramterType); + + List parameterMappingList = boundSql.getParameterMappingList(); + + System.out.println( boundSql ); + System.out.print("参数: "); + for (int i = 0; i < parameterMappingList.size(); i++) { + ParameterMapping parameterMapping = parameterMappingList.get(i); + String content = parameterMapping.getContent(); + + //反射 + Field declaredField = paramtertypeClass.getDeclaredField(content); + //暴力访问 + declaredField.setAccessible(true); + Object o = declaredField.get(param); + + preparedStatement.setObject(i+1,o); + System.out.print(o + " "); + } + System.out.println(); + } + private Class getClassType(String paramterType) throws ClassNotFoundException { if(paramterType!=null){ Class aClass = Class.forName(paramterType); diff --git a/code/IPersistence/src/main/java/com/lagou/utils/ParameterMapping.java b/code/IPersistence/src/main/java/com/lagou/utils/ParameterMapping.java index 6011f809f03ac431ce9790f80f2c76e863c764c1..9754ce74c0e685cde7790fae9d142ad3b647f4ce 100644 --- a/code/IPersistence/src/main/java/com/lagou/utils/ParameterMapping.java +++ b/code/IPersistence/src/main/java/com/lagou/utils/ParameterMapping.java @@ -15,4 +15,11 @@ public class ParameterMapping { public void setContent(String content) { this.content = content; } + + @Override + public String toString() { + return "ParameterMapping{" + + "content='" + content + '\'' + + '}'; + } } diff --git a/code/IPersistence_test/src/main/java/com/lagou/test/IPersistenceTest.java b/code/IPersistence_test/src/main/java/com/lagou/test/IPersistenceTest.java index b2e0c33bfd989777580e391a94777e2894b074ce..6b0c7e3942086d02ebdca3694d0b017d9c42674e 100644 --- a/code/IPersistence_test/src/main/java/com/lagou/test/IPersistenceTest.java +++ b/code/IPersistence_test/src/main/java/com/lagou/test/IPersistenceTest.java @@ -16,38 +16,48 @@ import java.util.List; public class IPersistenceTest { - private SqlSession sqlSession; + private IUserDao userDao; @Before public void before() throws PropertyVetoException, DocumentException { InputStream resourceAsSteam = Resources.getResourceAsSteam("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsSteam); - sqlSession = sqlSessionFactory.openSession(); + SqlSession sqlSession = sqlSessionFactory.openSession(); + userDao = sqlSession.getMapper(IUserDao.class); } @Test public void testInsert() throws Exception { - + User user = new User(); + user.setUsername("test"); + int num = userDao.insertOne(user); + System.out.println( "insertOne num is " + num ); } @Test public void testUpdate() throws Exception { - + User user = new User(); + user.setId(4); + user.setUsername("testU"); + int num = userDao.updateById(user); + System.out.println( "updateById num is " + num ); } @Test public void testDelete() throws Exception { - + User user = new User(); + user.setId(4); + int num = userDao.deleteById(user); + System.out.println( "updateById num is " + num ); } @Test - public void test() throws Exception { + public void tesFindAll() throws Exception { //调用 User user = new User(); user.setId(1); user.setUsername("张三"); /* User user2 = sqlSession.selectOne("user.selectOne", user); - System.out.println(user2);*/ /* List users = sqlSession.selectList("user.selectList"); @@ -55,13 +65,20 @@ public class IPersistenceTest { System.out.println(user1); }*/ - IUserDao userDao = sqlSession.getMapper(IUserDao.class); - List all = userDao.findAll(); for (User user1 : all) { System.out.println(user1); } + } + @Test + public void tesFindByCondition() throws Exception { + //调用 + User user = new User(); + user.setId(1); + user.setUsername("lisi"); + User res = userDao.findByCondition(user); + System.out.println(res); } } diff --git a/code/IPersistence_test/src/main/resources/UserMapper.xml b/code/IPersistence_test/src/main/resources/UserMapper.xml index e69309f0cb2919f9d344b687dc3294cbc1e93726..f83e771d40a233abe7965f6a359fef80fa4238ff 100644 --- a/code/IPersistence_test/src/main/resources/UserMapper.xml +++ b/code/IPersistence_test/src/main/resources/UserMapper.xml @@ -16,14 +16,14 @@ - + INSERT INTO `user`( `username`, `password`, `birthday`) VALUES ( #{username}, #{password}, #{birthday}); - + DELETE FROM `user` WHERE `id` = #{id} - + UPDATE `user` SET `username` = #{username} WHERE `id` = #{id} \ No newline at end of file diff --git "a/\344\275\234\344\270\232\351\252\214\350\257\201\350\265\204\346\226\231/\344\275\234\344\270\232\350\256\262\350\247\243.pdf" "b/\344\275\234\344\270\232\351\252\214\350\257\201\350\265\204\346\226\231/\344\275\234\344\270\232\350\256\262\350\247\243.pdf" new file mode 100644 index 0000000000000000000000000000000000000000..c7342e1bf3509983a5af16fe55aaceeabb4b113a Binary files /dev/null and "b/\344\275\234\344\270\232\351\252\214\350\257\201\350\265\204\346\226\231/\344\275\234\344\270\232\350\256\262\350\247\243.pdf" differ diff --git "a/\345\220\254\350\257\276\347\254\224\350\256\260/1 \350\207\252\345\256\232\344\271\211\346\214\201\344\271\205\345\214\226\346\241\206\346\236\266.pdf" "b/\345\220\254\350\257\276\347\254\224\350\256\260/1 \350\207\252\345\256\232\344\271\211\346\214\201\344\271\205\345\214\226\346\241\206\346\236\266.pdf" new file mode 100644 index 0000000000000000000000000000000000000000..463719d6a4289bc1e8b98bdad62fc66aa043ea69 Binary files /dev/null and "b/\345\220\254\350\257\276\347\254\224\350\256\260/1 \350\207\252\345\256\232\344\271\211\346\214\201\344\271\205\345\214\226\346\241\206\346\236\266.pdf" differ diff --git "a/\345\220\254\350\257\276\347\254\224\350\256\260/2 Mybatis\345\237\272\347\241\200\345\233\236\351\241\276\343\200\201\351\253\230\347\272\247\345\272\224\347\224\250.pdf" "b/\345\220\254\350\257\276\347\254\224\350\256\260/2 Mybatis\345\237\272\347\241\200\345\233\236\351\241\276\343\200\201\351\253\230\347\272\247\345\272\224\347\224\250.pdf" new file mode 100644 index 0000000000000000000000000000000000000000..9bd750918862864f95f0feb6dcac8fee73a5b4e0 Binary files /dev/null and "b/\345\220\254\350\257\276\347\254\224\350\256\260/2 Mybatis\345\237\272\347\241\200\345\233\236\351\241\276\343\200\201\351\253\230\347\272\247\345\272\224\347\224\250.pdf" differ diff --git "a/\345\220\254\350\257\276\347\254\224\350\256\260/3 Mybatis\346\272\220\347\240\201\350\247\243\346\236\220.pdf" "b/\345\220\254\350\257\276\347\254\224\350\256\260/3 Mybatis\346\272\220\347\240\201\350\247\243\346\236\220.pdf" new file mode 100644 index 0000000000000000000000000000000000000000..0b42a8238a4d97384723b920e2e8baa983cfe6cc Binary files /dev/null and "b/\345\220\254\350\257\276\347\254\224\350\256\260/3 Mybatis\346\272\220\347\240\201\350\247\243\346\236\220.pdf" differ