Spring3+mybatis+mysql整合详解.docx
《Spring3+mybatis+mysql整合详解.docx》由会员分享,可在线阅读,更多相关《Spring3+mybatis+mysql整合详解.docx(32页珍藏版)》请在冰豆网上搜索。
![Spring3+mybatis+mysql整合详解.docx](https://file1.bdocx.com/fileroot1/2022-12/30/f40c7dae-82c8-4ca4-b54b-cf4b79ff8be1/f40c7dae-82c8-4ca4-b54b-cf4b79ff8be11.gif)
Spring3+mybatis+mysql整合详解
Spring3+mybatis+mysql整合详解
(一)
博客分类:
∙Spring
Spring3AOPMybatis
废话不多说,我们先来看项目所需要的jar包:
Spring3:
org.springframework.aop-3.1.0.RELEASE.jar
org.springframework.asm-3.1.0.RELEASE.jar
org.springframework.aspects-3.1.0.RELEASE.jar
org.springframework.beans-3.1.0.RELEASE.jar
org.springframework.context-3.1.0.RELEASE.jar
org.springframework.context.support-3.1.0.RELEASE.jar
org.springframework.core-3.1.0.RELEASE.jar
org.springframework.expression-3.1.0.RELEASE.jar
org.springframework.instrument-3.1.0.RELEASE.jar
org.springframework.instrument.tomcat-3.1.0.RELEASE.jar
org.springframework.jdbc-3.1.0.RELEASE.jar
org.springframework.jms-3.1.0.RELEASE.jar
org.springframework.orm-3.1.0.RELEASE.jar
org.springframework.oxm-3.1.0.RELEASE.jar
org.springframework.spring-library-3.1.0.RELEASE.libd
org.springframework.test-3.1.0.RELEASE.jar
org.springframework.transaction-3.1.0.RELEASE.jar
org.springframework.web-3.1.0.RELEASE.jar
org.springframework.web.portlet-3.1.0.RELEASE.jar
org.springframework.web.servlet-3.1.0.RELEASE.jar
org.springframework.web.struts-3.1.0.RELEASE.jar
由于我也不搞不清楚,Spring这么多jar包每个的作用,有些有相互依赖,没办法,只能全部一起导进来。
如果有高人指点一下他们的作用,不胜感激!
Spring3.0.1jar下载地址:
http:
//www.springsource.org/download
Mybatis:
mybatis-3.0.6.jar--mybatis核心包(
mybatis-spring-1.0.2.jar--同Spring整合所需jar(
SpringAOP依赖jar:
aopalliance-1.0.jar
asm-3.3.1.jar
aspectj-1.6.10.jar
aspectjweaver-1.6.9.jar
cglib-2.2.2.jar
Jstl标签依赖jar:
jstl-1.2.jar
MySql数据库驱动jar:
mysql-connector-java-5.0.4-bin.jar
其他jar:
commons-logging-1.0.4.jar
log4j-1.2.9.jar
portlet-api-2.0.jar
Spring3+mybatis+mysql整合详解
(二)
博客分类:
∙Spring
Spring配置
下面来看web.xml的配置:
Xml代码
1.
xml version="1.0" encoding="UTF-8"?
>
2.3. xmlns="
4. xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
5. xsi:
schemaLocation="
6.
7.
8. index.jsp
9.
10.
11.
--配置Sring MVC的核心控制器DispatcherServlet -->
12.
13. dispatcherServlet
14. org.springframework.web.servlet.DispatcherServlet
15.
--
16. DispatcherServlet 默认加载的bean文件是/WEB-INF/(servlet-name)-servlet.xml
17. 可以通过配置contextConfigLocation来改变加载的文件
18. -->
19.
20. contextConfigLocation
21. /WEB-INF/classes/applicationContext.xml
22.
23. 1
24.
25.
--为DispatcherServlet建立映射 -->
26.
27. dispatcherServlet
28. *.do
29.
30.
xmlversion="1.0"encoding="UTF-8"?
>
xmlns="
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="
index.jsp
--配置SringMVC的核心控制器DispatcherServlet-->
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
--
DispatcherServlet默认加载的bean文件是/WEB-INF/(servlet-name)-servlet.xml
可以通过配置contextConfigLocation来改变加载的文件
-->
contextConfigLocation
/WEB-INF/classes/applicationContext.xml
1
--为DispatcherServlet建立映射-->
dispatcherServlet
*.do
传统Spring配置中,需要在web.xml中配置spring的监听器。
这里我们使用了springmvc框架后,需要配置一个dispatcherServlet,它会加载/WEB-INF/classes/applicationContext.xml这个spring的配置文件。
后面你会看到,我们将spring的不同功能配置分别写入不同的spring配置文件中,然后在/WEB-INF/classes/applicationContext.xml中将这些配置文件导入即可。
当然,你也可以使用分号隔开的方式,引入多个配置文件,但我更喜欢导入的方式。
关于servlet-mapping中的*.do配置,我想说明的是,如果配置成“/”将拦截所有请求,包括静态资源。
Spring3+mybatis+mysql整合详解(三)
博客分类:
∙Spring
Spring配置mybatis配置springmvc配置springaop配置
下面来看Spring的配置:
1、applicationContext.xml
Xml代码
1.
xml version="1.0" encoding="UTF-8"?
>
2.//www.springframework.org/schema/beans"
3. xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
4. xmlns:
aop="http:
//www.springframework.org/schema/aop"
5. xmlns:
tx="http:
//www.springframework.org/schema/tx"
6. xsi:
schemaLocation="
7. http:
//www.springframework.org/schema/beans http:
//www.springframework.org/schema/beans/spring-beans-2.5.xsd
8. http:
//www.springframework.org/schema/tx http:
//www.springframework.org/schema/tx/spring-tx-2.5.xsd
9. http:
//www.springframework.org/schema/aop http:
//www.springframework.org/schema/aop/spring-aop-2.5.xsd">
10.
-- DAO配置 -->
11.
12.
13.
-- IoC配置 -->
14.
15.
16.
-- MVC配置 -->
17.
18.
19.
-- AOP配置 -->
20.
21.
xmlversion="1.0"encoding="UTF-8"?
>
//www.springframework.org/schema/beans"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xmlns:
aop="http:
//www.springframework.org/schema/aop"
xmlns:
tx="http:
//www.springframework.org/schema/tx"
xsi:
schemaLocation="
http:
//www.springframework.org/schema/beanshttp:
//www.springframework.org/schema/beans/spring-beans-2.5.xsd
http:
//www.springframework.org/schema/txhttp:
//www.springframework.org/schema/tx/spring-tx-2.5.xsd
http:
//www.springframework.org/schema/aophttp:
//www.springframework.org/schema/aop/spring-aop-2.5.xsd">
--DAO配置-->
--IoC配置-->
--MVC配置-->
--AOP配置-->
2、daoContext.xml
Xml代码
1.
xml version="1.0" encoding="UTF-8"?
>
2.//www.springframework.org/schema/beans"
3. xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance" xmlns:
aop="http:
//www.springframework.org/schema/aop"
4. xmlns:
tx="http:
//www.springframework.org/schema/tx" xmlns:
p="http:
//www.springframework.org/schema/p"
5. xmlns:
context="http:
//www.springframework.org/schema/context"
6. xmlns:
mvc="http:
//www.springframework.org/schema/mvc"
7. xsi:
schemaLocation="
8. http:
//www.springframework.org/schema/beans http:
//www.springframework.org/schema/beans/spring-beans-2.5.xsd
9. http:
//www.springframework.org/schema/tx http:
//www.springframework.org/schema/tx/spring-tx-2.5.xsd
10. http:
//www.springframework.org/schema/aop http:
//www.springframework.org/schema/aop/spring-aop-2.5.xsd
11. http:
//www.springframework.org/schema/context http:
//www.springframework.org/schema/context/spring-context-2.5.xsd
12. http:
//www.springframework.org/schema/mvc http:
//www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
13.
14.
-- 配置数据源 -->
15. 16. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
17.
18. mysql:
//localhost:
3306/myfristdb" />
19.
20.
21.
22.
23.
-- Spring、MyBatis的整合,需要在 Spring 应用上下文中定义至少两样东西:
一个SqlSessionFactory和至少一个数据映射器类(UserMapper->iocContext.xml)。
-->
24.
25. SqlMapConfig.xml" />
26.
27.
28.
29.
-- 配置事务管理器 -->
30. 31. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
32.
33.
34.
xmlversion="1.0"encoding="UTF-8"?
>
//www.springframework.org/schema/beans"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"xmlns:
aop="http:
//www.springframework.org/schema/aop"
xmlns:
tx="http:
//www.springframework.org/schema/tx"xmlns:
p="http:
//www.springframework.org/schema/p"
xmlns:
context="http:
//www.springframework.org/schema/context"
xmlns:
mvc="http:
//www.springframework.org/schema/mvc"
xsi:
schemaLocation="
http:
//www.springframework.org/schema/beanshttp:
//www.springframework.org/schema/beans/spring-beans-2.5.xsd
http:
//www.springframework.org/schema/txhttp:
//www.springframework.org/schema/tx/spring-tx-2.5.xsd
http:
//www.springframework.org/schema/aophttp:
//www.springframework.org/schema/aop/spring-aop-2.5.xsd
http:
//www.springframework.org/schema/contexthttp:
//www.springframework.org/schema/context/spring-context-2.5.xsd
http:
//www.springframework.org/schema/mvchttp:
//www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
--配置数据源-->
class="org.springframework.jdbc.datasource.DriverManagerDataSource">