ssm framework integration and small test

       The ssm framework is integrated by Spring+SpringMVC+Mybatis, and is currently studying hard. Although there are many building tutorials on the Internet, they can’t actually run very well. Maybe the development software you use is different, and the requirements for the framework It's different; I'll give an overview of how I built it myself. (from Spring3.x enterprise application development combat)

 1、Spring

      It is a one-stop lightweight open source framework for layered Java SE/EE applications, with IOC (Inverse of Control: Inversion of Control) and AOP (Aspect Oriented Programming: Aspect-Oriented Programming) as the core, providing presentation layers SpringMVC and Many enterprise-level application technologies such as persistence layer Spring JDBC and business layer transaction management;

advantage:

①Easy decoupling and simplified development

   Through the IOC container provided by Spring, we can hand over the dependencies between objects to Spring for control, avoiding excessive coupling caused by hard coding. With Spring, users no longer have to write code for very low-level requirements such as single-instance mode classes, property file parsing, etc., and can focus on upper-level applications;

②AOP programming support

    Through the AOP function provided by Spring, it is convenient for aspect-oriented programming, and many functions that are not easy to implement with traditional OOP can be easily implemented with AOP;

③ Support for declarative transactions

    In Spring, we can get rid of the monotonous and boring transaction management code, and flexibly manage transactions in a declarative way to improve development efficiency and quality;

④Convenient program testing

 

⑤Easy to integrate various excellent frameworks

 

⑥Reduce the difficulty of using JavaEE API

 

⑦Java source code is a classic learning paradigm

 

2、Spring MVC

Spring MVC separates the roles of controller, model object, dispatcher, and handler object, which makes them easier to customize.

 

3、Mybatis

MyBatis is an excellent persistence layer framework that supports plain SQL queries, stored procedures and advanced mappings. MyBatis eliminates almost all JDBC code and manual setting of parameters and retrieval of result sets. MyBatis uses simple XML or annotations for configuration and primitive mapping, mapping interfaces and Java POJOs (Plain Old Java Objects, ordinary Java objects) to records in the database.
Each MyBatis application mainly uses an instance of Sql Session Factory, and an instance of SqlSession Factory can be obtained through SqlSessionFactoryBuilder. SqlSessionFactoryBuilder can be obtained from an xml configuration file or an instance of a predefined configuration class.
Building an instance of SqlSessionFactory from an xml file is a very simple matter. It is recommended to use classpath resources in this configuration, but you can use any Reader instance, including those created with file paths or urls starting with file://. MyBatis has a utility class, Resources, which has many methods to easily load resources from the classpath and other locations.
(1) Load configuration and initialize
Trigger condition: load configuration file
Processing process: Load the SQL configuration information into MappedStatement objects (including incoming parameter mapping configuration, executed SQL statement, and result mapping configuration), and store them in memory.
(2) Receive the call request
Trigger condition: call the API provided by Mybatis
Incoming parameters: the ID of the SQL and the incoming parameter object
Processing process: The request is passed to the lower request processing layer for processing.
(3) Processing operation requests
Trigger condition: The API interface layer passes the request over
Incoming parameters: the ID of the SQL and the incoming parameter object
Process:
(A) Find the corresponding MappedStatement object according to the SQL ID.
(B) Parse the MappedStatement object according to the incoming parameter object, get the final SQL to be executed and execute the incoming parameter.
(C) Obtain a database connection, execute the database according to the obtained final SQL statement and execution parameters, and obtain the execution result.
(D) Convert the obtained execution result according to the result mapping configuration in the MappedStatement object, and obtain the final processing result.
(E) Release connection resources.
(4) Return the processing result Return the final processing result.
(Note: I searched a lot of information on the Internet and found that I am really a fool. In the future, I will break these frameworks one by one. I think that those who come from a professional school must be able to understand the principles of the framework they use, otherwise, what have they learned for so many years? ?)
Let's start to explain my configuration process. This process is very painful, because the three frameworks are directly integrated, and many books start to learn the Spring framework first, and then slowly integrate and improve according to the increase of knowledge. many things
The development tool I use is Idea;
<1> First, use Idea to create a Maven project. This creation process and the configuration of the maven environment will not be discussed here. Baidu, there are quite a lot of tutorials;
<2> Create your favorite directory structure. I am lazy. First, create a simple small directory; as shown below:


 

The directory is probably like this, the few that must be configured are written, and the filters that need to be added will be added later;

 

<3> Configure your own dao layer, which is spring-db.xml, which mainly connects to the database:

