小程序开发之后台SSM环境搭建(一)

1、新建web项目

打开eclipse,选择file-->New-->Dynamic web Project ,填写项目名字,一直点击next,勾选Generate web.xml deployment descriptor,点击Finish即可。

2、引入SSM所需jar包

链接:https://pan.baidu.com/s/1h0i79ieER5K-s1YoD55xzg 密码:npze

3、新建项目目录结构

项目目录结构.png

4、SSM配置文件

4-1、jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/luolan?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
4-2、applicationContext-dao.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:p="http://www.springframework.org/schema/p"  
       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-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 
      <!-- 引入jdbc配置文件 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	<!-- 配置数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
	</bean>
	<!-- 配置sqlSessionFactory spring mybatis整合-->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 注入数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 扫描mybatis核心配置文件 -->
 		<property name="configLocation" value="classpath:mybatis.xml"></property> 
		<!-- 自动使用别名 -->
		<property name="typeAliasesPackage" value="com.luolan.entity"></property> 
		<!-- 扫描mapper映射文件 -->
		<property name="mapperLocations" value="classpath:com/luolan/mapping/*.xml"></property>
	</bean>
	<!-- 扫描DAO接口包 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <!-- 扫描DAO接口包 -->
        <property name="basePackage" value="com.luolan.dao"></property>
    </bean>
</beans>
4-3、applicationContext-web.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"
	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">
	<!-- 开启注解映射的支持 -->
	<mvc:annotation-driven />
	<!-- 允许对静态资源的访问 -->
	<mvc:default-servlet-handler/>
	<!-- 配置视图解析器 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />  
        <property name="suffix" value=".jsp" />  
	</bean>
	<!-- 设置包扫描注解 -->
	<context:component-scan base-package="com.luolan.controller"></context:component-scan>
	<!-- 开启注解 -->
	<context:annotation-config></context:annotation-config>
</beans>
4-4、applicationContext-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"
	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="com.luolan.serviceImpl"></context:component-scan>
	<!-- 配置事物管理器 -->
	<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 事物采用注解的方式进行 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
4-5、mybatis.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" />
		<!-- 使用列别名替换列名 -->
		<setting name="useColumnLabel" value="true" />
		<!-- 开启驼峰命名法则 -->
		<setting name="mapUnderscoreToCamelCase" value="true" />
	</settings>
</configuration>
4-6、web.xml
    <filter>  
        <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  
    
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:config/applicationContext-*.xml</param-value>  
    </context-param> 
    <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:config/applicationContext-web.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>  

5、测试

TestController.java
@Controller
@RequestMapping(value="/test")
public class TestController {
	@ResponseBody
	@RequestMapping(value="/index",method = RequestMethod.GET)
	public Integer test(){
		return 1;
	}
}

运行结果如图所示:执行成功
运行结果.png
至此,SSM框架搭建成功。

posted @ 2018-06-15 21:49  冰小禾  阅读(689)  评论(0编辑  收藏  举报