好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring整合SpringMVC + Mybatis基础框架的配置文件详解

前言

新建一个普通的Maven项目

基本目录结构

?

1

2

3

4

5

6

7

├── src     #

│ ├── main  #

│ │ └── java    # java代码目录

│ │ └── resources # 配置文件目录, 存放下面Spring配置文件

│ ├── test      # 单元测试目录

├── web     # web目录

│ └── WEB-INF   # web.xml 配置文件目录

1. Mybatis层编写

1、在 resources 目录下新建数据库配置文件 database.properties

?

1

2

3

4

5

jdbc.driver=com.mysql.jdbc.Driver

# 如果是使用 MySQL8. 0 + 那么还需要增加一个时区的配置; serverTimezone=Asia/Shanghai

jdbc.url=jdbc:mysql: //localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8

jdbc.username=root

jdbc.password= 123456

2、在 resources 目录下创建Mybatis配置文件 mybatis-config.xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<?xml version= "1.0" encoding= "UTF-8" ?>

<!DOCTYPE configuration

  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

  "http://mybatis.org/dtd/mybatis-3-config.dtd" >

<configuration>

  <!--配置数据源, 交给Spring-->

 

  <!--配置log-->

  <settings>

  <!--STDOUT_LOGGING: 标准的日志工厂实现-->

  <setting name= "logImpl" value= "STDOUT_LOGGING" />

  </settings>

 

  <!--配置别名-->

  <typeAliases>

  < package name= "com.pro.pojo" />

  </typeAliases>

 

  <!--绑定Mapper-->

  <mappers>

  <mapper class = "com.pro.dao.BooksMapper" />

  </mappers>

 

</configuration>

2. Spring层编写

1. Spring整合Mybatis

配置Spring整合MyBatis,这里数据源使用c3p0连接池;

编写Spring整合Mybatis的相关的配置文件;在 resources 目录下创建 spring-dao.xml

注意:这里要引入上面Mybatis层的两个配置文件,配置文件的名称不要写错

?

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

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

<?xml version= "1.0" encoding= "UTF-8" ?>

<beans xmlns= "http://HdhCmsTestspringframework.org/schema/beans"

  xmlns:xsi= "http://HdhCmsTestw3.org/2001/XMLSchema-instance"

  xmlns:context= "http://HdhCmsTestspringframework.org/schema/context"

  xsi:schemaLocation="http: //HdhCmsTestspringframework.org/schema/beans

  http: //HdhCmsTestspringframework.org/schema/beans/spring-beans.xsd

  http: //HdhCmsTestspringframework.org/schema/context

  http: //HdhCmsTestspringframework.org/schema/context/spring-context.xsd">

 

  <!-- 1 . 关联数据库配置文件-->

  <context:property-placeholder location= "classpath:database.properties" />

 

  <!-- 2 . 连接池

  dbcp: 半自动化操作, 不能自动连接

  c3p0: 自动化操作 (自动加载配置文件并设置到对象中)

  druid, hikari

  -->

  <bean id= "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource" >

  <property name= "driverClass" value= "${jdbc.driver}" />

  <property name= "jdbcUrl" value= "${jdbc.url}" />

  <property name= "user" value= "${jdbc.username}" />

  <property name= "password" value= "${jdbc.password}" />

 

  <!--c3p0连接池的私有属性, 最大最小连接池大小-->

  <property name= "maxPoolSize" value= "30" />

  <property name= "minPoolSize" value= "10" />

  <!--关闭连接后不自动commit-->

  <property name= "autoCommitOnClose" value= "false" />

  <!--连接超时-->

  <property name= "checkoutTimeout" value= "10000" />

  <!--获取连接失败重试次数-->

  <property name= "acquireRetryAttempts" value= "2" />

  </bean>

 

  <!-- 3 . sqlSessionFactory-->

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

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

  <!--绑定Mybatis配置文件-->

  <property name= "configLocation" value= "classpath:mybatis-config.xml" />

  </bean>

 

  <!-- 4 . 配置Dao扫描包, 动态实现Dao接口注入到Spring容器中-->

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

  <!--注入 sqlSessionFactory-->

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

  <!--配置要扫描的dao包-->

  <property name= "basePackage" value= "com.pro.dao" />

  </bean>

</beans>

2. Spring整合service

将业务层的类注入到Spring中,在 resources 目录下创建 spring-service.xml

?

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

33

34

35

36

37

38

39

40

41

42

43

44

45

46

<?xml version= "1.0" encoding= "UTF-8" ?>

