A course of constructing SSM frame--spring+springmvc+mybatis

Source: Internet
Author: User
Tags aop log4j
A course of constructing SSM frame--spring+springmvc+mybatis

One: Overview
The SSM framework is often used in project development, and it is used more extensively in only a few years of development than the SSH framework. Spring, as a lightweight framework, has a lot of expansion capabilities, and the main thing we use for our general projects is IOC and AOP. SPRINGMVC is a web layer of spring implementation, which is equivalent to the framework of struts, but is more flexible and powerful than struts. MyBatis is a persistence-layer framework that is more flexible to use than Hibernate, can control SQL authoring, and uses XML or annotations for related configuration.

According to the above description, learning the SSM framework is very important.

II: The process of building a SSM using MAVEN to manage projects
Using MAVEN to create a WebApp project in Eclipse, the specific creation process is not demonstrated, such as create a project that will not be created
You can also create using the MAVEN command, enter the specified directory in the DOS window, and execute the following command:

MVN Archetype:create-dgroupid=org.ssm.dufy-dartifactid=ssm-demo-darchetypeartifactid=maven-archetype-webapp- Dinteractivemode=false

Use commands to be aware that the system has MAVEN installed and that environment variables are configured. [MAVEN Installation and environment variable configuration]

Import project (named creation), add dependencies
The import project is in the IDE or created directly in the IDE, typically by default "Src/main/java" and manually creating the Src/test/resources, Src/test/java folder.

The following project structure:

Then directly configure the package dependencies in the Pom.xml file.

<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>org.dufy</groupId> <artifactId>ssm</artifactId> & Lt;packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>ssmdemo</name > <url>http://maven.apache.org</url> <properties> <spring.version>4.0.5.release</sp Ring.version> <mybatis.version>3.2.1</mybatis.version> <slf4j.version>1.6.6</ Slf4j.version> <log4j.version>1.2.12</log4j.version> <mysql.version>5.1.35</ Mysql.version> </properties> <dependencies> <!--add Spring dependent--> <dependency&gt
        ; <groupId>org.springframework</groupId> <artiFactid>spring-core</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactid>spring-context </artifactId> <version>${spring.version}</version> </dependency> <dependency&gt
        ;
        <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> &LT;GROUPID&G T;org.springframework</groupid> <artifactId>spring-aop</artifactId> &LT;VERSION&GT;${SPR ing.version}</version> </dependency> <dependency> <groupId>org.springframework< /groupid> <artifactId>spring-aspects</artifactId> <version>${spring.version}</vers Ion> &Lt;/dependency> <dependency> <groupId>org.springframework</groupId> <artifactid >spring-tx</artifactId> <version>${spring.version}</version> </dependency> < Dependency> <groupId>org.springframework</groupId> <artifactid>spring-jdbc</artifa ctid> <version>${spring.version}</version> </dependency> <dependency> & Lt;groupid>org.springframework</groupid> <artifactId>spring-web</artifactId> <vers 
            Ion>${spring.version}</version> </dependency> <!--spring Unit test Dependencies--> <dependency> <groupId>org.springframework</groupId> &LT;ARTIFACTID&GT;SPRING-TEST&LT;/ARTIFACTID&G
            T
 
  <version>${spring.version}</version> <scope>test</scope> </dependency> <!--Spring WEBMVC related jar--> <dependency> <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version>
            </dependency> <dependency> <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId> <version>${spring.version}</version>
        </dependency> <!--mysql driver package--> <dependency> <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!--Alibaba data source related Jar pack--> <dependency> <groupid>com. 
     Alibaba</groupid> <artifactId>druid</artifactId> <version>0.2.23</version> </dependency>   
            <!--Alibaba Fastjson format to--> <dependency> <groupId>com.alibaba</groupId>  
        <artifactId>fastjson</artifactId> <version>1.1.41</version> </dependency> <!--logback start--> <dependency> <groupid>log4j</group Id> <artifactId>log4j</artifactId> <version>${log4j.version}</version> < /dependency> <dependency> <groupId>org.slf4j</groupId> <artifactid>slf4j-ap i</artifactid> <version>${slf4j.version}</version> </dependency> <dependency&gt
        ; <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <ver sion>1.1.2</version> </dependency> <dependency> <groupid>ch.qos.logback</gro Upid> <artifactId>logback-core</artifactId> <version>1.1.2</version> </de pendency> <dependency> <groupId>org.logback-extensions</groupId> &LT;ARTIFACTID&G T;logback-ext-spring</artifactid> <version>0.1.1</version> </dependency> < !--mybatis dependent--> <dependency> <groupId>org.mybatis</groupId> <artifactid>my Batis</artifactid> <version>${mybatis.version}</version> </dependency> <!--my Batis/spring Package--> <dependency> <groupId>org.mybatis</groupId> <artifactid>m Ybatis-spring</artifactid> <version>1.2.0</version> </dependency> <!--add Servlet3 .0 Core Package--> <dependency> <groupId>javax.servlet</groupId> <art Ifactid>javax.servlet-api</artifactid> <version>3.0.1</version> </dependency> &LT;DEP Endency> <groupId>javax.servlet.jsp</groupId> &LT;ARTIFACTID&GT;JAVAX.SERVLET.J Sp-api</artifactid> <version>2.3.2-b01</version> </dependency> & lt;! --Jstl--> <dependency> <groupId>javax.servlet</groupId> <a rtifactid>jstl</artifactid> <version>1.2</version> </dependency> &L t;! --unit testing relies on--> <dependency> <groupId>junit</groupId> <artifactid>junit</artifa ctid> <version>3.8.1</version> <scope>test</scope> </dependency> </d
 ependencies> <build> <finalName>ssmDemo</finalName> </build> </project>
