ssm框架整合项目完整案例

ssm框架整合项目完整案例

需求:数据库中朝代列表与皇帝列表各一张,一个朝代对应多个皇上,一个皇上对应一个朝代,实现朝代与皇上的CRUD,可以通过朝代的id查询皇上,通过皇上的id查询朝代,通过关键字、状态值和发布时间查询符合条件的记录,并将查询的结果在网页上进行显示。

第一天:基础环境配置

spring+mybatis+spring-mvc的基础配置

1、创建动态web项目并发布到tomcat

注意配置tomcat
添加junit log4j的jar包和配置文件
测试项目是否正常

2、目录规划

不适用maven的情况下

|->src_config
|->src_common
|->src_demo
|->src_head
|->src_back
|->WEB-INF
|->|->jsp
|->|->|->back
|->|->|->head
|->|->lib
|->|->web.xml

3、添加spring

3.1 添加jar包

aspectjweaver-1.9.4.jar
hamcrest-core-1.3.jar
junit-4.12.jar
log4j-api-2.12.1.jar
log4j-core-2.12.1.jar
spring-aop-5.2.1.RELEASE.jar
spring-aspects-5.2.1.RELEASE.jar
spring-beans-5.2.1.RELEASE.jar
spring-context-5.2.1.RELEASE.jar
spring-context-support-5.2.1.RELEASE.jar
spring-core-5.2.1.RELEASE.jar
spring-expression-5.2.1.RELEASE.jar
spring-instrument-5.2.1.RELEASE.jar
spring-jcl-5.2.1.RELEASE.jar
spring-jdbc-5.2.1.RELEASE.jar
spring-jms-5.2.1.RELEASE.jar
spring-orm-5.2.1.RELEASE.jar
spring-tx-5.2.1.RELEASE.jar
spring-web-5.2.1.RELEASE.jar
spring-webmvc-5.2.1.RELEASE.jar

3.2 配置文件

applicationContest_common.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:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd"
	default-autowire="byName">
	<!-- default-autowire="byName" 自动装备  配置起来 -->
	<!-- 使用注解  项目都是用注解 -->	
	<context:component-scan base-package="com.jinghang"/>
</beans>

3.3 测试代码

BataTest

package com.jinghang.ssm.common.test;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jinghang.ssm.common.util.ConstatFinalUtil;

public class BaseTest {
	/**
	 * 所有方法都可以使用此属性
	 * 包括子类
	 */
	protected ApplicationContext ac;
	/**
	 * 初始化   读取配置文件
	 */
	@Before
	public void init() {

		this.ac = new ClassPathXmlApplicationContext("classpath*:spring/applicationContext_*.xml");

		ConstatFinalUtil.SYSTEM_LOGGER.info("==init=初始化====");
	}
	/**
	 * 测试代码
	 */
	@Test
	public void test() {

		ConstatFinalUtil.SYSTEM_LOGGER.info("==test=测试====");
	}
	/**
	 * 关闭
	 */
	@After
	public void close() {

		if (this.ac instanceof ClassPathXmlApplicationContext) {
			ClassPathXmlApplicationContext cpxa = (ClassPathXmlApplicationContext) this.ac;
			cpxa.close();
			cpxa = null;
		}

		ConstatFinalUtil.SYSTEM_LOGGER.info("==close=关闭====");
	}

}

4、添加mybatis

4.1 添加jar包

ant-1.10.3.jar
ant-launcher-1.10.3.jar
asm-7.0.jar
cglib-3.2.10.jar
hamcrest-core-1.3.jar
javassist-3.24.1-GA.jar
junit-4.12.jar
log4j-api-2.12.1.jar
log4j-core-2.12.1.jar
mybatis-3.5.3.jar
mysql-connector-java-8.0.18.jar
ognl-3.2.10.jar

4.2 配置文件

Mybatis.cfg.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>
	<!-- mybatis环境配置 -->
	<properties resource="jdbc.properties"></properties>
	<environments default="my">
		<environment id="my">
			<transactionManager type="JDBC"/>
			<dataSource type="POOLED">
				<property name="driver" value="${jdbc.driver}"/>
				<property name="url" value="${jdbc.url}"/>
				<property name="username" value="${jdbc.username}"/>
				<property name="password" value="${jdbc.password}"/>
			</dataSource>
		</environment>
	</environments>

</configuration>

4.3 mybatis和spring整合不需要测试代码,但是需要引入一个jar包 >>mybatis-spring-2.0.3.jar<< 和一个配置文件

applicationContext_mybatis.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:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd"
	default-autowire="byName">
	<!-- default-autowire="byName" 自动装备  配置起来 -->
	
	<!-- 在配置文件的时候 区分大小写 属性名字、包名加类名、方法名能复制就不要手打  防止打错 -->
	
	<!-- 第三步、配置属性文件的位置 -->
	<context:property-placeholder location="classpath:jdbc.properties" />
	
	<!-- 
	 	第二步、如果mybatis整合spring  
		dataSource数据源的配置必须在添加的applicationContext_mybatis.xml文件中配置
		在mybatis.cfg.xml中配置不管用
		
	 	在mybatis整合spring时  必须配置spring-jdbc中的
	 	数据源配置的类  org.springframework.jdbc.datasource.DriverManagerDataSource
	-->
	<!-- id必须是datasource   default-autowire="byName" 默认按照名字进行装配 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<!-- 想要使用${jdbc.driver} 必须配置属性文件  必须进行第三步 -->
		<property name="driverClassName" value="${jdbc.driver}"/>
		<property name="url" value="${jdbc.url}"/>
		<property name="username" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>
	
	<!-- 	
		第一步、整合mybatis和spring需要将引入的jar包中的核心类配置一下
		org.mybatis.spring.SqlSessionFactoryBean 
	-->
	<bean id="aa" class="org.mybatis.spring.SqlSessionFactoryBean" >
		<!-- 配置文件的关联 -->
		<property name="configLocation" value="classpath:mybatis.cfg.xml"></property>
		<!-- 映射文件的关联 -->
		<property name="mapperLocations" value="classpath*:com/jinghang/MySpring_SSM/*/pojo/*Mapper.xml"></property>
	</bean>
	
</beans>

需要注意的是:当spring整合mybatis后 数据源的配置必须在新添加的applicationContext_mybatis.xml文件中进行配置,原mybatis.cfg.xml文件配置的数据源不起作用

5、添加spring-mvc

5.1、添加jar包

和spring的jar包一样,不需要重复导入,但是需要引入另外2个jar包
===>>>taglibs-standard-impl-1.2.5.jar
===>>>taglibs-standard-spec-1.2.5.jar

5.2、配置web.xml文件

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
id="WebApp_ID" version="3.1">
	<display-name>MySpring_SSM</display-name>

	<servlet>
		<servlet-name>DispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath*:spring/applicationContext_*.xml</param-value>
		</init-param>
		
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>DispatcherServlet</servlet-name>
		<url-pattern>*.htm</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

6、至此为止,spring+mybatis+spring-mvc框架(ssm)整合的基础配置完成,可以进行各种测试

7、ssm整合后的总结

  • 前期配置到此结束,后面的内容定时更新

你可能感兴趣的:(ssm框架整合项目完整案例)