<beans xmlns= "http://HdhCmsTestspringframework.org/schema/beans"

  xmlns:xsi= "http://HdhCmsTestw3.org/2001/XMLSchema-instance"

  xmlns:context= "http://HdhCmsTestspringframework.org/schema/context"

  xmlns:aop= "http://HdhCmsTestspringframework.org/schema/aop"

  xmlns:tx= "http://HdhCmsTestspringframework.org/schema/tx"

  xsi:schemaLocation="http: //HdhCmsTestspringframework.org/schema/beans

  http: //HdhCmsTestspringframework.org/schema/beans/spring-beans.xsd

  http: //HdhCmsTestspringframework.org/schema/context

  http: //HdhCmsTestspringframework.org/schema/context/spring-context.xsd

  http: //HdhCmsTestspringframework.org/schema/aop

  http: //HdhCmsTestspringframework.org/schema/aop/spring-aop.xsd

  http: //HdhCmsTestspringframework.org/schema/tx

  http: //HdhCmsTestspringframework.org/schema/tx/spring-tx.xsd">

 

  <!-- 1 . 扫描service下的包-->

  <context:component-scan base- package = "com.pro.service" />

 

  <!-- 2 . 将业务层的类注入到Spring中-->

  <bean id= "BooksServiceImpl" class = "com.pro.service.BooksServiceImpl" >

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

  </bean>

 

  <!-- 3 . 配置声明式事务-->

  <bean id= "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" >

  <!--注入数据源-->

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

  </bean>

 

  <!-- 4 . 配置aop实现事务织入-->

  <!--配置事务通知-->

  <tx:advice id= "txAdvice" transaction-manager= "transactionManager" >

  <!-- 1 . 给那些方法配置事务-->

  <!-- 2 . 配置事务的传播特性: propagation-->

  <tx:attributes>

  <tx:method name= "*" propagation= "REQUIRED" />

  </tx:attributes>

  </tx:advice>

 

  <!--配置事务切入-->

  <aop:config>

  <!--mapper包下的所有类的所有方法-->

  <aop:pointcut id= "txPointCut" expression= "execution(* com.pro.dao.*.*(..))" />

  <aop:advisor advice-ref= "txAdvice" pointcut-ref= "txPointCut" />

  </aop:config>

</beans>

3. SpringMVC层编写

1. 编写web.xml

修改 WEB-INF 下的 web.xml 文件

这里引入Spring整合的配置文件 applicationContext.xml

?

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

33

34

35

36

37

38

39

40

41

42

<?xml version= "1.0" encoding= "UTF-8" ?>

