Mybatis(四)--Spring集成Mybatis

时间:2024-10-30 09:21:33

强烈推荐一个大神的人工智能的教程:/zhanghan

一、创建zh-root项目(用来管理依赖包的版本)

       1.使用idea开发工具新建maven项目,坐标如下:

  1. <groupId></groupId>
  2. <artifactId>zh-root</artifactId>
  3. <version>1.0-SNAPSHOT</version>

       2.添加对应依赖及版本

  1. <project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance"
  2. xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId></groupId>
  5. <artifactId>zh-root</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <packaging>pom</packaging>
  8. <properties>
  9. <!--构建项目时使用,用于maven中的文件拷贝-->
  10. <>UTF-8</>
  11. <>UTF-8</>
  12. <!-- 编译时的编码 -->
  13. <!--否则在dos执行mvn compile可能由于编码问题出现莫名其妙的问题,因为dos默认的编码,格式是系统的GBK-->
  14. <>UTF-8</>
  15. <!-- 编译源代码 jdk版本编译 -->
  16. <>1.8</>
  17. <!-- 编译源代码 版本 -->
  18. <>1.8</>
  19. <!-- 编译插件版本号控制 -->
  20. <>3.7.0</>
  21. <!--junit单元测试 -->
  22. <>4.12</>
  23. <!--日志-->
  24. <>1.2.17</>
  25. <>5.0.</>
  26. <>1.8.13</>
  27. <>2.9.2</>
  28. <>3.4.5</>
  29. <>1.3.1</>
  30. <>5.1.38</>
  31. <>1.1.6</>
  32. </properties>
  33. <dependencyManagement>
  34. <dependencies>
  35. <!--测试-->
  36. <dependency>
  37. <groupId>junit</groupId>
  38. <artifactId>junit</artifactId>
  39. <version>${}</version>
  40. </dependency>
  41. <!--日志-->
  42. <dependency>
  43. <groupId>log4j</groupId>
  44. <artifactId>log4j</artifactId>
  45. <version>${}</version>
  46. </dependency>
  47. <!--spring-->
  48. <dependency>
  49. <groupId></groupId>
  50. <artifactId>spring-core</artifactId>
  51. <version>${}</version>
  52. </dependency>
  53. <dependency>
  54. <groupId></groupId>
  55. <artifactId>spring-context</artifactId>
  56. <version>${}</version>
  57. </dependency>
  58. <!--JDBC-->
  59. <dependency>
  60. <groupId></groupId>
  61. <artifactId>spring-jdbc</artifactId>
  62. <version>${}</version>
  63. </dependency>
  64. <!--spring事务-->
  65. <dependency>
  66. <groupId></groupId>
  67. <artifactId>spring-tx</artifactId>
  68. <version>${}</version>
  69. </dependency>
  70. <!--Spring面向切面编程-->
  71. <dependency>
  72. <groupId></groupId>
  73. <artifactId>spring-aop</artifactId>
  74. <version>${}</version>
  75. </dependency>
  76. <!--spring-aop的依赖-->
  77. <dependency>
  78. <groupId></groupId>
  79. <artifactId>aspectjweaver</artifactId>
  80. <version>${}</version>
  81. </dependency>
  82. <!--spring-web的依赖-->
  83. <dependency>
  84. <groupId></groupId>
  85. <artifactId>spring-web</artifactId>
  86. <version>${}</version>
  87. </dependency>
  88. <!--spring-mvc-->
  89. <dependency>
  90. <groupId></groupId>
  91. <artifactId>spring-webmvc</artifactId>
  92. <version>${}</version>
  93. </dependency>
  94. <!--spring-mvc-json-->
  95. <dependency>
  96. <groupId></groupId>
  97. <artifactId>jackson-databind</artifactId>
  98. <version>${}</version>
  99. </dependency>
  100. <!--mybatis-->
  101. <dependency>
  102. <groupId></groupId>
  103. <artifactId>mybatis</artifactId>
  104. <version>${}</version>
  105. </dependency>
  106. <dependency>
  107. <groupId></groupId>
  108. <artifactId>mybatis-spring</artifactId>
  109. <version>${}</version>
  110. </dependency>
  111. <!--mysql-->
  112. <dependency>
  113. <groupId>mysql</groupId>
  114. <artifactId>mysql-connector-java</artifactId>
  115. <version>${}</version>
  116. </dependency>
  117. <!-- 连接池 -->
  118. <dependency>
  119. <groupId></groupId>
  120. <artifactId>druid</artifactId>
  121. <version>${}</version>
  122. </dependency>
  123. </dependencies>
  124. </dependencyManagement>
  125. <build>
  126. <plugins>
  127. <plugin>
  128. <!-- 用来编译位于src/main/java/目录下的主源码 -->
  129. <groupId></groupId> <!-- 项目或者组织的唯一标志 -->
  130. <artifactId>maven-compiler-plugin</artifactId> <!-- 项目的通用名称 -->
  131. <version>${}</version>
  132. <!-- 使用maven-compiler-plugin 指定JDK版本和编码 -->
  133. <configuration> <!-- 通常用于私有不开源的plugin -->
  134. <source>${}</source> <!-- 编译源代码 jdk版本编译 -->
  135. <target>${}</target>
  136. <encoding>${}</encoding> <!-- 用于maven中的文件拷贝 的 编码格式 -->
  137. </configuration>
  138. </plugin>
  139. </plugins>
  140. </build>
  141. </project>

