當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SSM之一(使用idea创建一个Spring+SpringMVC的项目)
生活随笔
收集整理的這篇文章主要介紹了
SSM之一(使用idea创建一个Spring+SpringMVC的项目)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1. 使用idea創建一個基本的maven項目(Web版本)
- 2. 完善目錄結構
- 3. 相關依賴
- 4. 配置spring與springmvc
1. 使用idea創建一個基本的maven項目(Web版本)
此時的項目只有一個web項目最基本結構,能夠部署到tomcat上(有web.xml文件)。但是還需要我們繼續完善項目結構。
2. 完善目錄結構
在src目錄下創建java,resources兩個目錄,并將java設置為Sources Root,resources設置為Resources Root。
- java,Sources Root,存放項目源碼的目錄
- resources(Resources Root),存放資源的目錄
在java(Sources Root)創建包com.ssm。并且在該包下創建五個包
- controller,存放controller層的代碼
- service,存放service層的代碼
- dao,存放持久層代碼
- model,存放數據模型
- utils,一些第三方工具
在resources創建目錄
- mapper,存放mybatis的mapper文件
- configure,存放配置文件
最后的目錄結構如下:
3. 相關依賴
需要明確一點的是,針對上述構建的項目,雖然是一個web項目,但是歸根結底是一個maven項目,而且是一個使用了模板maven-archetype-webapp的maven項目。對于一個maven項目而言,其核心是pom.xml文件。
POM的的含義是項目對象模型(Project Object Model)。也就是說,maven通過pom.xml定義了項目的基本信息,用于描述如何構建,聲明項目依賴等等。
pom文件主要分為以下幾塊
- 項目的基本信息,
- 配置信息,比如在這里定義配置信息<spring-version>5.3.7.RELEASE</spring-version>,在下面的配置依賴配置中,直接使用${spring-version}代表5.3.7.RELEASE,有點我們java代碼中的spring-version=5.3.7.RELEASE,然后在接下來的代碼中使用spring-version一樣。
- 依賴配置,常用,導入jar包
- 編譯配置,顧名思義,就是設置編譯的配置信息,具體的等以后完善。
默認依賴
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.ssm</groupId><artifactId>ssm_04_full_edition_analysis</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><name>ssm_04_full_edition_analysis Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency></dependencies><build><finalName>ssm_04_full_edition_analysis</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin></plugins></pluginManagement></build> </project>主要是在dependencies標簽中配置依賴
<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.1.1.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>4.3.18.RELEASE</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency><!--2. 持久層(mybatis)配置--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.40</version></dependency><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.3</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.1</version></dependency><!--2. 配置完--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency></dependencies>4. 配置spring與springmvc
也就是配置項目接口能夠從網絡訪問。
配置web.xml
<!--1.配置Spring的IOC容器--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:configure/spring-context.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--1.完--><!--2.配置dispatcherServlet,并匹配所有的URL--><servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:configure/spring-mvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!--2.完-->spring-context.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--告訴Spring在創建容器需要掃描的包為com.ssm--><context:component-scan base-package="com.ssm"></context:component-scan> </beans>spring-mvc.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><mvc:annotation-driven /><context:component-scan base-package="com.ssm"></context:component-scan></beans>編寫controller層
@RequestMapping("/user") @Controller public class UserController {@AutowiredIUserService userServiceImpl = null;@RequestMapping(value = "/login",method = RequestMethod.POST)public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {String str = userServiceImpl.login();response.getWriter().print(str);} }編寫service層
@Service public class UserServiceImpl implements IUserService {@Overridepublic String login() {System.out.println("This is UserServiceImpl");return "hello World!";} }總結
以上是生活随笔為你收集整理的SSM之一(使用idea创建一个Spring+SpringMVC的项目)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 问题之JVM_Bind
- 下一篇: SSM之二(Spring整合Mybati