网上的整合资料比较多了,小白在这里简单说一下自己碰到的坑和解决过程,其中很多东西并不懂,只是找到的东西照葫芦画瓢了一下,没有讲解.一把辛酸一把泪.
使用idea + Maven进行配置.先上完整项目结构(可以正常使用.),特别注意target文件夹,这是编译后可以正常运行的项目,如果出了错误,那么一般是target里面找一些原因.
- 创建Maven项目
- 创建Maven的时候, 选择本地的Maven,因为版本比较新,
- setting.xml文件自己可以定义,注意需要在setting.xml文件里面修改阿里的镜像,这样加载比较快 .add maven
- property的时候,添加archetypeCatalog为internal,也是为了加载快.原因
- pom.xml设置
坑1:未来在编译的时候target里面不会包含xml和properties文件,非常痛苦!在pom.xml文件里设置Maven,使其可以打包xml和properties文件,设置办法及讲解
坑2:坑1设置的时候,我个人理解是将classpath文件里面的配置文件编译到target的classpath里面去,坑1可以解决文件放在java目录下,与包文件同级的时候的问题,但是若想把配置文件放在resource里面去,那么还需要进一步的配置.解决方法为在输出里面设置resource打包输出的位置.步骤 - 在我的理解里,配置web.xm文件的时候,寻找配置文件的目录为:WEB-INF/classes,也就是说打包完成的时候
classpath
为这个文件夹,那么就需要把一系列xml文件,mapper文件/文件夹都打包到这里才对. - 坑4:包的引入,平常的包可以在上面的文档里看,还有一些包是特别注意,必须要加的,不过这里应该简单,因为我后边出了很多次
can't create bean name xxxx
的错误,结果发现是没有引入包.- spring-aop
- aspectjweaver
- mybatis,spring-jdbc,mybatis-spring,mysql-connector-java
- Spring的applicationContext.xml设置
- 将mapper文件放在resource里面的时候,需要在配置工厂的时候声明mapperlocations属性.datasource属性指向连接池,这里使用的是jdbc的连接池和mysql的数据库连接.网上教程比较多.
- web.xml的各项配置
,web.xml文件的配置.
1.SpringMVC
<!--配置SpringMVC的配置文件-->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
2.Spring,这里需要使用Spring的ContextLoaderListener对Spring的文件进行读取.
<!--配置Spring,使用ContextLoaderListener进行Spring的读取-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
- Spring的配置文件-整合Mybatis
: 1.头文件的设置.
这里是一个小坑,需要注意一下.参考资料里讲了一些文件格式的说明,我暂时看不太明白,但是命名空间需要注意一下,不然用不了的.
: 2.配置工厂
<!--配置工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
: 3. 事务管理器的配置没怎么看懂.
参考资料1
参考资料2
: 4.Mybatis的配置.mybatis.cfg.xml
参考资料
- 巨大坑!头文件的设置,idea的自动填充头文件并不准确,需要去网上或者去官网找,这个其实也比较简单,引用的位置都是一样的,顶多改下名字而已.