完整配置SSM及实践-欧克

配置SSM

#环境配置
1,开发工具 eclipse navicat
2, 环境JDK8 MAVEN TONCAT
3,数据库 mysql
可参考以下博客或自行百度
jdk安装配置
tomcat安装配置
maven安装配置
mysql安装配置
#打开eclipse创建maven项目
创建maven项目
创建完之后添加一些包,存储类以及各种配置文件,如下图
在这里插入图片描述
#开始创建数据库以及表

create table tb_area(
area_id int(2) NOT NULL AUTO_INCREMENT PRIMARY KEY,
area_name VARCHAR(200) DEFAULT NULL,
priority int(2) NOT NULL DEFAULT '0',
create_time datetime DEFAULT NULL,
last_edit_time datetime DEFAULT NULL,
UNIQUE key UK_AREA (area_name)
)ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8

#在maven项目中创建实体类

package com.imooc.o2o.entity;

import javax.xml.crypto.Data;

/**
 * 区域类
 * 
 * @author pan
 *
 */
public class Area {
	// ID
	private Integer id;
	// 名称
	private String areaName;
	// 权重
	private Integer priority;
	// 创建时间
	private Data createTime;
	// 更新时间
	private Data lastEditTime;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getAreaName() {
		return areaName;
	}

	public void setAreaName(String areaName) {
		this.areaName = areaName;
	}

	public Integer getPriority() {
		return priority;
	}

	public void setPriority(Integer priority) {
		this.priority = priority;
	}

	public Data getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Data createTime) {
		this.createTime = createTime;
	}

	public Data getLastEditTime() {
		return lastEditTime;
	}

	public void setLastEditTime(Data lastEditTime) {
		this.lastEditTime = lastEditTime;
	}

}

创建好之后开始逐层配置SSM
首先配置pom.xml

<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.imooc</groupId>
	<artifactId>o2o</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>o2o Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<properties>
		<spring.version>4.3.7.RELEASE</spring.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
	<dependency>
	    <groupId>ch.qos.logback</groupId>
	    <artifactId>logback-classic</artifactId>
	    <version>1.2.3</version>
	</dependency>
	<!-- spring -->
	<!-- 1)包含基本的核心工具类。Spring其他组件要都要使用这个包里的类,是其他组件的基本核心 -->
	<dependency>
	 	<groupId>org.springframework</groupId>
	 	<artifactId>spring-core</artifactId>
	 	<version>${spring.version}</version>
	</dependency>
	<!-- 2)这个jar文件是所有应用都要用到的,它包含访问配置文件,创建和管理bean以及进行Inversion of Cont
		/De'pendency Injection(Ioc/DI)操作相关的所有类。如果应用只需基本的IoC/DI支持,引入spring-code
		及spring-beans.jar文件就可以了。 -->
	<dependency>
	 	<groupId>org.springframework</groupId>
	 	<artifactId>spring-beans</artifactId>
	 	<version>${spring.version}</version>
	</dependency>
	<!-- 3)这个jar文件为Spring核心提供了大量扩展。可以找到使用Spring ApplicationContext特性时所需的全部类,
		instrumentation组件以及校验Validation方面的相关类 -->
	<dependency>
	 	<groupId>org.springframework</groupId>
	 	<artifactId>spring-context</artifactId>
	 	<version>${spring.version}</version>
	</dependency>
	<!-- 4)这个jar文件包含对Spring对JDBC数据访问进行封装所有类 -->
	<dependency>
	 	<groupId>org.springframework</groupId>
	 	<artifactId>spring-jdbc</artifactId>
	 	<version>${spring.version}</version>
	</dependency>
	<!-- 5)为 JDBC,Hibernate,JDO,JPA等提供的一致的声明式和编程式事务管理。 -->
	<dependency>
	 	<groupId>org.springframework</groupId>
	 	<artifactId>spring-tx</artifactId>
	 	<version>${spring.version}</version>
	</dependency>
	<!-- 6)Spring web 包含web应用开发时,用到Spring框架时所需的核心类,包括自动载入WebApplicationContext. -->
	<dependency>
	 	<groupId>org.springframework</groupId>
	 	<artifactId>spring-web</artifactId>
	 	<version>${spring.version}</version>
	</dependency>
	<!-- 7)包含SpingMVC框架相关的所有类。 -->
	<dependency>
	 	<groupId>org.springframework</groupId>
	 	<artifactId>spring-webmvc</artifactId>
	 	<version>${spring.version}</version>
	</dependency>
	<!-- 8)Spring test对JUNIT等测试框架的简单封装 -->
	<dependency>
	 	<groupId>org.springframework</groupId>
	 	<artifactId>spring-test</artifactId>
	 	<version>${spring.version}</version>
	 	<scope>test</scope>
	</dependency>
	<!-- Servlet web -->
	<dependency>
	 	<groupId>javax.servlet</groupId>
	 	<artifactId>javax.servlet-api</artifactId>
	 	<version>3.1.0</version>
	</dependency>
	<!-- json解析 -->
	<dependency>
	 	<groupId>com.fasterxml.jackson.core</groupId>
	 	<artifactId>jackson-databind</artifactId>
	 	<version>2.8.7</version>
	</dependency>
	<!-- Map工具类对标准java Collcetion 的扩展spring-core.jar需commons-collections.jar -->
	<dependency>
	 	<groupId>commons-collections</groupId>
	 	<artifactId>commons-collections</artifactId>
	 	<version>3.2</version>
	</dependency>
	<!-- DAO:MyBatis -->
	<dependency>
	 	<groupId>org.mybatis</groupId>
	 	<artifactId>mybatis</artifactId>
	 	<version>3.4.2</version>
	</dependency>
	<dependency>
	 	<groupId>org.mybatis</groupId>
	 	<artifactId>mybatis-spring</artifactId>
	 	<version>1.3.1</version>
	</dependency>
	<!-- 数据库 -->
	<dependency>
	 	<groupId>mysql</groupId>
	 	<artifactId>mysql-connector-java</artifactId>
	 	<version>8.0.19</version>
	</dependency>
	<dependency>
	 	<groupId>c3p0</groupId>
	 	<artifactId>c3p0</artifactId>
	 	<version>0.9.1.2</version>
	</dependency>
	
	</dependencies>
	<build>
		<finalName>o2o</finalName>
		<plugins>
			<plugin>
				<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF8</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