<web-app xmlns= "https://jakarta.ee/xml/ns/jakartaee"

  xmlns:xsi= "http://HdhCmsTestw3.org/2001/XMLSchema-instance"

  xsi:schemaLocation= "https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"

  version= "5.0" >

 

  <!--DispatchServlet-->

  <servlet>

  <servlet-name>springmvc</servlet-name>

  <servlet- class >org.springframework.web.servlet.DispatcherServlet</servlet- class >

  <!--加载Spring配置文件-->

  <init-param>

  <param-name>contextConfigLocation</param-name>

  <param-value>classpath:applicationContext.xml</param-value>

  </init-param>

  <!--启动级别-->

  <load-on-startup> 1 </load-on-startup>

  </servlet>

  <servlet-mapping>

  <servlet-name>springmvc</servlet-name>

  <url-pattern>/</url-pattern>

  </servlet-mapping>

 

  <!--乱码过滤-->

  <filter>

  <filter-name>encodingFilter</filter-name>

  <filter- class >org.springframework.web.filter.CharacterEncodingFilter</filter- class >

  <init-param>

  <param-name>encoding</param-name>

  <param-value>utf- 8 </param-value>

  </init-param>

  </filter>

  <filter-mapping>

  <filter-name>encodingFilter</filter-name>

  <url-pattern>/*</url-pattern>

  </filter-mapping>

 

  <!--Session过期时间-->

  <session-config>

  <session-timeout> 15 </session-timeout>

  </session-config>

</web-app>

2. 编写spring-mvc.xml

在 resources 目录下创建 spring-mvc.xml

?

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

<?xml version= "1.0" encoding= "UTF-8" ?>

<beans xmlns= "http://HdhCmsTestspringframework.org/schema/beans"

  xmlns:xsi= "http://HdhCmsTestw3.org/2001/XMLSchema-instance"

  xmlns:mvc= "http://HdhCmsTestspringframework.org/schema/mvc"

  xmlns:context= "http://HdhCmsTestspringframework.org/schema/context"

  xsi:schemaLocation="http: //HdhCmsTestspringframework.org/schema/beans

  http: //HdhCmsTestspringframework.org/schema/beans/spring-beans.xsd

  http: //HdhCmsTestspringframework.org/schema/mvc

  http: //HdhCmsTestspringframework.org/schema/mvc/spring-mvc.xsd

  http: //HdhCmsTestspringframework.org/schema/context

  https: //HdhCmsTestspringframework.org/schema/context/spring-context.xsd">

 

 

  <!-- 1 . 注解驱动-->

  <mvc:annotation-driven/>

  <!-- 2 . 静态资源过滤-->

  <mvc: default -servlet-handler/>

  <!-- 3 . 扫描包: controller-->

  <context:component-scan base- package = "com.pro.controller" />

  <!-- 4 . 视图解析器-->

  <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" >

  <property name= "prefix" value= "/WEB-INF/jsp/" />

  <property name= "suffix" value= ".jsp" />

  </bean>

 

</beans>

4. Spring配置整合文件,applicationContext.xml

在 resources 目录下创建 applicationContext.xml

这里引入上面三个配置文件 spring-dao.xml 、 spring-service.xml 、 spring-mvc.xml 整合成一个总的配置文件

?

1

2

3

4

5

6

7

8

9

10

11

<?xml version= "1.0" encoding= "UTF-8" ?>

<beans xmlns= "http://HdhCmsTestspringframework.org/schema/beans"

  xmlns:xsi= "http://HdhCmsTestw3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http: //HdhCmsTestspringframework.org/schema/beans

  http: //HdhCmsTestspringframework.org/schema/beans/spring-beans.xsd">

 

  < import resource= "classpath:spring-dao.xml" />

  < import resource= "classpath:spring-service.xml" />

  < import resource= "classpath:spring-mvc.xml" />

 

</beans>

依赖

?

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

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

<!--依赖-->

<dependencies>

  <dependency>

  <groupId>org.projectlombok</groupId>

  <artifactId>lombok</artifactId>

  <version> 1.18 . 10 </version>

  </dependency>

  <!--Junit-->

  <dependency>

  <groupId>junit</groupId>

  <artifactId>junit</artifactId>

  <version> 4.13 </version>

  </dependency>

  <!--数据库驱动-->

  <dependency>

  <groupId>mysql</groupId>

  <artifactId>mysql-connector-java</artifactId>

  <version> 5.1 . 47 </version>

  </dependency>

  <!--数据库连接池-->

  <dependency>

  <groupId>com.mchange</groupId>

  <artifactId>c3p0</artifactId>

  <version> 0.9 . 5.2 </version>

  </dependency>

 

  <!--Servlet - JSP -->

  <dependency>

  <groupId>javax.servlet</groupId>

  <artifactId>servlet-api</artifactId>

  <version> 2.5 </version>

  </dependency>

  <dependency>

  <groupId>javax.servlet.jsp</groupId>

  <artifactId>jsp-api</artifactId>

  <version> 2.2 </version>

  </dependency>

  <dependency>

  <groupId>javax.servlet</groupId>

  <artifactId>jstl</artifactId>

  <version> 1.2 </version>

  </dependency>

 

  <!--Mybatis-->

  <dependency>

  <groupId>org.mybatis</groupId>

  <artifactId>mybatis</artifactId>

  <version> 3.5 . 2 </version>

  </dependency>

  <dependency>

  <groupId>org.mybatis</groupId>

  <artifactId>mybatis-spring</artifactId>

  <version> 2.0 . 2 </version>

  </dependency>

 

  <!--Spring-->

  <dependency>

  <groupId>org.springframework</groupId>

  <artifactId>spring-webmvc</artifactId>

  <version> 5.1 . 9 .RELEASE</version>

  </dependency>

  <dependency>

  <groupId>org.springframework</groupId>

  <artifactId>spring-jdbc</artifactId>

  <version> 5.1 . 9 .RELEASE</version>

  </dependency>

  <dependency>

  <groupId>org.aspectj</groupId>

  <artifactId>aspectjweaver</artifactId>

  <version> 1.9 . 4 </version>

  </dependency>

</dependencies>

 

<!--静态资源导出问题-->

<build>

  <resources>

  <resource>

  <directory>src/main/java</directory>

  <includes>

  <include>** /*.properties</include>

  <include>**/*.xml</include>

  </includes>

  <filtering>false</filtering>

  </resource>

  <resource>

  <directory>src/main/resources</directory>

  <includes>

  <include>**/*.properties</include>

  <include>**/ *.xml</include>

  </includes>

  <filtering> false </filtering>

  </resource>

  </resources>

</build>

到此这篇关于Spring整合SpringMVC + Mybatis基础框架的配置文件的文章就介绍到这了,更多相关Spring整合SpringMVC Mybatis内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://HdhCmsTestcnblogs测试数据/pojo/archive/2021/01/31/14353557.html

查看更多关于Spring整合SpringMVC + Mybatis基础框架的配置文件详解的详细内容...

  阅读:20次