idea创建Maven版的ssm项目
Posted 明月松间照
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了idea创建Maven版的ssm项目相关的知识,希望对你有一定的参考价值。
要使用idea创建一个maven项目,首先电脑安装maven,maven下载地址:http://maven.apache.org/download.cgi
1、打开idea,选择创建一个新项目,选择maven创建
选择jdk版本,不需要选择create from archetype,否则你会很可能没有src包或者scr下的main/java等包。点击下一步
2、输入Nama(项目名称),groupid(项目的包名),artifactid(项目名,与Name一致),Version不用管,然后点击下一步完成创建。
3、创建完成后,初始的目录结构是这样的,
此时的项目里面没有web文件夹,我们需要自己去添加。
点击工具栏的该图标,进入以下页面
然后选择左侧栏里的Modules,点击添加,选择web,如图:
完成后在java文件夹里面添加pojo、service等包,结构如下图
4.在pom.xml中引入ssm需要的相关依赖
<?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>om.mysjz</groupId> <artifactId>maven-test</artifactId> <version>1.0-SNAPSHOT</version> <properties> <!--设置项目编码编码--> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <!-- spring版本号 方便在下面的依赖中进行引用--> <spring.version>4.3.5.RELEASE</spring.version> <!-- mybatis版本号 方便在下面的依赖中进行引用--> <mybatis.version>3.4.1</mybatis.version> </properties> <!-- 配置阿里云仓库,要不然从中央maven库下载很慢--> <repositories> <repository> <id>nexus-aliyun</id> <name>nexus-aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <!-- 添加项目里面的依赖 --> <dependencies> <!-- java ee --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <!-- log4j日志 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!-- 实现slf4j接口并整合 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.2</version> </dependency> <!-- JSON --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.7</version> </dependency> <!-- 数据库 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> <scope>runtime</scope> </dependency> <!-- 数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.8</version> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- mybatis/spring整合包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <!-- aop的配置 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.11</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.1_3</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.1</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <finalName>springspringmvcmybatis</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <!-- 设置JDK版本 --> <source>13</source> <target>13</target> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>
5.在resource目录下创建一个mybatisconfig文件,用于配置Mybatis.xml文件,命名为SqlMapConfig.xml。
<?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> <!-- 这里面你可以配置一下关于JavaBean中类的一些别名,这样就方便在之后的Mapper文件中进行处理,包括开启下划线转驼峰命名 --> <typeAliases> <package name="com.mysjz.pojo"/> </typeAliases> </configuration>
6.在resource目录下创建一个mapperxmlconfig文件,用于存放mybatis的mapper映射文件
7.在resource目录下创建一个springconfig的文件,这里存放之后的关于spring的一些配置文件;
(1)在springconfig文件中,创建applicationContext-dao.xml文件,主要是配置:加载properties文件,数据源,SqlSessionFactory,Mapper扫描。
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!-- 配置 读取properties文件 jdbc.properties --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 自动扫描所有的注解 --> <context:component-scan base-package="com.mysjz"/> <!-- 配置阿里的druid数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 设置数据源 --> <property name="dataSource" ref="dataSource"/> <!-- 设置MyBatis核心配置文件 --> <property name="configLocation" value="classpath:mybatisconfig/SqlMapConfig.xml" /> <!-- 设置扫描mapper.xml文件 --> <property name="mapperLocations" value="classpath:mapperxmlconfig/*Mapper.xml"/> </bean> <!-- 配置Mapper层java类扫描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 设置Mapper扫描包 --> <property name="basePackage" value="com.mysjz.mapper" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> </beans>
刚开始创建时扫描mapper.xml文件的value可能会报错,是因为mapperxmlconfig.xml中没有以Mapper结尾的xml文件,可以先不用管或者注释掉,当开始写项目的时候再往里面添加。
(2)在springconfig文件中,创建管理service层的文件,主要是对service包开启扫描。命名为applicationContext-service.xml。(也可以将该扫描步骤添加至dao层的配置文件中)
<?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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spr以上是关于idea创建Maven版的ssm项目的主要内容,如果未能解决你的问题,请参考以下文章