配置jdbc.properties,这个是配置MYSQL连接驱动

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sys?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
jdbc.username=root
jdbc.password=123456

配置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" />
		<!-- 打印查询语句 -->
		<setting name="logImpl" value="STDOUT_LOGGING" />
	</settings>
</configuration>

配置spring-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: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} -->
	<!-- <bean class="com.imooc.myo2o.util.EncryptPropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:jdbc.properties</value>
				<value>classpath:redis.properties</value>
			</list>
		</property>
		<property name="fileEncoding" value="UTF-8" /> 
	</bean> -->
	<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="com.imooc.entity" />
		<!-- 扫描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="com.imooc.o2o.dao" />
	</bean>
</beans>

配置spring-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.imooc.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-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-3.2.xsd">
	<!-- 配置SpringMVC -->
	<!-- 1.开启SpringMVC注解模式 -->
	<!-- 简化配置: (1)自动注册DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter 
		(2)提供一些列:数据绑定,数字和日期的format @NumberFormat, @DateTimeFormat, xml,json默认读写支持 -->
	<mvc:annotation-driven />

	<!-- 2.静态资源默认servlet配置 (1)加入对静态资源的处理:js,gif,png (2)允许使用"/"做整体映射 -->
	<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>
	<!-- 文件上传解析器 -->
	<!-- <bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="utf-8"></property>
		<property name="maxUploadSize" value="10485760000"></property>最大上传文件大小
		<property name="maxInMemorySize" value="10960"></property>
	</bean> -->
	<!-- 在spring-mvc.xml文件中加入这段配置后,spring返回给页面的都是utf-8编码了 -->
	<!-- <bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean
					class="org.springframework.http.converter.StringHttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>text/html;charset=UTF-8</value>
						</list>
					</property>
				</bean>
			</list>
		</property>
	</bean> -->
	<!-- 4.扫描web相关的bean -->
	<context:component-scan base-package="com.imooc.o2o.web" />
	<!-- 5.权限拦截器 -->
	<!-- <mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/shop/**" />
			<mvc:exclude-mapping path="/shop/ownerlogin" />
			<mvc:exclude-mapping path="/shop/ownerlogincheck" />
			<mvc:exclude-mapping path="/shop/logout" />
			<mvc:exclude-mapping path="/shop/register" />
			<bean id="ShopInterceptor" class="com.imooc.myo2o.interceptor.shop.ShopLoginInterceptor" />
		</mvc:interceptor>
		<mvc:interceptor>
			<mvc:mapping path="/shop/**" />
			<mvc:exclude-mapping path="/shop/ownerlogin" />
			<mvc:exclude-mapping path="/shop/ownerlogincheck" />
			<mvc:exclude-mapping path="/shop/register" />
			shoplist page
			<mvc:exclude-mapping path="/shop/shoplist" />
			<mvc:exclude-mapping path="/shop/logout" />
			<mvc:exclude-mapping path="/shop/list" />
			changepsw page
			<mvc:exclude-mapping path="/shop/changepsw" />
			<mvc:exclude-mapping path="/shop/changelocalpwd" />							
			ownerbind page
			<mvc:exclude-mapping path="/shop/ownerbind" />
			<mvc:exclude-mapping path="/shop/bindlocalauth" />
			shopmanage page
			<mvc:exclude-mapping path="/shop/shopmanage" />
			shopedit page
			<mvc:exclude-mapping path="/shop/shopedit" />
			<mvc:exclude-mapping path="/shop/getshopbyid" />
			<mvc:exclude-mapping path="/shop/getshopinitinfo" />
			<mvc:exclude-mapping path="/shop/registershop" />
			<bean id="ShopPermissionInterceptor"
				class="com.imooc.myo2o.interceptor.shop.ShopPermissionInterceptor" />
		</mvc:interceptor>
		<mvc:interceptor>
			<mvc:mapping path="/superadmin/**" />
			<mvc:exclude-mapping path="/superadmin/login" />
			<mvc:exclude-mapping path="/superadmin/logincheck" />
			<mvc:exclude-mapping path="/superadmin/main" />
			<mvc:exclude-mapping path="/superadmin/top" />
			<bean id="SuperAdminInterceptor"
				class="com.imooc.myo2o.interceptor.superadmin.SuperAdminLoginInterceptor" />
		</mvc:interceptor>

	</mvc:interceptors> -->
</beans>

最后配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>
<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>

配置完这些之后就已经配置完所有的配置,接下来就是验证是否配置成功
#验证首先在DAO下创建一个查询接口

package com.imooc.o2o.dao;

import java.util.List;

import com.imooc.o2o.entity.Area;

public interface AreaDao {
	/*
	 * 列出区域列表
	 * @return areaList
	 */
	List<Area> queryArea();
}