二、创建mybatis-spring-zh(我使用的是quickstart)

      1.使用idea开发工具新建maven项目.坐标如下:

  1. <groupId>-spring</groupId>
  2. <artifactId>mybatis-spring</artifactId>
  3. <version>1.0-SNAPSHOT</version>

     2.修改中的打包方式为war

<packaging>war</packaging>

     3.新建文件(目录:src/main/webapp/WEB-INF/),添加约束如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http:///2001/XMLSchema-instance"
  3. xmlns="/xml/ns/javaee"
  4. xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_0.xsd"
  5. version="3.0">
  6. </web-app>

     4.在中添加相关依赖

  1. <project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance"
  2. xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.">
  3. <parent>
  4. <groupId></groupId>
  5. <artifactId>zh-root</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. </parent>
  8. <modelVersion>4.0.0</modelVersion>
  9. <groupId>-spring</groupId>
  10. <artifactId>mybatis-spring</artifactId>
  11. <version>1.0-SNAPSHOT</version>
  12. <packaging>war</packaging>
  13. <dependencies>
  14. <!--测试-->
  15. <dependency>
  16. <groupId>junit</groupId>
  17. <artifactId>junit</artifactId>
  18. <scope>test</scope>
  19. </dependency>
  20. <!--日志-->
  21. <dependency>
  22. <groupId>log4j</groupId>
  23. <artifactId>log4j</artifactId>
  24. </dependency>
  25. <!--Spring核心-->
  26. <dependency>
  27. <groupId></groupId>
  28. <artifactId>spring-core</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId></groupId>
  32. <artifactId>spring-context</artifactId>
  33. </dependency>
  34. <!--Spring JDBC-->
  35. <dependency>
  36. <groupId></groupId>
  37. <artifactId>spring-jdbc</artifactId>
  38. <version>5.0.</version>
  39. </dependency>
  40. <!--Spring事务-->
  41. <dependency>
  42. <groupId></groupId>
  43. <artifactId>spring-tx</artifactId>
  44. </dependency>
  45. <!--Spring面向切面编程-->
  46. <dependency>
  47. <groupId></groupId>
  48. <artifactId>spring-aop</artifactId>
  49. </dependency>
  50. <!--spring-aop依赖-->
  51. <dependency>
  52. <groupId></groupId>
  53. <artifactId>aspectjweaver</artifactId>
  54. </dependency>
  55. <!--Spring-MVC依赖-->
  56. <!--Spring web依赖-->
  57. <dependency>
  58. <groupId></groupId>
  59. <artifactId>spring-web</artifactId>
  60. </dependency>
  61. <!--Spring-MVC-->
  62. <dependency>
  63. <groupId></groupId>
  64. <artifactId>spring-webmvc</artifactId>
  65. </dependency>
  66. <!--spring-mvc-json-->
  67. <dependency>
  68. <groupId></groupId>
  69. <artifactId>jackson-databind</artifactId>
  70. </dependency>
  71. <!--spring-mybatis-->
  72. <dependency>
  73. <groupId></groupId>
  74. <artifactId>mybatis</artifactId>
  75. </dependency>
  76. <dependency>
  77. <groupId></groupId>
  78. <artifactId>mybatis-spring</artifactId>
  79. </dependency>
  80. <!--mysql-->
  81. <dependency>
  82. <groupId>mysql</groupId>
  83. <artifactId>mysql-connector-java</artifactId>
  84. </dependency>
  85. <!--数据库连接池-->
  86. <dependency>
  87. <groupId></groupId>
  88. <artifactId>druid</artifactId>
  89. </dependency>
  90. </dependencies>
  91. </project>

     5.项目的完整路径如下,第一步先创建项目的基本结构,controller,mapper,model,service

                                              
 

      6.创建springmvc的配置文件,配置自动扫描

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="/schema/beans"
  3. xmlns:xsi="http:///2001/XMLSchema-instance"
  4. xmlns:context="/schema/context"
  5. xmlns:mvc="/schema/mvc"
  6. xsi:schemaLocation="/schema/beans
  7. /schema/beans/
  8. /schema/mvc
  9. /schema/mvc/spring-mvc-4.
  10. /schema/context
  11. /schema/context/">
  12. <mvc:annotation-driven/>
  13. <context:component-scan base-package=""/>
  14. <context:component-scan base-package=""/>
  15. <bean class="">
  16. <!--<property name="viewClass" value=""/>-->
  17. <property name="prefix" value="/WEB-INF/jsp/"/>
  18. <property name="suffix" value=".jsp"/>
  19. </bean>
  20. <import resource="classpath:spring/"/>
  21. <import resource="classpath:spring/"/>
  22. </beans>

       7.创建文件,配置数据库连接的相关参数

  1. jdbc_driverClassName=
  2. jdbc_url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
  3. jdbc_username=root
  4. jdbc_password=

      8.创建,连接数据库配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="/schema/beans"
  3. xmlns:xsi="http:///2001/XMLSchema-instance"
  4. xsi:schemaLocation="/schema/beans
  5. /schema/beans/">
  6. <!--数据库连接设置-->
  7. <!--<bean class="com..P6DataSource">-->
  8. <!--<constructor-arg>-->
  9. <!--<ref bean="dataSourceDefault"/>-->
  10. <!--</constructor-arg>-->
  11. <!--</bean>-->
  12. <bean id="dataSource" class="" destroy-method="close">
  13. <property name="url" value="${jdbc_url}"/>
  14. <property name="username" value="${jdbc_username}"/>
  15. <property name="password" value="${jdbc_password}"/>
  16. </bean>
  17. </beans>

        9.创建mybatis配置文件,用于mybatis的常用配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration PUBLIC "-////DTD Config 3.0//EN" "/dtd/">
  3. <configuration>
  4. <settings>
  5. <setting name="logImpl" value="LOG4J"/>
  6. <setting name="cacheEnabled" value="true"/>
  7. <setting name="mapUnderscoreToCamelCase" value="true"/>
  8. <setting name="aggressiveLazyLoading" value="false"/>
  9. </settings>
  10. </configuration>

     10.创建mybatis和spring整合的配置文件,

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns:xsi="http:///2001/XMLSchema-instance"
  3. xmlns="/schema/beans"
  4. xmlns:context="/schema/context"
  5. xsi:schemaLocation="/schema/beans
  6. /schema/beans/
  7. /schema/context
  8. /schema/context/">
  9. <!--加载db配置文件-->
  10. <context:property-placeholder location="classpath:"/>
  11. <!--配置sqlSessionFactory-->
  12. <bean id="sqlSessionFactory" class="">
  13. <property name="configLocation" value="classpath:spring/"/>
  14. <property name="dataSource" ref="dataSource"/>
  15. <property name="mapperLocations">
  16. <array>
  17. <value>classpath:mapper/*.xml</value>
  18. </array>
  19. </property>
  20. <property name="typeAliasesPackage" value=""/>
  21. </bean>
  22. <!--扫描mapper-->
  23. <bean class="">
  24. <property name="basePackage" value=""/>
  25. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  26. </bean>
  27. </beans>

       11.完善

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http:///2001/XMLSchema-instance"
  3. xmlns="/xml/ns/javaee"
  4. xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_0.xsd"
  5. version="3.0">
  6. <context-param>
  7. <!--定义了要装入的spring配置文件-->
  8. <param-name>contextConfigLocation</param-name>
  9. <param-value>classpath:spring/</param-value>
  10. </context-param>
  11. <listener>
  12. <listener-class></listener-class>
  13. </listener>
  14. <!--前端控制器-->
  15. <servlet>
  16. <servlet-name>mybatis</servlet-name>
  17. <servlet-class>
  18. </servlet-class>
  19. <init-param>
  20. <param-name>contextConfigLocation</param-name>
  21. <param-value>classpath:spring/</param-value>
  22. </init-param>
  23. <load-on-startup>1</load-on-startup>
  24. </servlet>
  25. <servlet-mapping>
  26. <servlet-name>mybatis</servlet-name>
  27. <url-pattern>/</url-pattern>
  28. </servlet-mapping>
  29. </web-app>

      12.写一个小例子测试一下是否成功了,以查询用户为例,分别创建,UserService,UserController,SysUser,访问该测试方法,查询出如下结果。相关程序代码已上传到码云,可以参考,/ZhangHuan8/


     关于为什么要抽出一个root项目管理版本,可以参考以下博客:Maven实战(六)--- dependencies与dependencyManagement的区别__Emily的博客-****博客