使用intelliJ IDE开发java web项目
<!-- spring版本号 -->
<spring.version>4.3.0.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.4.4</mybatis.version>
<!-- mysql驱动版本号 -->
<mysql-driver.version>5.1.40</mysql-driver.version>
<!-- log4j日志包版本号 -->
<log4j.version>2.7</log4j.version>
<commons-logging.version>1.2</commons-logging.version>
<!-- 对象转 jackson 版本号 -->
<jackson.version>1.9.13</jackson.version>
使用maven 开发springmvc apring mybatis web项目。
1.File -> New Project ,进入创建项目窗口
2 出现以下界面
3 按照如上选项进行点击,点next
4 上面两项随便填,点击next
上面选项中可以直接使用默认配置。我修改的local repository是本地仓库位置,可以改也可以不改。下面在Properties中添加一个参数 archetypeCatalog=internal,不加这个参数,在maven生成骨架的时候将会非常慢,有时候直接卡住。来自网上的解释:archetypeCatalog表示插件使用的archetype元数据,不加这个参数时默认为remote,local,即中央仓库archetype元数据,由于中央仓库的archetype太多了所以导致很慢,指定internal来表示仅使用内部元数据。
5 点击next
这个我没有修改,点击finish
6 生成maven的项目骨架之后,我们还需要手动在 src/main 下创建 java目录。现在可以直接编写了
到此为止,项目的框架基本搭建完成。
7 下面进行pom.xml进行配置
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.huitong</groupId> <artifactId>webapp1</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>webapp1 Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- spring版本号 --> <spring.version>4.3.0.RELEASE</spring.version> <!-- mybatis版本号 --> <mybatis.version>3.4.4</mybatis.version> <!-- mysql驱动版本号 --> <mysql-driver.version>5.1.40</mysql-driver.version> <!-- log4j日志包版本号 --> <log4j.version>2.7</log4j.version> <commons-logging.version>1.2</commons-logging.version> <!-- 对象转 jackson 版本号 --> <jackson.version>1.9.13</jackson.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- 添加jstl依赖 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- javaee 7.0 --> <!-- https://mvnrepository.com/artifact/javax/javaee-api --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <!-- ######## spring mvc 依赖包 start ################################--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- ######## spring mvc 依赖包 end ################################--> <!-- ######## spring core 依赖包 start ################################--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <!-- ######## spring core 依赖包 end ################################--> <!-- ######## mybatis core 依赖包 start ################################--> <!-- https://mvnrepository.com/artifact/org.ow2.asm/asm --> <dependency> <groupId>org.ow2.asm</groupId> <artifactId>asm</artifactId> <version>5.2</version> </dependency> <!-- https://mvnrepository.com/artifact/cglib/cglib --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.2.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- ######## mybatis core 依赖包 end ################################--> <!-- ######## mybatis spring 依赖包 start ################################--> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- ######## mybatis spring 依赖包 end ################################--> <!-- ######## DBconnector 依赖包 start ################################--> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-driver.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- ######## DBconnector 依赖包 end ################################--> <!-- ######## translation 依赖包 start ################################--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <!-- ######## translation 依赖包 end ################################--> <!-- ######## AOP 依赖包 start ################################--> <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance --> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.10</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.10</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <!-- ######## AOP 依赖包 end ################################--> <!-- ######## json 依赖包 start ################################--> <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>${jackson.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>${jackson.version}</version> </dependency> <!-- ######## json 依赖包 end ################################--> <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>${commons-logging.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>${log4j.version}</version> </dependency> </dependencies> <build> <finalName>webapp1</finalName> </build> </project>
写完pom.xml文件后需要更新依赖,
点击更新。
8 按照常规步骤开发SSM项目即可。
可以参考SSM整合开发流程
9 spring.xml项目配置,mybatis配置文件指定,spring配置,springMVC配置
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <!-- 1 dataSource--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql:///day17?useSSL=true"/> <property name="user" value="root"/> <property name="password" value="123456"/> <property name="initialPoolSize" value="3"/> <property name="maxPoolSize" value="10"/> <property name="maxStatements" value="20"/> <property name="acquireIncrement" value="2"/> </bean> <!-- 2 sqlSessionFactoryBean--> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis/mybatis.xml"/> <property name="dataSource" ref="dataSource"/> </bean> <!-- 3 dao/service --> <bean id="studentDao" class="com.huitong.dao.StudentDao"> <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/> </bean> <bean id="studentService" class="com.huitong.service.StudentService"> <property name="studentDao" ref="studentDao"/> </bean> <!-- 4 scan --> <context:component-scan base-package="com.huitong.action"/> <!-- 5 transaction --> <!-- 5.1 txManager --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 5.2 txAdvice --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="query*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 5.3 AOP --> <aop:config> <aop:pointcut id="pt" expression="execution(* com.huitong.service.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/> </aop:config> <!-- 6 internalResourceViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <mvc:interceptors> <mvc:interceptor> <!-- 先匹配所有路径,然后排除不需要检查的路径 --> <mvc:mapping path="/**"/> <!-- 与登录相关的请求 --> <mvc:exclude-mapping path="/*.jsp"/> <mvc:exclude-mapping path="/student/login.action"/> <mvc:exclude-mapping path="/student/register.action"/> <!-- 以下是静态资源 --> <mvc:exclude-mapping path="/images/**" /> <mvc:exclude-mapping path="/css/**" /> <mvc:exclude-mapping path="/font/**" /> <mvc:exclude-mapping path="/js/**" /> <mvc:exclude-mapping path="/datepicker/**" /> <!-- 用户是否已经登录的检查bean --> <bean class="com.huitong.util.StudentAuth"/> </mvc:interceptor> </mvc:interceptors> </beans>
10 项目部署,这几步很关键。可以参考 图文详解 IntelliJ IDEA 15 创建普通 Java Web 项目
常见问题
1 maven项目中java文件夹不能新建package和class,需要做如下设置
2 在idea中安装mybatis插件
选择file -> setting -> plugins,搜索mybatis,这时因为没有安装过插件,所以会出现如图所示:
点击Browse,这是会弹出如下窗口,intellij idea会自动从网络搜索插件,方便下载,如图所示:
点击Install plugin即可自动下载安装,之后重启intellij idea即可。
3 在进行mapper文件映射时出现 URI is not registered.解决办法如下
鼠标点击红色字,然后Intellij出现小红灯,选择Fetch external resource即可解决
4 MAVEN项目jar包依赖导入问题解决,
在新建项目后,项目的路径中会生成pom.xml文件和项目名.iml文件。新建项目后,IDEA不会自动刷新Maven的依赖。刷新Maven配置的方法为:
(1)右键单击项目;
(2)在弹出菜单中选择Maven|Reimport菜单项。
此时,IDEA将通过网络自动下载相关依赖,并存放在Maven的本地仓库中。
如果觉得有用,想赞助一下请移步赞助页面:赞助一下