发表于: 2018-03-26 16:49:37

1 411


今天完成的事情: 

今天成功把spring 和 mybatis 整合了 就昨天的问题来看 

这是由于 配置文件读不到放在java的xml文件。然后昨天经师兄一提 

整合后 xml文件不需要mybatisconfig.xml 靠spring的配置文件就可以读到

这是工厂的bean 在mapperLocations就是 

所以把上面的<mappers>删了后就ok了。 


然后之前失败的原因 可能是配置文件先读取的mabatisconfig.xml 然后 mabatisconfig.xml不能读java下的xml文件 导致 无法找到mapper.xml



整个流程来说

spring的配置文件。applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<context:annotation-config />

<context:component-scan base-package="mybatis" />用来扫描包 

<!-- 导入数据库配置文件 -->
<!--<context:property-placeholder location="classpath:mybatis"/>-->

<!-- 配置数据库连接池 -->
   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 基本属性 url、user、password -->
   <property name="driverClassName" value="com.mysql.jdbc.Driver"/><!--druid可以不配置驱动-->
   <property name="url" value="jdbc:mysql://localhost:3306/task1?useUnicode=true&amp;useSSL=true&amp;characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="135246" />

<!--Mybatis的SessionFactory配置-->

<配置sqlsessionFactoryBean的。sqlsessionFactoryBean是用来生产sqlsessionFactory的 

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  

<property name="typeAliasesPackage" value="mybatis.jopo.User" />。  对以应实体类所在的包,需要包的全名

sqlsessionFactoryBean由spring加载初始化 构造之后给予属性值dataSource mapper

<property name="dataSource" ref="dataSource"/>

加载mapper.xml配置文件 

<property name="mapperLocations" value="classpath:mybatis/Mapper/UserMapper.xml"/>

</bean>


  •  mapperLocations:它表示我们的Mapper文件存放的位置,当我们的Mapper文件跟对应的Mapper接口处于同一位置的时候可以不用指定该属性的值。
  • configLocation:用于指定Mybatis的配置文件位置。如果指定了该属性,那么会以该配置文件的内容作为配置信息构建对应的SqlSessionFactoryBuilder,但是后续属性指定的内容会覆盖该配置文件里面指定的对应内容。

经我昨天的心路历程来看 configLocation默认即可。只要mapperLocations在  而且跟mapper放在同一位置 有mapperlocations的话 就不是很关键了 。

但是我看了源码后发现   configLocation读取配置 ,关联mybatis整个生命周期。

那么关于mapperlocations 还有没有必要呢 

https://www.cnblogs.com/1xin1yi/p/7373739.html。然后我看到了这篇文章

他的

<结论是:如果Mapper.xml与Mapper.class在同一个包下且同名,spring扫描Mapper.class的同时会自动扫描同名的Mapper.xml并装配到Mapper.class。

如果Mapper.xml与Mapper.class不在同一个包下或者不同名,就必须使用配置mapperLocations指定mapper.xml的位置。

此时spring是通过识别mapper.xml中的 <mapper namespace="mybatis.mapper.UserMapper"> namespace的值来确定对应的Mapper.class的。>


那么我是不是可以认为 如果Mapper.xml与Mapper.class在同一个包下且同名那么可以不需要mapperLocations 只需要用configLocatoion指定mybatis.xml就可以了 

但是昨天的经历来看 mybatis.xml 确实指定不了 放在scr/main/java/mybatis下的UserMapper.xml文件 

之后百度了 说是bug 告诉我需要把mapper.xml加载到paths下。这个等我之后在仔细研究吧 


然后还有个问题是 由sqlsessionFactoryBean协助创建sqlsessionFactory. 创建sqlsessionFactory配置configurition

如果Mapper.xml与Mapper.class在同一包下且同名,spring扫描Mapper.class的同时会自动扫描同名的Mapper.xml并装配到Mapper.class。 《----不需要指定mapper.xml

哪么configurition里也没有指定mapper.xml的话是否能运行呢 。。讲道理应该是不能的吧 如果不能的话 说明spring 扫描Mapper.class时装配Mapper.xml并不会在加载qlsessionFactoryBean时赋予Mapper.xml的地址。先后矛盾问题?

还是放着做个标记之后再考虑吧。


<!--Mybatis的Mapper接口识别-->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="basePackage" value="mybatis.Mapper"/>

包的路径

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>

这个属性一般都用不到,只有当你配置多数据源的时候,这是会有多个sqlSessionFactory,你就需要通过该属性来指定哪一个sqlSessionFactory(值为SqlSessionFactoryBean <bean>配置中的id属性)。

</bean>


大概是这么个配置。

做一些深度思考

10.CreateAt和UpdateAt的意义分别是创建时间和修改时间,这两个时间应该在什么情况下赋值?是否应该开放给外部调用的接口?

createat只在创建时赋值。updataat在修改对象时赋值

应该开放。也看需求吧

11.修真类型应该是直接存储Varchar,还是应该存储int?

因为修真类型总数固定 如果有数字代表的话就可以用int 不然用varchar 。

12.varchar类型的长度怎么确定?有什么样的原则,和Text和LongText的区别是什么?

varchar长度可变,在255个字符内。 
在字符超过255时,转变为text。 
text和longtext区别在于text最大2^16,longtext达到2^32个字符。


以后新接触一个插件 先看配置 看懂配置理解就容易多了 

明天计划的事情:(完善整合包 学习下语法) 
遇到的问题:(null) 
收获:(可还行)


返回列表 返回列表
评论

    分享到