這篇文章主要為大家展示了“MyBatis如何實現注冊及獲取Mapper”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“MyBatis如何實現注冊及獲取Mapper”這篇文章吧。
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </dependency>
public interface BlogMapper { List<Blog> selectBlog(String id); }
<?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="mybatis.source.study.BlogMapper"> <select id="selectBlog" resultType="mybatis.source.study.Blog"> select * from t_blog where id= #{id} </select> </mapper>
BlogMapper.xml放在resource目錄下與BlogMapper.java包路徑相同的路徑下
public class MyBatisDemo { public static void main(String[] args) { //創建數據源 DataSource dataSource = getDataSource(); TransactionFactory transactionFactory = new JdbcTransactionFactory(); //創建sql運行環境 Environment environment = new Environment("development", transactionFactory, dataSource); //創建mybatis的所有配置 Configuration configuration = new Configuration(environment); //注冊mapper configuration.addMapper(BlogMapper.class); // configuration.addInterceptor(new PaginationInterceptor()); //根據配置創建sql會話工廠 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); SqlSession sqlSession = sqlSessionFactory.openSession(); BlogMapper mapper = sqlSession.getMapper(BlogMapper.class); System.out.println(mapper.selectBlog("001")); } private static DataSource getDataSource() { DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setUrl("jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8&serverTimezone=Asia/Shanghai"); druidDataSource.setUsername("root"); druidDataSource.setPassword("root"); return druidDataSource; }
這塊就是判斷這個mapper.xml解析過沒有,解析是在 parser.parse();中做的,來看
loadXmlResource();根據xml解析每個mapper接口的方法,將得到的MapperStatement放進了configuration,然后記錄該xml的namespace表示已經處理過。具體調用鏈:
loadXmlResource()–>xmlParser.parse()–>configurationElement(parser.evalNode("/mapper"))–> buildStatementFromContext(context.evalNodes(“select|insert|update|delete”))–> buildStatementFromContext(list, null)–>statementParser.parseStatementNode()–>builderAssistant.addMappedStatement–>configuration.addMappedStatement(statement);
parseStatement(method);根據注解解析每個mapper接口的方法,因此xml和注解可以同時使用。但是同一個方法兩者同時使用會報錯
放入knownMappers的是MapperProxyFactory,它是一個Mapper代理的工廠,這個工廠提供newInstance方法,產生一個代理類(也就是BlogMapper接口的代理實現類),調用BlogMapper所有的方法將在MapperProxy的invoke方法中執行
getMapper會調用MapperRegistry的getMapper從knownMappers中獲取代理工廠,再調用newInstance方法產生一個代理類MapperProxy。
在執行mapper.selectBlog(“001”)時,就會調用MapperProxy的invoke方法
根據method(selectBlog)生成對應的MapperMethod,并將MapperMethod放入本地緩存。
mapperMethod.execute(sqlSession, args);執行真正的sql邏輯。
MapperMethod的構造方法,根據接口信息、方法信息、配置信息得到SqlCommand(sql名稱、類型)、method(方法簽名),方便后續執行命令、處理結果集等。
以上是“MyBatis如何實現注冊及獲取Mapper”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。