Creating databases and tables, generating code
Create a database I refer to other people's blog database design, this piece does not write their own, directly add code
DROP TABLE IF EXISTS ' user_t ';  
  
CREATE TABLE ' user_t ' (  
  ' id ' int (one) not null auto_increment,  
  ' user_name ' varchar (+) NOT null,  
  ' password ' VA Rchar (255) NOT NULL,  
  "age" int (4) NOT NULL,  
  PRIMARY KEY (' id ')  
) engine=innodb auto_increment=2 DEFAULT CHARS Et=utf8;  
  
/*data for the table ' user_t '  
  
/INSERT INTO  ' user_t ' (' IDs ', ' user_name ', ' password ', ' age ') VALUES (1, ' Test ', ' Sfasgfaf ', 24)

To generate code, see:

[MyBatis generate code automatically]

Generated code import picture explanation:

Spring and mybatis consolidation, connecting databases, JUnit testing
Copy the generated code into your project, and then consolidate the spring and mybatis to add a configuration file.

Mainly has
Related configuration of applicationContent.xml:Spring.
Spring-mhbatis.xml:spring and MyBatis integrated configuration.
Jdbc.properties: Database information configuration.
Logback.xml: Log output information configuration. (Do not introduce, detailed information to view the source )

Mainly introduced Applicationcontext.xml, Spring-mhbatis.xml, Jdbc.properties. The main contents are as follows:

jdbc.properties

Jdbc_driverclassname =com.mysql.jdbc.driver
jdbc_url=jdbc:mysql://localhost:3306/ssm?useunicode=true& Characterencoding=utf8
jdbc_username=root
jdbc_password=root

Applicationcontext.xml

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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:tx= "Http://www.springframework.org/schema/tx" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-3.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.0.xsd "> <!--1. Configure the JDBC file--> <bean id=" Propertyconfigurer class= "Org.springframework.bean S.factory.config.propertyplaceholderconfigurer "> <property name=" Locations "value=" Classpath:jdbc.propErties "/> </bean> <!--2. Scan the package path, this does not scan the @controller annotation class--><!--use <context:component-scan/> Can not configure <context:annotation-config/>--> <context:component-scan base-package= "Org.ssm.dufy" > Context:exclude-filter type= "Annotation" expression= "Org.springframework.stereotype.Controller"/> </ context:component-scan> <import resource= "Classpath:spring-mybatis.xml"/> </beans>

Spring-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:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "
    xmlns:tx=" Http://www.springframework.org/schema/tx "xmlns:context=" http://www.springframework.org/schema/ Context "
    xsi:schemalocation=" Http://www.springframework.org/schema/beans http://www.springframework.org/ Schema/beans/spring-beans-3.2.xsd
   HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/ Schema/aop/spring-aop-3.2.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/ Schema/tx/spring-tx-3.2.xsd
   Http://www.springframework.org/schema/context http://www.springframework.org/ Schema/context/spring-context-3.2.xsd ">

<!--3. Configure the data source, use the Alibba database-->
     <bean id=" DataSource " Class= "Com.alibaba.druid.pool.DruidDataSou

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.