标签:resources line 配置 insert password reporting schema 目录 new
打开 IDEA 工具,通过 File -> New -> Project,新建项目,或者直接在 IDEA 欢迎页面点击新建项目
“ GroupID ” 是项目组织唯一的标识,实际对应 Java 的包结构,是 main 目录里 Java 的目录结构,
“ ArtifactID ” 是项目的唯一的标识符,实际对应项目的名称,也就是项目根目录的名称,名为 ssmdemo 。
一路next 对于maven 这里不详细介绍
Pom中导入相关依赖
<dependencies> <!-- Begin: Spring依赖 --> <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> <!-- End: Spring依赖 --> <!-- Begin: Spring MVC依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- End: Spring MVC依赖 --> <!-- Begin: Mybatis依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis-spring.version}</version> </dependency> <!-- End: mybatis依赖 --> <!-- Begin: 数据库依赖包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${jdbc.driver.version}</version> <scope>runtime</scope> </dependency> <!-- End: 数据库依赖包 --> <!-- Begin: aspectj相关jar包--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${aspectj.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectj.version}</version> </dependency> <!-- End: aspectj相关jar包--> <!-- Begin: Servlet相关依赖包 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${javax.servlet-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>${jsp-api.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <!-- End: Servlet相关依赖包 --> </dependencies>
对于版本集中管理
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring.version>4.2.4.RELEASE</spring.version>
<java.version>1.8</java.version>
<jdbc.driver.version>8.0.16</jdbc.driver.version>
<aspectj.version>1.7.4</aspectj.version>
<javax.servlet-api.version>3.1.0</javax.servlet-api.version>
<jsp-api.version>2.2</jsp-api.version>
<jstl.version>1.2</jstl.version>
<jackson.version>2.5.3</jackson.version>
<mybatis.version>3.2.5</mybatis.version>
<mybatis-spring.version>1.2.2</mybatis-spring.version>
<maven.test.skip>true</maven.test.skip>
</properties>
完善目录结构
通过 Maven 骨架生成的项目结构只有 main 目录及其目录下的 webapp 目录,这两个目录并不完善,缺少了 java 目录和 resource 目录,因此需要在 main 目录新建这两个目录,过程如下:
main -> New -> Directory -> 新建 Java 目录 main -> New -> Directory -> 新建 Resources 目录
之后需要右键选择 “ Mark Directory as ” ,分别将 java 目录和 resource 目录设置为 “ 源码根目录 ” 和 “ 配置文件目录 ”
之后在 resources 目录下新建 mappers 目录用来存放 MyBatis 的 mapper 文件,整个项目的目录结构就完成了,如下所示:
ssmdemo ——项目名称
├── src/main/java ——Java 源码根目录
├── controller ——控制层目录
├── dao ——dao层目录
├── entity ——实体层目录
├── service ——业务层目录
└── impl ——业务实现类目录
└── utiles ——工具类目录
├── src/main/resources ——配置文件根目录
└── mappers ——mapper文件目录
├── src/main/webapp ——网站 Web 资源
└── pom.xml ——pom文件
整合spring
resource 目录下创建applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 自动扫描 --> <context:component-scan base-package="com.ssm.demo.dao"/> <context:component-scan base-package="com.ssm.demo.service"/> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/ssm-demo"/> <!-- 改为你的地址即可 --> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <!-- 配置mybatis的sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 自动扫描mappers.xml文件 --> <!--<property name="mapperLocations" value="classpath:mappers/*.xml"></property>--> <!-- mybatis配置文件 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ssm.demo.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置事务通知属性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- 定义事务传播属性 --> <tx:attributes> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="upd*" propagation="REQUIRED"/> <tx:method name="edit*" propagation="REQUIRED"/> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="new*" propagation="REQUIRED"/> <tx:method name="set*" propagation="REQUIRED"/> <tx:method name="remove*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="change*" propagation="REQUIRED"/> <tx:method name="check*" propagation="REQUIRED"/> <tx:method name="get*" propagation="REQUIRED" read-only="true"/> <tx:method name="search*" propagation="REQUIRED" read-only="true"/> <tx:method name="find*" propagation="REQUIRED" read-only="true"/> <tx:method name="load*" propagation="REQUIRED" read-only="true"/> <tx:method name="*" propagation="REQUIRED" read-only="true"/> </tx:attributes> </tx:advice> <!-- 配置事务切面 --> <aop:config> <aop:pointcut id="serviceOperation" expression="(execution(* com.ssm.demo.service.*.*(..)))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/> </aop:config> </beans>
整合mybatis
resource 目录下创建 mybatis-config.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> <typeAliases> <package name="com.ssm.demo.entity"/> </typeAliases> </configuration>
整合springmvc
resource 目录下创建 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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 使用注解的包,包括子集 --> <context:component-scan base-package="com.ssm.demo.controller"/> <!-- 视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"></property> </bean> </beans>
最重要的是在web.xml中进行配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>ssm-demo</display-name> <!--Start 欢迎页--> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!--End 欢迎页--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!--Start 编码过滤器 解决乱码问题--> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--End 编码过滤器 解决乱码问题--> <!--Start spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--End Start spring监听器 --> <!--Start spring mvc servlet--> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!--End spring mvc servlet--> <!--Start servlet-mapping --> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!--End servlet-mapping --> </web-app>
完整的项目目录
配置Tomcat 运行项目
启动成功 运行效果
loading...
标签:resources line 配置 insert password reporting schema 目录 new
原文地址:https://www.cnblogs.com/lflying/p/10997473.html