디렉토리 생성
pom. Struts
service의 작업과 동일: 비즈니스 로직 레이어-> 내부 다른 패키지 .impl 생성(예: com.imooc.Project name.service.impl)
jar 패키지를 쿼리할 수 있습니다. :http://mvnrepository.com/
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
logback
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency>
2.3.1 spring-core
은 구성 파일 액세스, Bean 생성 및 관리, IOS/DI 작업 수행을 포함하여 모든 애플리케이션에서 사용해야 합니다. .모든 관련 수업. Java와 유사한 반사 메커니즘
2.3.3 spring-context
2.3.4 spring-jdbc
제공 JDBC, Hibernate, JDO, JPA 등에 대한 일관된 선언적 및 프로그래밍 방식 트랜잭션 관리
WebApplicationContext 자동 로드
2.3을 포함하여 웹 애플리케이션을 개발할 때 Spring 프레임워크를 사용할 때 필요한 핵심 클래스가 포함되어 있습니다. 7 spring-webmvc
2.3.8 spring-test
범위 지정:
2.4.1 mybatis
2.4.2 mybatis-spring
2.4.3 mysql -connector-java
2.4.4 c3p0
연결 풀
서블릿 제공
서비스는 JSON을 구문 분석하는 데 사용됩니다. SpringMVC의 컨트롤러는 프런트 엔드와 상호 작용할 때 JSON 구문 분석
Map 도구 클래스가 많은 경우에 사용됩니다. JAVA 컬렉션
spring-core.jar에는 기본 클래스 지원을 제공하기 위해 commons-collections.jar이 필요합니다2.6 수정이 필요한 pom.xml의 전체 부분maven 프로젝트의 pom.xml 파일을 생성합니다<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> org.springframework spring-core ${spring.version} org.springframework spring-beans ${spring.version} org.springframework spring-context ${spring.version} org.springframework spring-jdbc ${spring.version} org.springframework spring-tx ${spring.version} org.springframework spring-web ${spring.version} org.springframework spring-webmvc ${spring.version} org.springframework spring-test ${spring.version} test javax.servlet javax.servlet-api 3.1.0 com.fasterxml.jackson.core jackson-databind 2.8.7 commons-collections commons-collections 3.2 org.mybatis mybatis 3.4.2 org.mybatis mybatis-spring 1.3.1 mysql mysql-connector-java 5.1.37 c3p0 c3p0 0.9.1.2 net.coobird thumbnailator 0.4.8 com.github.penggle kaptcha 2.3.2 commons-fileupload commons-fileupload 1.3.2 redis.clients jedis 2.9.0
<properties> <spring.version>4.3.7.RELEASE</spring.version> </properties>
을 입력하세요.
jdbc.properties
mybatis-config.xml
web.xml
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/{填写项目名}?useUnicode=true&characterEncoding=utf8 jdbc.username={databases的username} jdbc.password={databases的password}
계좌번호와 비밀번호를 임시로 평문으로 작성합니다.
3.2 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> <!-- 配置全局属性 --> <settings> <!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 --> <setting name="useGeneratedKeys" value="true" /> <!-- 使用列标签替换列别名 默认:true --> <setting name="useColumnLabel" value="true" /> <!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} --> <setting name="mapUnderscoreToCamelCase" value="true" /> </settings> </configuration>
3.3 Spring 관련 구성
3.3.1 spring-dao.xml 만들기
데이터베이스 연결 풀 구성
注意包名的填写:我用中文写了的位置,IDE会标红
<?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"> <!-- 配置整合mybatis过程 --> <!-- 1.配置数据库相关参数properties的属性:${url} --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 2.数据库连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 配置连接池属性 --> <property name="driverClass" value="${jdbc.driver}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- c3p0连接池的私有属性 --> <property name="maxPoolSize" value="30" /> <property name="minPoolSize" value="10" /> <!-- 关闭连接后不自动commit --> <property name="autoCommitOnClose" value="false" /> <!-- 获取连接超时时间 --> <property name="checkoutTimeout" value="10000" /> <!-- 当获取连接失败重试次数 --> <property name="acquireRetryAttempts" value="2" /> </bean> <!-- 3.配置SqlSessionFactory对象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource" /> <!-- 配置MyBaties全局配置文件:mybatis-config.xml --> <property name="configLocation" value="classpath:mybatis-config.xml" /> <!-- 扫描entity包 使用别名 --> <property name="typeAliasesPackage" value="{实体类包所在的位置}" /> <!-- 扫描sql配置文件:mapper需要的xml文件 --> <property name="mapperLocations" value="classpath:mapper/*.xml" /> </bean> <!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <!-- 给出需要扫描Dao接口包 --> <property name="basePackage" value="{找到需要填写的dao包}" /> </bean> </beans>
扫描Service包下所有使用注释的类型 [注意填写包名]
配置事务管理器:多个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:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 扫描service包下所有使用注解的类型 --> <context:component-scan base-package="{service包所在的目录,如com.cat.o2o.service}" /> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置基于注解的声明式事务 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
开启Spring注解模式,自动实现Controller,不用做bean的配置
静态资源默认servlet配置,需要在webapp下创建resource目录,图片等静态资源存放位置
视图解析器配置
扫描controller所在的包[需要填写你的controller所在的包]
文件内容如下:
<?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: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/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 配置SpringMVC --> <!-- 1.开启SpringMVC注解模式 --> <mvc:annotation-driven /> <!-- 2.静态资源默认servlet配置 (1)加入对静态资源的处理:js,gif,png (2)允许使用"/"做整体映射 告知SpringMVC的核心类dispatcher-servlet不要对此拦截,交由默认的servlet-handler处理--> <mvc:resources mapping="/resources/**" location="/resources/" /> <mvc:default-servlet-handler /> <!-- 3.定义视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/html/"></property> <property name="suffix" value=".html"></property> </bean> <!-- 4.扫描web(放置controller的包)相关的bean --> <context:component-scan base-package="需要修改的地方" /> </beans>
配置index文件,暂时默认即可
配置SpringMVC的Servlet,注意填写初始参数(指定目录)
配置Servlet-mapping,默认匹配所有的请求(url-pattern填写/即可)
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1" metadata-complete="true"> <display-name>Archetype Created Web Application</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>spring-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-*.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring-dispatcher</servlet-name> <!-- 默认匹配所有的请求 --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
相关推荐:
위 내용은 [자바 튜토리얼] SSM: 환경 설정의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!