SSM框架整合---附Spring配置文件和Mybatis配置文件以及一大堆jar包

一、Spring整合javaWeb

       配置文件中配置出一个监听器充当Spring容器

		<context-param>
	        <param-name>contextConfigLocation</param-name>
	        <param-value>/WEB-INF/applicationContext.xml</param-value>
	    </context-param>
	    <listener>
	        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	    </listener>

       Servlet不能用注解注入,因为Servlet对象创建是交给Tomcat的,和IOC容器中的对象不是同个概念,因此不能直接用Controller注解注入。故成员变量Service也不能自动装配,只能用容器.getBean(class)方法获得。

	WebApplicationContext ioc = ContextLoader.getCurrentWebApplicationContext();	//在Web应用程序启动时载入Ioc容器
    Object object = ioc.getBean(class);
	




二、SpringMVC整合Spring

       Spring是父容器,SpringMVC是子容器。
       两个容器各司其职,SpringMVC的容器就负责创建标注Controller和ControllerAdvice的组件。Spring容器负责创建其他的组件。

       web.xml配置:

		<!--    配置Spring容器-->
	    <context-param>
	        <param-name>contextConfigLocation</param-name>
	        <param-value>/WEB-INF/applicationContext.xml</param-value>
	    </context-param>
	    <listener>
	        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	    </listener>
	    <servlet>
	<!--        默认将dispatcher-servlet.xml作为SpringMVC的配置文件-->
	        <servlet-name>dispatcher</servlet-name>
	        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	        <load-on-startup>1</load-on-startup>
	    </servlet>
	    <servlet-mapping>
	        <servlet-name>dispatcher</servlet-name>
	        <url-pattern>/</url-pattern>
	    </servlet-mapping>

       Spring容器配置文件(事务控制、数据源等等):

	<!--    不配置Controller和ControllerAdvice-->
	    <context:component-scan base-package="com.cj">
	        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
	    </context:component-scan>
	
	    <!--    配置数据源-->
	    <context:property-placeholder location="classpath:dbcp.properties"></context:property-placeholder>
	    <bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource">
	        <property name="username" value="${jdbc.username}"></property>
	        <property name="password" value="${jdbc.password}"></property>
	        <property name="url" value="${jdbc.url}"></property>
	        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
	        <!--        <property name="minIdle" value="5"></property>-->
	        <!--        <property name="maxIdle" value="20"></property>-->
	    </bean>

       SpringMVC容器配置文件(视图解析器、数据校验、文件上传等等):

		<context:component-scan base-package="com.cj" use-default-filters="false">
	        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
	    </context:component-scan>




三、MyBatis整合Spring、SpringMVC

       导入mybatis-spring-2.0.3.jar包。

       Spring配置文件中配置数据源,交给Mybatis,之前已经将除了带有Controller和ControllerAdvice注解的组件加到容器中了。Spring可以方便地指定映射配置文件的位置。将接口的实现类加入到容器中后,就可以用注解自动装配了。

<!--    配置数据源-->
    <context:property-placeholder location="classpath:dbcp.properties"></context:property-placeholder>
    <bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource">
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <!--        <property name="minIdle" value="5"></property>-->
        <!--        <property name="maxIdle" value="20"></property>-->
    </bean>
    
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
        <property name="dataSource" ref="dataSource"></property>
<!--        指定mapper配置文件-->
        <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property>
    </bean>
    
<!--    把dao包下所有接口的实现类加入到IOC容器-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.cj.dao"></property>
    </bean>

Spring5.0已经不支持Log4j了,需要改用Log4j2,这个是前面的升级版。


测试SSM代码

       新建log4j2.xml(名字必须是这个):

	<?xml version="1.0" encoding="UTF-8"?>
	<Configuration status="WARN">
	
	
	    <!-- Author:  Crunchify.com  -->
	    <Appenders>
	        <Console name="Console" target="SYSTEM_OUT">
	            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
	        </Console>
	        <!-- 注意%i和 %d{yyyyMMdd},这样子才能将文件删除-->
	        <RollingFile name="RollingFile" filename="/log/Convergence.log"
	                     filepattern="/log/%d{yyyyMMdd}-Convergence-%i.log">
	            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
	            <Policies>
	                <SizeBasedTriggeringPolicy size="20 MB" />
	            </Policies>
	            <DefaultRolloverStrategy max="20" >
	                <Delete basePath="/log/" maxDepth="1">
	                    <IfFileName glob="*-Convergence-*.log" />
	                    <IfLastModified age="30D" />
	                </Delete>
	            </DefaultRolloverStrategy>
	        </RollingFile>
	
	    </Appenders>
	    <Loggers>
	        <!-- name中的值为mybatis的DAO层接口包路径 -->
	        <logger name="convergence.idao" level="DEBUG" additivity="false">
	            <appender-ref ref="Console"/>
	        </logger>
	        <Root level="DEBUG">
	            <AppenderRef ref="Console" />
	            <AppenderRef ref="RollingFile" />
	        </Root>
	    </Loggers>
	</Configuration>

       javaBean:

	public class Person implements Serializable {
	    private static final long serialVersionUID = 7758287963968328945L;
	    private Integer PERSON_ID;
	    private String PERSON_NAME;
	    private Integer PERSON_GENDER;
	    private Integer PERSON_AGE;
	    ...
	}

       Dao接口:

	public interface PersonDao {
	    public Person QueryById(Integer id);
	}

       映射配置文件:

	<?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.cj.dao.PersonDao">
	    <cache/>
	    <select id="QueryById" resultType="com.cj.entity.Person">
	        select * from person where PERSON_ID=#{id}
	    </select>
	</mapper>

       testSSM.jsp:

	<%@ page contentType="text/html;charset=UTF-8" language="java" %>
	<html>
	<head>
	    <title>SSM</title>
	</head>
	<body>
	    <a href="justQuery?id=2">测试SSM整合</a>
	</body>
	</html>

       控制器:

	@Controller
	public class PersonController {
	    @Autowired
	    private PersonService personService;
	
	    @RequestMapping("/justQuery")
	    public String Query(@RequestParam("id")Integer id, Model model) {
	        Person p=personService.QueryById(id);
	        model.addAttribute("person",p);
	        return "success";
	    }
	}

       success.jsp:

	<%@ page contentType="text/html;charset=UTF-8" language="java" %>
	<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
	<html>
	<head>
	    <title>Successful</title>
	</head>
	<body>
	    ${person}
	</body>
	</html>

       SSM整合效果测试:
Alt
Alt


a href=“https://pan.baidu.com/s/17LfkbRLB3s4ufeKh0eeb-g”>需要用到的jar包

面向百度编程!

发布了33 篇原创文章 · 获赞 5 · 访问量 2274

猜你喜欢

转载自blog.csdn.net/cj1561435010/article/details/104007483
今日推荐