项目结构 1 2 3 4 5 6 7 8 9 10 . ├── IPersistence │ ├── IPersistence.iml │ ├── pom.xml │ └── src └── IPersistence_test ├── IPersistence_test.iml ├── pom.xml ├── src └── target
具体代码 sqlMapperConfig.xml配置文件 1 2 3 4 5 6 7 8 9 10 11 12 <configuration > <dataSource > <property name ="driverClass" value ="com.mysql.jdbc.Driver" > </property > <property name ="jdbcUrl" value ="jdbc:mysql://localhost:3306/zdy_mybatis" > </property > <property name ="username" value ="root" > </property > <property name ="password" value ="123456" > </property > </dataSource > <mapper resource ="UserMapper.xml" /> </configuration >
UserMapper.xml配置文件 1 2 3 4 5 6 7 8 9 <mapper namespace ="user" > <select id ="selectList" resultType ="com.terwergreen.pojo.User" > select * from user </select > <select id ="selectOne" resultType ="com.terwergreen.pojo.User" parameterType ="com.terwergreen.pojo.User" > select * from user where id = #{id} and username = #{username} </select > </mapper >
读取资源处理,Resources类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 object Resources { @JvmStatic fun getResourceAsStream (path: String ?) : InputStream { return Resources::class .java.classLoader.getResourceAsStream(path) } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class Resources { public static InputStream getResourceAsStream (String path) { InputStream inputStream = Resources.class.getClassLoader().getResourceAsStream(path); return inputStream; } }
SqlSessionFactoryBuider工厂构建对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class SqlSessionFactoryBuilder { @Throws(DocumentException::class, PropertyVetoException::class) fun build (ips: InputStream ?) : SqlSessionFactory { val xmlConfigBuilder = XmlConfigBuilder() val configuration = xmlConfigBuilder.parse(ips) return DefaultSqlSessionFactory(configuration) } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class SqlSessionFactoryBuilder { public SqlSessionFactory build (InputStream in) throws DocumentException, PropertyVetoException { XmlConfigBuilder xmlConfigBuilder = new XmlConfigBuilder (); Configuration configuration = xmlConfigBuilder.parse(in); DefaultSqlSessionFactory sqlSessionFactory = new DefaultSqlSessionFactory (configuration); return sqlSessionFactory; } }
配置文件解析
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 class XmlConfigBuilder { private val configuration: Configuration init { configuration = Configuration() } @Throws(DocumentException::class, PropertyVetoException::class) fun parse (ips: InputStream ?) : Configuration { val document = SAXReader().read(ips) val rootElement = document.rootElement val list: List<Element?> = rootElement.selectNodes("//property" ) val properties = Properties() for (element in list) { val name = element!!.attributeValue("name" ) val value = element.attributeValue("value" ) properties.setProperty(name, value) } val comboPooledDataSource = ComboPooledDataSource() comboPooledDataSource.driverClass = properties.getProperty("driverClass" ) comboPooledDataSource.jdbcUrl = properties.getProperty("jdbcUrl" ) comboPooledDataSource.user = properties.getProperty("username" ) comboPooledDataSource.password = properties.getProperty("password" ) configuration.dataSource = comboPooledDataSource val mapperList: List<Element?> = rootElement.selectNodes("//mapper" ) for (element in mapperList) { val mapperPath = element!!.attributeValue("resource" ) val resourceAsStream = Resources.getResourceAsStream(mapperPath) val xmlMapperBuilder = XmlMapperBuilder(configuration) xmlMapperBuilder.parse(resourceAsStream) } return configuration } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 public class XmlConfigBuilder { private Configuration configuration; public XmlConfigBuilder () { configuration = new Configuration (); } public Configuration parse (InputStream in) throws DocumentException, PropertyVetoException { Document document = new SAXReader ().read(in); Element rootElement = document.getRootElement(); List<Element> list = rootElement.selectNodes("//property" ); Properties properties = new Properties (); for (Element element : list) { String name = element.attributeValue("name" ); String value = element.attributeValue("value" ); properties.setProperty(name, value); } ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource (); comboPooledDataSource.setDriverClass(properties.getProperty("driverClass" )); comboPooledDataSource.setJdbcUrl(properties.getProperty("jdbcUrl" )); comboPooledDataSource.setUser(properties.getProperty("username" )); comboPooledDataSource.setPassword(properties.getProperty("password" )); configuration.setDataSource(comboPooledDataSource); List<Element> mapperList= rootElement.selectNodes("//mapper" ); for (Element element : mapperList) { String mapperPath = element.attributeValue("resource" ); InputStream resourceAsStream = Resources.getResourceAsStream(mapperPath); XmlMapperBuilder xmlMapperBuilder = new XmlMapperBuilder (configuration); xmlMapperBuilder.parse(resourceAsStream); } return configuration; } }
mapper映射文件解析
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 class XmlMapperBuilder (private val configuration: Configuration) { @Throws(DocumentException::class) fun parse (`in `: InputStream ?) { val document = SAXReader().read(`in `) val rootElement = document.rootElement val namespace = rootElement.attributeValue("namespace" ) val list: List<Element?> = rootElement.selectNodes("//select" ) for (element in list) { val id = element!!.attributeValue("id" ) val resultType = element.attributeValue("resultType" ) val parameterType = element.attributeValue("parameterType" ) val sqlText = element.textTrim val mappedStatement = MappedStatement() mappedStatement.statementId = id mappedStatement.resultType = resultType mappedStatement.parameterType = parameterType mappedStatement.sql = sqlText val mappedStatementMap = configuration.mappedStatementMap val statementId = "$namespace .$id " mappedStatementMap[statementId] = mappedStatement } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 public class XmlMapperBuilder { private Configuration configuration; public XmlMapperBuilder (Configuration configuration) { this .configuration = configuration; } public void parse (InputStream in) throws DocumentException { Document document = new SAXReader ().read(in); Element rootElement = document.getRootElement(); String namespace = rootElement.attributeValue("namespace" ); List<Element> list = rootElement.selectNodes("//select" ); for (Element element : list) { String id = element.attributeValue("id" ); String resultType = element.attributeValue("resultType" ); String parameterType = element.attributeValue("parameterType" ); String sqlText = element.getTextTrim(); MappedStatement mappedStatement = new MappedStatement (); mappedStatement.setStatementId(id); mappedStatement.setResultType(resultType); mappedStatement.setParameterType(parameterType); mappedStatement.setSql(sqlText); Map<String, MappedStatement> mappedStatementMap = configuration.getMappedStatementMap(); String statementId = namespace + "." + id; mappedStatementMap.put(statementId, mappedStatement); } } }
SqlSession的具体实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 interface SqlSession { @Throws(Exception::class) fun <E> selectList (statementId: String ?, vararg params: Any ?) : List<E>? @Throws(Exception::class) fun <T> selectOne (statementId: String ?, vararg params: Any ?) : T fun <T> getMapper (mapperClass: Class <*>?) : T }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public interface SqlSession { public <E> List<E> selectList (String statementId, Object... params) throws Exception; public <T> T selectOne (String statementId, Object... params) throws Exception; public <T> T getMapper (Class<?> mapperClass) ; }
文章更新历史
2022/05/08 feat:增加Kotlin实现。