标签:
看了一段时间Android,学了学C++,搭个SSM的框架复习复习老本行。 原来的SSH——Struts,spring,hibernate,逐渐被现在的SSM取代,当然了,还有各有优缺点的。 搭的这个框架中的SpringMVC并不是返回页面,而是返回json数据,在前端的js中处理页面的展现,我是为了让Android客户端能够访问SpringMVC的controller,并给Android客户端返回json数据考虑的。 一、还是一样的,要先看maven中都引入什么包: Java代码 复制代码 收藏代码 1.<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3. <modelVersion>4.0.0</modelVersion> 4. <groupId>CXFTest</groupId> 5. <artifactId>CXFTest</artifactId> 6. <packaging>war</packaging> 7. <version>0.0.1-SNAPSHOT</version> 8. <name>CXFTest Maven Webapp</name> 9. <url>http://maven.apache.org</url> 10. <dependencies> 11. <dependency> 12. <groupId>junit</groupId> 13. <artifactId>junit</artifactId> 14. <version>3.8.1</version> 15. <scope>test</scope> 16. </dependency> 17. <dependency> 18. <groupId>org.apache.aries.blueprint</groupId> 19. <artifactId>blueprint-parser</artifactId> 20. <version>1.3.1</version> 21. </dependency> 22. <dependency> 23. <groupId>org.apache.geronimo.specs</groupId> 24. <artifactId>geronimo-servlet_3.0_spec</artifactId> 25. <version>1.0</version> 26. </dependency> 27. <dependency> 28. <groupId>org.springframework</groupId> 29. <artifactId>spring-orm</artifactId> 30. <version>3.2.4.RELEASE</version> 31. </dependency> 32. <dependency> 33. <groupId>org.springframework</groupId> 34. <artifactId>spring-webmvc</artifactId> 35. <version>3.2.4.RELEASE</version> 36. </dependency> 37. <dependency> 38. <groupId>log4j</groupId> 39. <artifactId>log4j</artifactId> 40. <version>1.2.14</version> 41. </dependency> 42. <dependency> 43. <groupId>org.glassfish</groupId> 44. <artifactId>javax.servlet</artifactId> 45. <version>3.0.1</version> 46. <scope>provided</scope> 47. </dependency> 48. <dependency> 49. <groupId>mysql</groupId> 50. <artifactId>mysql-connector-java</artifactId> 51. <version>5.1.32</version> 52. </dependency> 53. <dependency> 54. <groupId>org.mybatis</groupId> 55. <artifactId>mybatis</artifactId> 56. <version>3.3.0</version> 57. </dependency> 58. <dependency> 59. <groupId>org.mybatis</groupId> 60. <artifactId>mybatis-spring</artifactId> 61. <version>1.2.3</version> 62. </dependency> 63. <dependency> 64. <groupId>c3p0</groupId> 65. <artifactId>c3p0</artifactId> 66. <version>0.9.1.2</version> 67. </dependency> 68.<!-- <dependency> 69. <groupId>com.google.code.gson</groupId> 70. <artifactId>gson</artifactId> 71. <version>2.4</version> 72. </dependency> --> 73. <dependency> 74. <groupId>com.fasterxml.jackson.core</groupId> 75. <artifactId>jackson-databind</artifactId> 76. <version>2.6.3</version> 77. </dependency> 78. 79. </dependencies> 80. <build> 81. <finalName>CXFTest</finalName> 82. <pluginManagement> 83. <plugins> 84. <plugin> 85. <groupId>org.apache.tomcat.maven</groupId> 86. <artifactId>tomcat7-maven-plugin</artifactId> 87. <version>2.1</version> 88. </plugin> 89. </plugins> 90. </pluginManagement> 91. </build> 92.</project> 二、第二点那必须是web.xml了: Xml代码 复制代码 收藏代码 1.<?xml version="1.0" encoding="UTF-8"?> 2. 3.<web-app> 4. 5. <display-name>CXFTest</display-name> 6.<welcome-file-list> 7. <welcome-file>view/test.jsp</welcome-file> 8. </welcome-file-list> 9. <!-- 配置字符集 --> 10. <filter> 11. <filter-name>encodingFilter</filter-name> 12. <filter-class>org.springframework.web.filter.CharacterEncodingFilter 13. </filter-class> 14. <init-param> 15. <param-name>encoding</param-name> 16. <param-value>UTF-8</param-value> 17. </init-param> 18. <init-param> 19. <param-name>forceEncoding</param-name> 20. <param-value>true</param-value> 21. </init-param> 22. </filter> 23. <filter-mapping> 24. <filter-name>encodingFilter</filter-name> 25. <url-pattern>/*</url-pattern> 26. </filter-mapping> 27. 28. <context-param> 29. <param-name>log4jConfigLocation</param-name> 30. <!-- <param-value>classpath*:config/log4j.properties</param-value> --> 31. <param-value>/WEB-INF/log4j.properties</param-value> 32. </context-param> 33. <listener> 34. <listener-class> 35. org.springframework.web.util.Log4jConfigListener 36. </listener-class> 37. </listener> 38. 39. <!-- 加载所有的配置文件 --> 40. <context-param> 41. <param-name>contextConfigLocation</param-name> 42. <param-value>classpath*:config/applicationContext-*.xml</param-value> 43. </context-param> 44. <listener> 45. <listener-class>org.springframework.web.context.ContextLoaderListener 46. </listener-class> 47. </listener> 48. 49. <!-- 配置springMVC --> 50. <servlet> 51. <servlet-name>springMVC</servlet-name> 52. <servlet-class>org.springframework.web.servlet.DispatcherServlet 53. </servlet-class> 54. <init-param> 55. <param-name>contextConfigLocation</param-name> 56. <param-value>classpath*:config/spring_mvc.xml</param-value> 57. </init-param> 58. <load-on-startup>1</load-on-startup> 59. </servlet> 60. <servlet-mapping> 61. <servlet-name>springMVC</servlet-name> 62. <url-pattern>/</url-pattern> 63. </servlet-mapping> 64. 65.</web-app> 三、springMVC配置文件: Java代码 复制代码 收藏代码 1.<?xml version="1.0" encoding="UTF-8"?> 2.<beans xmlns="http://www.springframework.org/schema/beans" 3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4. xmlns:mvc="http://www.springframework.org/schema/mvc" 5. xsi:schemaLocation="http://www.springframework.org/schema/beans 6. http://www.springframework.org/schema/beans/spring-beans.xsd 7. http://www.springframework.org/schema/context 8. http://www.springframework.org/schema/context/spring-context-3.2.xsd 9. http://www.springframework.org/schema/mvc 10. http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 11. 12. <!-- 注解扫描包 --> 13. <context:component-scan base-package="com.wang.cxf.test" /> 14. 15. <!-- 开启注解 --> 16. <mvc:annotation-driven /> 17. 18. <!-- 静态资源(js/image)的访问 --> 19. <mvc:resources location="/resource/" mapping="/resource/**" /> 20. 21. <!-- 定义视图解析器 --> 22. <bean id="viewResolver" 23. class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 24. <property name="prefix" value="/view/"></property> 25. <property name="suffix" value=".jsp"></property> 26. </bean> 27. 28.</beans> 四、在搭建这步的时候有点费劲了。关键是spring和mybatis的整合,不太熟悉,不知道dao层怎么弄, Java代码 复制代码 收藏代码 1.<?xml version="1.0" encoding="UTF-8"?> 2.<beans xmlns="http://www.springframework.org/schema/beans" 3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4. xmlns:jaxws="http://cxf.apache.org/jaxws" 5. xsi:schemaLocation=" 6. http://www.springframework.org/schema/beans 7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 8. http://camel.apache.org/schema/spring 9. http://camel.apache.org/schema/spring/camel-spring.xsd 10. http://cxf.apache.org/jaxws 11. http://cxf.apache.org/schemas/jaxws.xsd"> 12. <!-- 配置数据源 --> 13. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 14. <property name="driverClass" value="com.mysql.jdbc.Driver" /> 15. <property name="jdbcUrl" value="jdbc:mysql://localhost/mytest"></property> 16. <property name="user" value="root"></property> 17. <property name="password" value="123456"></property> 18. </bean> 19. 20. <!-- mybatis 操作数据 --> 21. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 22. <property name="configLocation" value="classpath:mybatis/mybatis-conf.xml"></property> 23. <property name="dataSource" ref="dataSource" /> 24. </bean> 25. 26. <bean class="org.mybatis.spring.SqlSessionTemplate"> 27. <constructor-arg ref="sqlSessionFactory" /> 28. </bean> 29. 30. <!-- DAO接口所在包名,Spring会自动查找其下的类 --> 31. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 32. <property name="basePackage" value="com.wang.cxf.test.dao" /> 33. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> 34. </bean> 35. 36.</beans> spring和mybatis整合后,只需要写dao层的接口,不用写实现类,在mybatis的配置文件中就是dao层的实现类了。 Java代码 复制代码 收藏代码 1.<?xml version="1.0" encoding="UTF-8"?> 2.<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3.<mapper namespace="com.wang.cxf.test.dao.TestDao"> 4. 5. <select id="getAllTest" resultType="com.wang.cxf.test.model.Test"> 6. select id, name from test 7. </select> 8. 9.</mapper> 之后,就能够想以前一样使用如下代码了: Java代码 复制代码 收藏代码 1.@Autowired 2.private TestDao testDao; 五、我的整个框架的结构:
Maven+Spring+SpringMVC+MyBatis框架搭建
标签:
原文地址:http://www.cnblogs.com/opisnhihs/p/4942800.html