1、创建项目maven,方便依赖下载。使用的jar如下:
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
|
<dependencies> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version> 5.0 . 8 .release</version> </dependency> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis-spring</artifactid> <version> 2.0 . 0 </version> </dependency> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis</artifactid> <version> 3.4 . 6 </version> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version> 5.1 . 34 </version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-jdbc</artifactid> <version> 5.0 . 8 .release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</artifactid> <version> 5.0 . 8 .release</version> </dependency> </dependencies> |
2、创建包com >config、dao、service、test
3、使用spring创建appconfig文件,创建bean>sqlsessionfactorybean、datasourcebean
1、spring注解@configuration,说明是配置层
2、注册扫描映射
3、注册包扫描器
4、创建sqlsessionfactorybean
5、创建数据源bean
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
|
package com.config; import org.mybatis.spring.sqlsessionfactorybean; import org.mybatis.spring.annotation.mapperscan; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.componentscan; import org.springframework.context.annotation.configuration; import org.springframework.jdbc.datasource.drivermanagerdatasource; import javax.sql.datasource; @configuration @mapperscan ( "com.dao" ) //注解 与xml<mybatis:scan base-package="org.mybatis.spring.sample.mapper" /> //注册包中递归搜索映射器 @componentscan ( "com" ) //注册bean public class appconfig { @bean @autowired public sqlsessionfactorybean sqlsessionfactorybean(datasource datasource ){ sqlsessionfactorybean sqlsessionfactorybean = new sqlsessionfactorybean(); sqlsessionfactorybean.setdatasource(datasource); return sqlsessionfactorybean; } @bean public datasource getdatasource(){ drivermanagerdatasource datasource = new drivermanagerdatasource(); datasource.setdriverclassname( "com.mysql.jdbc.driver" ); datasource.setusername( "root" ); datasource.setpassword( "110226wjwj" ); datasource.seturl( "jdbc:mysql://localhost:3306/mybatis_wjw?useunicode=true&characterencoding=utf-8&usessl=false" ); return datasource; } } |
4、创建dao接口
1、创建query方法并使用注解@select(mybatis提供,mybatis-spring官网有相关解释)
1
2
3
4
5
6
7
8
|
package com.dao; import org.apache.ibatis.annotations.select; import java.util.list; import java.util.map; public interface userdao { @select ( "select * from t_user where tid =3" ) public list<map> query(); } |
5、创建服务层
1、注解服务层
2、引入daobean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.service; import com.dao.userdao; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; //spring注解中service与component意思差不多,区别在于component是中立注解,而service是业务逻辑层的注解 //@component @service public class userservice { @autowired userdao userdao; public void query(){ system.out.println(userdao.query()); } } |
6、测试
1、创建application
2、创建service
1
2
3
4
5
6
7
8
9
10
11
|
package com.test; import com.config.appconfig; import com.service.userservice; import org.springframework.context.annotation.annotationconfigapplicationcontext; public class maintest { public static void main(string args[]){ annotationconfigapplicationcontext acc = new annotationconfigapplicationcontext(appconfig. class ); userservice us = acc.getbean(userservice. class ); us.query(); } } |
jar包之家:https://mvnrepository.com/artifact
使用spring依赖注入mapper 根据官网提示需要
<mybatis:scan base-package="org.mybatis.spring.sample.mapper" />==@mapperscan(“需要注入的包”)
mybatis缺点:使用xml方式与dao开发结合出现严重的臃肿现象,需要维护很多sql语句。
测试的时候出现这个问题。(版本导致,我直接将最新[mybatis-spring]的导入进来就没问题了)
上面的问题解决后有出现这个问题,此问题出现的原因是java compiler改成8就ok了
测试结果:
重点:
mybatis运行原理
我们可以看到,再测试类中用的是userservice对象调用dao接口中的query,但是mybatis是如何实现将接口转换成对象的呢? 答案:动态代理 ,我们常用的代理(proxy)一共有两种:cglib和jdk两用代理模式
无论哪一种最终都是使用反射机制进行代理。详情自己查看度娘(哈哈@0@)
mybatis源码解析
defaultsqlsession下如何实现返回一条数据的:底层调用的是selectlist方法,对返回的结果集进行数量判断如果==1则直接放回,>1直接抛出toomanyresultsexception(感觉很傻,有些妄自菲薄!)
1
2
3
4
5
6
7
8
9
10
|
public <t> t selectone(string statement, object parameter) { list<t> list = this .selectlist(statement, parameter); if (list.size() == 1 ) { return list.get( 0 ); } else if (list.size() > 1 ) { throw new toomanyresultsexception( "expected one result (or null) to be returned by selectone(), but found: " + list.size()); } else { return null ; } } |
下面我们继续看selectlist是如何实现的
1
2
3
4
5
6
7
8
9
10
11
12
|
public <e> list<e> selectlist(string statement, object parameter, rowbounds rowbounds) { list var5; try { mappedstatement ms = this .configuration.getmappedstatement(statement); var5 = this .executor.query(ms, this .wrapcollection(parameter), rowbounds, executor.no_result_handler); } catch (exception var9) { throw exceptionfactory.wrapexception( "error querying database. cause: " + var9, var9); } finally { errorcontext.instance().reset(); } return var5; } |
我们来看看这个defaultsqlsession.configuration.getmappedstatement方法具体是什么
结论:再启动项目的时候,mybatis会进行初始化,这个初始化就是将我们的"包+类+方法名"作为key 和 sql语句作为value 的键值对形式赋给下面map类型的mappedstatements
protected final map<string, mappedstatement> mappedstatements;
这也是为什么我们使用xml方式一定要将方法名字与id对应上才能使用,如果对应不上再进行id传值的时候找不到对应的key。
继续往下分析:
已发帖子询问大神具体是什么原因导致不进入124行,等大佬们回答后我将公布结果。直接看看executor是什么鬼
是一个接口,https://blog.csdn.net/ykzhen2015/article/details/50315027
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
|
protected set<beandefinitionholder> doscan(string... basepackages) { assert .notempty(basepackages, "at least one base package must be specified" ); set<beandefinitionholder> beandefinitions = new linkedhashset(); string[] var3 = basepackages; int var4 = basepackages.length; for ( int var5 = 0 ; var5 < var4; ++var5) { string basepackage = var3[var5]; set<beandefinition> candidates = this .findcandidatecomponents(basepackage); iterator var8 = candidates.iterator(); while (var8.hasnext()) { beandefinition candidate = (beandefinition)var8.next(); scopemetadata scopemetadata = this .scopemetadataresolver.resolvescopemetadata(candidate); candidate.setscope(scopemetadata.getscopename()); string beanname = this .beannamegenerator.generatebeanname(candidate, this .registry); if (candidate instanceof abstractbeandefinition) { this .postprocessbeandefinition((abstractbeandefinition)candidate, beanname); } if (candidate instanceof annotatedbeandefinition) { annotationconfigutils.processcommondefinitionannotations((annotatedbeandefinition)candidate); } if ( this .checkcandidate(beanname, candidate)) { beandefinitionholder definitionholder = new beandefinitionholder(candidate, beanname); definitionholder = annotationconfigutils.applyscopedproxymode(scopemetadata, definitionholder, this .registry); beandefinitions.add(definitionholder); this .registerbeandefinition(definitionholder, this .registry); } } } return beandefinitions; } |
关于mybatis中的executor与一级缓存的关系:
这里引出一个经典问题,关于mybatis一级缓存问题,这个缓存存储在session中,当sql关闭的时候就会自动销毁,涉及到mybatis中的session生命周期问题
为什么mybatis与spring结合后一级缓存会失效?以为sqlsession是由sqlsessionfactorybean生成,二这个sqlsessionfactorybean是由spring管理,也就是此时的session是由spring进行管理的并不是mybatis管理,所以此时session缓存会失效。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public interface executor { resulthandler no_result_handler = null ; int update(mappedstatement var1, object var2) throws sqlexception; <e> list<e> query(mappedstatement var1, object var2, rowbounds var3, resulthandler var4, cachekey var5, boundsql var6) throws sqlexception; <e> list<e> query(mappedstatement var1, object var2, rowbounds var3, resulthandler var4) throws sqlexception; <e> cursor<e> querycursor(mappedstatement var1, object var2, rowbounds var3) throws sqlexception; list<batchresult> flushstatements() throws sqlexception; void commit( boolean var1) throws sqlexception; void rollback( boolean var1) throws sqlexception; cachekey createcachekey(mappedstatement var1, object var2, rowbounds var3, boundsql var4); boolean iscached(mappedstatement var1, cachekey var2); void clearlocalcache(); void deferload(mappedstatement var1, metaobject var2, string var3, cachekey var4, class <?> var5); transaction gettransaction(); void close( boolean var1); boolean isclosed(); void setexecutorwrapper(executor var1); } |
mybatis-spring依赖中mapperscannerregistrar的作用:registerbeandefinitions方法中的doscan()此方法是将mapper扫描到初始化中。原理就是获取到项目路径找到对应的classes路径,获取了com.dao包名,将获取的路径+包名转成文件夹,循环获取文件夹下面的文件(userdao.class),然后使用将userdao拿到。此时包名+类名都拿到后使用class.forname(包名+类名)反射出对象,进行bean注册
总结运行原理:
初始化信息(数据库连接信息,扫描mapper包中的class用于创建bean对象,spring中的类applicationfactory用于创建变对象、mapper中的xml的id与sql放到mapperstatement对象中)
其中对于扫描mapper包中的class路径+参数basepackages转成文件夹,然后循环找到所有的类名,使用.......(请看mapperscannerregistrar引出的doscan方法)
执行过程:根据sqlsessionfactorybean创建出sqlsession,调用selectlist方法,之后根据参数(namespacename+id)作为key找到mapperstatement对象中存储的value获取到sql语句,再有executor(mybatis默认使用 cacheexecutor)执行sql语句查询出结果集
原文链接:https://www.cnblogs.com/gnwzj/p/10680940.html