您好,登錄后才能下訂單哦!
怎么快速搭建一個SSM框架?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
一、我用的是idea
二、首先創建一個maven項目,結構如下:
三、開始寫配置文件
pom文件
<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.xxx.demo</groupId> <artifactId>ssm</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>ssm 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.2.6.RELEASE</spring.version> <!-- mybatis版本號 --> <mybatis.version>3.2.8</mybatis.version> <!-- mysql驅動版本號 --> <mysql-driver.version>5.1.29</mysql-driver.version> <!-- log4j日志包版本號 --> <slf4j.version>1.7.18</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> <!-- 添加jstl依賴 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <!-- 添加junit4依賴 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <!-- 指定范圍,在測試時才會加載 --> <scope>test</scope> </dependency> <!-- 添加spring核心依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</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-oxm</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-jdbc</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-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</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.2.2</version> </dependency> <!-- 添加mysql驅動依賴 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-driver.version}</version> </dependency> <!-- 添加數據庫連接池依賴 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.3</version> </dependency> <!-- 添加fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.41</version> </dependency> <!-- 添加日志相關jar包 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- log end --> <!-- 映入JSON --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.10</version> </dependency> </dependencies> <build> <finalName>ssm</finalName> </build> </project>
db.properties
db.Driver=com.mysql.jdbc.Driver db.Url=jdbc:mysql://localhost:3306/myuser?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull db.username=root db.password=xxxxxxxxxxxxx #定義初始連接數 db.initialSize=1 #定義最大連接數 db.maxActive=20 #定義最大空閑 db.maxIdle=20 #定義最小空閑 db.minIdle=1 #定義最長等待時間 db.maxWait=60000 #使用非公平鎖 db.useUnfairLock=true #配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 db.timeBetweenEvictionRunsMillis=60000 #配置一個連接在池中最小生存的時間,單位是毫秒 db.minEvictableIdleTimeMillis=300000 db.validationQuery=SELECT 'x' #申請連接的時候檢測,如果空閑時間大于timeBetweenEvictionRunsMillis,執行validationQuery檢測連接是否有效。 db.testWhileIdle=true #申請連接時執行validationQuery檢測連接是否有效 db.testOnBorrow=false #歸還連接時執行validationQuery檢測連接是否有效 db.testOnReturn=false #打開PSCache,并且指定每個連接上PSCache的大小 db.poolPreparedStatements=true db.maxOpenPreparedStatements=20 #配置監控統計攔截的filters,去掉后監控界面sql無法統計 db.filters=start
log4j.properties
log4j.rootLogger=DEBUG,CONSOLE,FILEOUT log4j.addivity.org.apache=true # CONSOLE 打印到控制臺 log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.Threshold=DEBUG log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH\:mm\:ss} \:%m%n # # FILEOUT 打印到 <span >D\:\\report.log</span> log4j.appender.FILEOUT=org.apache.log4j.RollingFileAppender log4j.appender.FILEOUT.File=D\:\\report.log log4j.appender.FILEOUT.layout=org.apache.log4j.PatternLayout log4j.appender.fileout.MaxFileSize=100000KB log4j.appender.FILEOUT.Append=true #log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d \u2013 %c -%-4r [%t] %-5p %c %x \u2013 %m%n log4j.appender.FILEOUT.layout.ConversionPattern=[%-5p]_%d{yyyy-MM-dd HH\:mm\:ss} \:%m%n
UserMapper.xml配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.ssm.dao.UserDao"> <resultMap type="com.ssm.pojo.User" id="User"> <id column="id" property="id"/> <result column="username" property="username" /> <result column="password" property="password"/> </resultMap> <select id="seleceUserById" parameterType="int" resultMap="User"> select * from t_user WHERE id=#{id} </select> </mapper>
sp-mybatis配置
<?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"> <!--掃描數據庫配置文件--> <context:property-placeholder location="classpath:db.properties"></context:property-placeholder> <!-- 第二種方式:加載多個properties文件 <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> <value>classpath:common.properties</value> </list> </property> <property name="fileEncoding" value="UTF-8"/> </bean> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="properties" ref="configProperties"/> </bean> --> <!--Druid配置數據源--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${db.Driver}"></property> <property name="url" value="${db.Url}"></property> <property name="username" value="${db.username}"></property> <property name="password" value="${db.password}"></property> <property name="initialSize" value="${db.initialSize}"/><!-- 配置初始化連接池數量--> <property name="minIdle" value="${db.minIdle}"/><!-- 配置最小連接池數量--> <property name="maxActive" value="${db.maxActive}"/><!-- 配置最大連接池數量--> <property name="maxWait" value="${db.maxWait}"/><!-- 配置獲取連接等待超時的時間 單位毫秒--> <property name="useUnfairLock" value="${db.useUnfairLock}"/><!--使用非公平鎖--> <property name="timeBetweenEvictionRunsMillis" value="${db.timeBetweenEvictionRunsMillis}"/><!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="${db.minEvictableIdleTimeMillis}"/><!-- 配置一個連接在池中最小生存的時間,單位是毫秒 --> <property name="validationQuery" value="${db.validationQuery}"/><!--用來檢測連接是否有效的sql,要求是一個查詢語句。--> <property name="testWhileIdle" value="${db.testWhileIdle}"/><!--申請連接的時候檢測,如果空閑時間大于timeBetweenEvictionRunsMillis,執行validationQuery檢測連接是否有效。--> <property name="testOnBorrow" value="${db.testOnBorrow}"/><!--申請連接時執行validationQuery檢測連接是否有效,--> <property name="testOnReturn" value="${db.testOnReturn}"/><!--歸還連接時執行validationQuery檢測連接是否有效,--> <property name="poolPreparedStatements" value="${db.poolPreparedStatements}"/><!-- 打開PSCache,并且指定每個連接上PSCache的大小 --> <property name="maxOpenPreparedStatements" value="${db.maxOpenPreparedStatements}"/> <property name="filters" value="${db.filters}"/><!-- 配置監控統計攔截的filters,去掉后監控界面sql無法統計 --> </bean> <!-- mybatis和spring完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> </bean> <!-- DAO接口所在包名,Spring會自動查找其下的類 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ssm.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
sp-service.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.ssm.service"> </context:component-scan> </beans>
sp-trans.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 注解方式配置事務 --> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- 攔截器方式配置事物 --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <!--REQUIRED支持當前存在的事務,如果沒有的話句創建一個新的事務 PROPAGATION_REQUIRED:支持當前事務,如果當前沒有事務,就新建一個事務。這是最常見的選擇。 SUPPORTS:支持當前事務,如果當前沒有事務,就以非事務方式執行。 MANDATORY:支持當前事務,如果當前沒有事務,就拋出異常。 REQUIRES_NEW:新建事務,如果當前存在事務,把當前事務掛起。 NOT_SUPPORTED:以非事務方式執行操作,如果當前存在事務,就把當前事務掛起。 NEVER:以非事務方式執行,如果當前存在事務,則拋出異常。 NESTED:支持當前事務,如果當前事務存在,則執行一個嵌套事務(還不是太明白),如果當前沒有事務,就新建一個事務。 --> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="append*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="modify*" propagation="REQUIRED" /> <tx:method name="edit*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="repair" propagation="REQUIRED" /> <tx:method name="delAndRepair" propagation="REQUIRED" /> <tx:method name="get*" propagation="SUPPORTS" /> <tx:method name="find*" propagation="SUPPORTS" /> <tx:method name="load*" propagation="SUPPORTS" /> <tx:method name="search*" propagation="SUPPORTS" /> <tx:method name="datagrid*" propagation="SUPPORTS" /> <tx:method name="*" propagation="SUPPORTS" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* com.ssm.service..*.*(..))" /> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /> </aop:config> <!-- 配置druid監控spring jdbc --> <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"> </bean> <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype"> <property name="patterns"> <list> <value>com.ssm.service.*</value> </list> </property> </bean> <aop:config> <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" /> </aop:config> </beans>
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: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.xsd"> <!--掃描controller--> <context:component-scan base-package="com.ssm.controller"></context:component-scan> <!--mvc驅動--> <mvc:annotation-driven></mvc:annotation-driven> <!--視圖解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsps"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 文件上傳配置 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 默認編碼 --> <property name="defaultEncoding" value="UTF-8"/> <!-- 上傳文件大小限制為31M,31*1024*1024 --> <property name="maxUploadSize" value="32505856"/> <!-- 內存中的最大值 --> <property name="maxInMemorySize" value="4096"/> </bean> </beans>
web.xml配置
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/sp-*.xml</param-value> </context-param> <!-- 編碼過濾器 --> <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> <!--Spring溢出監聽器 <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener>--> <!--Spring監聽器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--配置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/springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--session配置--> <session-config> <session-timeout>15</session-timeout> </session-config> </web-app>
然后基本框架已經配置好了,剩下的就是寫java代碼了
Controller
package com.ssm.controller; import com.ssm.pojo.User; import com.ssm.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; /** * @author: hsj * @Date: 2017/9/15 * @Description : */ @Controller @RequestMapping(value = "/user") public class UserController { @Resource private UserService userService; @RequestMapping(value = "user") @ResponseBody public User getUser(int userId){ return userService.findUserById(userId); } }
Service
package com.ssm.service; import com.ssm.pojo.User; import org.springframework.stereotype.Service; /** * @author: hsj * @Date: 2017/9/15 * @Description : */ public interface UserService { User findUserById(int userId); }
ServiceImpl
package com.ssm.service.impl; import com.ssm.dao.UserDao; import com.ssm.pojo.User; import com.ssm.service.UserService; import org.springframework.stereotype.Service; import javax.annotation.Resource; /** * @author: hsj * @Date: 2017/9/15 * @Description : */ @Service public class UserServiceImpl implements UserService { @Resource private UserDao userDao; public User findUserById(int userId) { return userDao.seleceUserById(userId); } }
pojo
package com.ssm.pojo; /** * @author: hsj * @Date: 2017/9/18 * @Description :這是一個User實體類 */ public class User { private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }
dao
package com.ssm.dao; import com.ssm.pojo.User; import org.springframework.stereotype.Repository; /** * @author: hsj * @Date: 2017/9/15 * @Description : */ @Repository public interface UserDao { User seleceUserById(int userId); }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。