在mapper下创建一个AreaDao.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.imooc.o2o.dao.AreaDao">
	<select id="queryArea" resultType="com.imooc.o2o.entity.Area">
		SELECT area_id,area_name,priority,create_time,last_edit_time FROM
		tb_area ORDER BY priority DESC
	</select>
</mapper>

创建一个基类用来扫描spinrg配置文件

package com.imooc.o2o;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/*
 * 配置spring和junit整合,junit启动时告诉springIoc容器
 */
@RunWith(SpringJUnit4ClassRunner.class)
//告诉junit spring配置文件的位置
@ContextConfiguration({"classpath:spring/spring-dao.xml","classpath:spring/spring-service.xml"})
public class BaseTest {

}

写好之后在创建一个test类用来测试时候成功获取到数据

package com.imooc.o2o.dao;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.imooc.o2o.BaseTest;
import com.imooc.o2o.entity.Area;

public class AreaDaoTest extends BaseTest {
	@Autowired
	private AreaDao areaDao;

	@Test
	public void testQueryArea() {
		List<Area> areaList = areaDao.queryArea();
		assertEquals(2, areaList.size());
	}
}

最后鼠标右键点击Run As 选择JUnit Test,记得在表中创建几条数据否则数据为空,如果控制台打印如下图,证明配置成功
在这里插入图片描述
后续会更新验证service以及通过访问把数据展示出来

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值