SSM框架搭建
1、首先使用idea创建maven骨架的web工程
2、设置项目包层级架构



2、pom文件导入jar包、插件
<?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>cn.jiaming.chen</groupId> <artifactId>SSMDemo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>SSMDemo Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!--mybatis--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <!--mybatis end--> <!-- spring basic --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.14.RELEASE</version> <!-- <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> --> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.14.RELEASE</version> </dependency> <!-- spring tx --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.13</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.13</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.0.4.RELEASE</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.4.GA</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.3.14.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.14.RELEASE</version> </dependency> <!-- SPRING end --> <!-- GSON begin --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.0</version> </dependency> <!-- GSON END --> <!-- JSON begin --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-jaxb-annotations</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-jaxrs</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> <!-- https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path --> <dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> <version>2.3.0</version> </dependency> <!-- JSON end --> </dependencies> <build> <finalName>SSMDemo</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <configuration> <httpConnector> <port>8090</port> </httpConnector> <webAppConfig> <contextPath>/</contextPath> </webAppConfig> <!--<scanIntervalSeconds>5</scanIntervalSeconds>--> <stopKey>foo</stopKey> <stopPort>9967</stopPort> </configuration> </plugin> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.0.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.0</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
3、配置web.xml文件
1 <!DOCTYPE web-app PUBLIC 2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 3 "http://java.sun.com/dtd/web-app_2_3.dtd" > 4 5 6 <web-app> 7 <!-- 把spring容器的启动交给web容器管理 --> 8 <context-param> 9 <param-name>contextConfigLocation</param-name> 10 <param-value>/WEB-INF/applicationContext.xml</param-value> 11 </context-param> 12 <!-- 创建监听器,在web容器启动的时候启动spring容器 --> 13 <listener> 14 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 15 </listener> 16 17 <display-name>Archetype Created Web Application</display-name> 18 <!-- 配置前端控制器 --> 19 <servlet> 20 <servlet-name>springmvc</servlet-name> 21 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 22 <init-param> 23 <param-name>contextConfigLocation</param-name> 24 <param-value>/WEB-INF/springmvc-servlet.xml</param-value> 25 </init-param> 26 </servlet> 27 <servlet-mapping> 28 <servlet-name>springmvc</servlet-name> 29 <url-pattern>/</url-pattern> 30 </servlet-mapping> 31 32 <!-- 配置post乱码过滤器 --> 33 <filter> 34 <filter-name>encodingFilter</filter-name> 35 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 36 <init-param> 37 <param-name>encoding</param-name> 38 <param-value>utf-8</param-value> 39 </init-param> 40 </filter> 41 <filter-mapping> 42 <filter-name>encodingFilter</filter-name> 43 <url-pattern>/*</url-pattern> 44 </filter-mapping> 45 </web-app>
4、配置spring的核心配置文件,路径:WEB-INF/applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd "> <!-- 开启包扫描,创建实体bean--> <context:component-scan base-package="cn.jiaming.chen.service"/> <!--配置数据源--> <!--读取配置文件key-value到spring上下文中--> <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:dataSource.properties</value> </list> </property> </bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass"> <value>${datasource.driverClassName}</value> </property> <property name="jdbcUrl"> <value>${datasource.url}</value> </property> <property name="user"> <value>${datasource.username}</value> </property> <property name="password"> <value>${datasource.password}</value> </property> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 <property name="acquireIncrement"> <value>${c3p0.acquireIncrement}</value> </property> //初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 <property name="initialPoolSize"> <value>${c3p0.initialPoolSize}</value> </property> <property name="minPoolSize"> <value>${c3p0.minPoolSize}</value> </property> <property name="maxPoolSize"> <value>${c3p0.maxPoolSize}</value> </property> //最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 <property name="maxIdleTime"> <value>${c3p0.maxIdleTime}</value> </property> //每60秒检查所有连接池中的空闲连接。Default: 0 <property name="idleConnectionTestPeriod"> <value>${c3p0.idleConnectionTestPeriod}</value> </property> //JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。 如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 <property name="maxStatements"> <value>${c3p0.maxStatements}</value> </property> //c3p0是异步操作的,缓慢的JDBC操作通过帮助进程完成。扩展这些操作可以有效的提升性能 通过 多线程实现多个操作同时被执行。Default: 3 <property name="numHelperThreads"> <value>${c3p0.numHelperThreads}</value> </property> --> </bean> <!-- 配置事物管理器,id最好叫transactionManager, --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置事物通知 ,配置整个系统中的事物策略,如果不配置transaction-manager时默认让其等于transactionManager--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- name表示需要添加事物的方法的名称,propagation表示事物传播的策略,默认值为:REQUEST --> <!-- REQUIRED(方法必须使用事物)、SUPPORTS(是否使用事物跟随原来是否使用事物来决定)、read-only="true"(表示当前方法不能修改数据库,只能读数据,否则报错,底层有优化,执行效率非常高) --> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!--配置事物切面 ,配置顺序:piontcut*、advisor*、aspect--> <aop:config> <!--顺序不能换--> <aop:pointcut expression="execution(* cn.jiaming.chen.service.*.*(..))" id="pc"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/> </aop:config> <!-- 整合Mybaits到spring容器中,把类SqlSessionFactoryBean对象交给spring容器管理 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 把数据源注入到工厂中 --> <property name="dataSource" ref="dataSource"></property> <!-- 把Mybaits核心配置文件加载到工厂中;由于数据源被spring管理了,而配置文件也被传到工厂了,所以核心配置文件只需要配置一些插件和二级缓存、驼峰映射之类的--> <property name="configLocation" value="/WEB-INF/mybatis/sqlMapConfig.xml"></property> <!-- 把所有的映射文件传递到工厂内部 --> <property name="mapperLocations" value="/WEB-INF/mybatis/mappers/*.xml"></property> </bean> <!-- 通过对象为指定的接口创建代理对象,映射到映射文件 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 把指定的接口包的全路径传递到对象内部 --> <property name="basePackage" value="cn.jiaming.chen.dao.mapper"></property> <property name="annotationClass" value="org.springframework.stereotype.Repository"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> </beans>
5、配置springmvc核心配置文件:WEB-INF/springmvc-servlet.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" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" 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-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <!--开启包扫描,自动开启注解识别--> <context:component-scan base-package="cn.jiaming.chen.controller"/> <!--配置注解识别 <context:annotation-config/> --> <!--开启根据注解来托管bean功能--> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <!--指定为application/json,默认只接受json格式参数的请求,其他格式请求不会响应--> <property name="supportedMediaTypes" value="application/json;charset=UTF-8" /> </bean> <bean class="org.springframework.http.converter.json.GsonHttpMessageConverter"/> </mvc:message-converters> </mvc:annotation-driven> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/page/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--springmvc拦截器 <mvc:interceptors> <mvc:interceptor> //拦截策略:database/**表示拦截database/前缀的所有请求,database/*表示只拦截database/前缀下一级的请求 <mvc:mapping path="/SSMDemo/**"/> <bean class=""/> </mvc:interceptor> //可以配置多个拦截器拦截不同路径 </mvc:interceptors> --> </beans>
6、配置mybatis核心配置文件:WEB-INF/mybatis/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> <settings> <!-- 开启驼峰自动映射 --> <setting name="mapUnderscoreToCamelCase" value="true" /> <!-- 二级缓存的总开关 --> <setting name="cacheEnabled" value="false" /> </settings> </configuration>
7、配置每个mapper的映射文件,路径:WEB-INF/mybatis/mappers/xxxMapper.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="cn.jiaming.chen.dao.mapper.userMapper"> <select id="getUserById" parameterType="int" resultType="cn.jiaming.chen.dao.entity.User"> select id,name,age,location,birthday,garally,departmentId,logTime from user where id = #{id} </select> <!--获取最后插入的id--> <insert id="addUser" useGeneratedKeys="true" keyProperty="id" parameterType="cn.jiaming.chen.dao.entity.User"> insert into user <trim prefix="(" suffix=")" suffixOverrides="," > <if test="departmentId != null">departmentId,</if> <if test="name != null">name,</if> <if test="age != null">age,</if> <if test="location != null">location,</if> <if test="garally != null">garally,</if> <if test="birthday != null">birthday,</if> <if test="logTime != null">logTime,</if> </trim> values <trim prefix="(" suffix=")" suffixOverrides=","> <if test="departmentId != null">#{departmentId,jdbcType=SMALLINT},</if> <if test="name != null">#{name,jdbcType=VARCHAR},</if> <if test="age != null">#{age,jdbcType=TINYINT},</if> <if test="location != null">#{location,jdbcType=VARCHAR},</if> <if test="garally != null">#{garally,jdbcType=DECIMAL},</if> <if test="birthday != null">#{birthday,jdbcType=DATE},</if> <if test="logTime != null">#{logTime,jdbcType=TIMESTAMP},</if> </trim> </insert> </mapper>
8、配置数据库的配置文件:main/resources/dataSource.properties
datasource.driverClassName = com.mysql.jdbc.Driver datasource.url = jdbc:mysql://xxxx:3306/ssmdemo?characterEncoding=utf-8 datasource.username = xxx datasource.password = xxx #c3p0.acquireIncrement=3 #c3p0.initialPoolSize=3 #c3p0.idleConnectionTestPeriod=60 #c3p0.minPoolSize=5 #c3p0.maxPoolSize=100 #c3p0.maxStatements=100 #c3p0.numHelperThreads=10 #c3p0.maxIdleTime=60
9、为ssm框架配置maven的启动jetty和编译打包configuration

posted on 2018-06-04 20:20 javaGreenHand。。。 阅读(50) 评论(0) 收藏 举报
浙公网安备 33010602011771号