<?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
http://www.springframework.org/schema/beans ">
    <!-- 开启事务注解驱动 -->
    <context:annotation-config />
    <!-- 启动组件扫描,排除@Controller组件,该组件由SpringMVC配置文件扫描 -->
    <context:component-scan base-package="com.tao.eat">
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
    <!-- 1.配置数据库相关参数properties的属性:${url} -->
    <context:property-placeholder location="classpath:jdbc.properties" />

    <!-- 2.数据库连接池 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!-- 配置连接池属性 -->
        <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>
    <!-- 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.tao.eat.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" />
        <!-- 给出需要扫描Mapper接口包 -->
        <property name="basePackage" value="com.tao.eat.mapper" />
    </bean>



</beans>

 <4>配置service层,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" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	   xmlns:aop="http://www.springframework.org/schema/aop"
	   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 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
	<!-- 扫描service包下所有使用注解的类型 -->
	<context:component-scan base-package="com.tao.eat.service" />
	<context:component-scan base-package="com.tao.eat.controller"/>

	<!-- 配置事务管理器 -->
	<bean id="transactionManager"
		  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 注入数据库连接池 -->
		<property name="dataSource" ref="dataSource" />
	</bean>
	<aop:config proxy-target-class="true">
	<aop:pointcut id="serviceMethod" expression="execution(* com.tao.eat.service..*(..))"/>
	<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice"/>
	</aop:config>
	<tx:advice id = "txAdvice" transaction-manager="transactionManager">
	<tx:attributes>
	<tx:method name = "*"/>
	</tx:attributes>
	</tx:advice>
	<!-- 配置基于注解的声明式事务 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
	<import resource="spring-db.xml"/>
</beans>

<5>配置基础资源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.0.xsd">
	<!-- 配置SpringMVC -->
	<!-- 1.开启SpringMVC注解模式 -->
	<!-- 简化配置: 
		(1)自动注册DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter 
		(2)提供一些列:数据绑定,数字和日期的format @NumberFormat, @DateTimeFormat, xml,json默认读写支持 
	-->
	<mvc:annotation-driven />
	
	<!-- 2.静态资源默认servlet配置
		(1)加入对静态资源的处理:js,gif,png
		(2)允许使用"/"做整体映射
	 -->
	 <mvc:default-servlet-handler/>
	 
	 <!-- 3.配置jsp 显示ViewResolver -->
	 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	 	<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
	 	<property name="prefix" value="/WEB-INF/jsp/" />
	 	<property name="suffix" value=".jsp" />
	 </bean>
	 
	 <!-- 4.扫描controller相关的bean -->
	 <context:component-scan base-package="com.tao.eat.controller" />
</beans>

 <6>整合spring和mybatis,mybatis-spring.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" />
    </settings>
</configuration>

 <7>最后就是配置一下web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!-- 如果是用mvn命令生成的xml,需要修改servlet版本为3.1 -->
  <!-- 配置DispatcherServlet -->
  <servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 配置springMVC需要加载的配置文件
        spring-dao.xml,spring-service.xml,spring-web.xml
        Mybatis - > spring -> springmvc
     -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/spring-*.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <!-- 默认匹配所有的请求 -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

<8>配置测试 BaseTest.java

package com.tao.eat;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

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

}

  <9>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>ssm_eat</groupId>
  <artifactId>ssm_eat</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>ssm_eat Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <!--单元测试-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <!--日志-->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <!--数据库连接-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.36</version>
    </dependency>
    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.1</version>
    </dependency>
    <!--Mybatis-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.3.0</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.2.3</version>

    </dependency>
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.1.1</version>
    </dependency>
    <!--Servlet Web-->
    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.8.0</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>
    <!--Spring-->
    <!--Spring核心-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>3.2.13.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>3.2.13.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>3.2.13.RELEASE</version>
    </dependency>
    <!--Spring Dap层-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>3.2.13.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>3.2.13.RELEASE</version>
    </dependency>
    <!--Spring Web-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>3.2.13.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>3.2.13.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.6</version>
    </dependency>
    <!--Spring Test-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>3.2.13.RELEASE</version>
    </dependency>
    <!-- redis客户端:Jedis -->
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.7.3</version>
    </dependency>
    <dependency>
      <groupId>com.dyuproject.protostuff</groupId>
      <artifactId>protostuff-core</artifactId>
      <version>1.0.8</version>
    </dependency>
    <dependency>
      <groupId>com.dyuproject.protostuff</groupId>
      <artifactId>protostuff-runtime</artifactId>
      <version>1.0.8</version>
    </dependency>

    <!-- Map工具类 -->
    <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>3.2</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>ssm_eat</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

 

我的配置 就是这样的,具体我能看懂,但是什么都不参考也写不出来,配置就是这样的,这里面还参考了好多博主的知识,但是网页关了,就无法一一列出了。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326359822&siteId=291194637