Maven + SSM environment to build

Maven + SSM

Before Maven + SSM is shining built, I wanted to write something set up when the discovery process is not clear.

So it took time to build a side edge organize your thoughts, and the build process is recorded.

It seems Zhongjue video, still need hands-on experience, memories stroke along the ideas will be profound.

Maven build related projects and modules

  • First, create a parent project and select Maven Project option.

  • Create a simple project on the check, and then click next

  • Note the parent project are packaged as pom

  • Click finish, the parent project is created.

  • Move the mouse to create a good demo-parent before, click the right mouse button and select Maven-> New Maven Module Project.

    (You can also create a direct Maven Module and then specify the parent.)

  • Module named demo-dao, Parent Project for the demo-parent.

  • Note are packaged as jar, you can click finish, demo-dao module is now created.

  • Create a Service Module in the same way, a packaging note also jar.

  • Next, create demo-web

  • Note here that are packaged for the war

  • After creating demo-web will find the error at this time because packaged as war, because the standard web application structure,

    The WEB-INF folder and web.xml file is missing under our webapp directory structure.

  • Next we create a WEB-INF folder, and then create an empty web.xml file.

web.xml shown as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>demo-web</display-name>
  <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>
  • Next, create demo-pojo, store all pojo. (Also select create a simple project)

  • The same way as packaged jar

Dependencies

  • First edit demo-parent in pom.xml

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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.demo</groupId>
  <artifactId>demo-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <modules>
    <module>demo-dao</module>
    <module>demo-service</module>
    <module>demo-web</module>
    <module>demo-pojo</module>
  </modules>
  
    <!-- 集中定义依赖版本号 -->
    <properties>
        <junit.version>4.12</junit.version>
        <spring.version>4.2.4.RELEASE</spring.version>
        <pagehelper.version>4.0.0</pagehelper.version>
        <servlet-api.version>2.5</servlet-api.version>  
        <mybatis.version>3.2.8</mybatis.version>
        <mybatis.spring.version>1.2.2</mybatis.spring.version>
        <mybatis.paginator.version>1.2.15</mybatis.paginator.version>
        <mysql.version>8.0.11</mysql.version>       
        <druid.version>1.0.15</druid.version>       
    </properties>
    
    <dependencyManagement>
        <dependencies>  
            <!-- Spring -->
            <dependency>
                <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>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jms</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>    
                <version>${spring.version}</version> 
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version> 
            </dependency>
                
            <!-- Mybatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>${mybatis.spring.version}</version>
            </dependency>
            <dependency>
                <groupId>com.github.miemiedev</groupId>
                <artifactId>mybatis-paginator</artifactId>
                <version>${mybatis.paginator.version}</version>
            </dependency>       
            <!-- MySql -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
            <!-- 连接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>       
            <dependency>
                <groupId>org.csource.fastdfs</groupId>
                <artifactId>fastdfs</artifactId>
                <version>1.2</version>
            </dependency>
    </dependencies>
    </dependencyManagement>
    
    <build>
        <plugins>           
            <!-- java编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Dao

  • Com.demo.mapper package created in the following demo-dao src / main / java and src / main / resoures.

  • Edit demo-dao's 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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.demo</groupId>
    <artifactId>demo-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>demo-dao</artifactId>
  
   <dependencies>
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>4.0.0</version>
    </dependency>   
     <!-- Mybatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>            
    </dependency>
        <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>                 
    </dependency>
    <dependency>
        <groupId>com.github.miemiedev</groupId>
        <artifactId>mybatis-paginator</artifactId>          
    </dependency>       
    <!-- MySql -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>      
    </dependency>
    <!-- 连接池 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>      
    </dependency>       
    <!-- Spring依赖 -->
    <!-- 1.Spring核心依赖 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>       
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>      
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>       
    </dependency>
    <!-- 2.Spring dao依赖 -->
    <!-- spring-jdbc包括了一些如jdbcTemplate的工具类 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>        
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>     
    </dependency>
    <!-- 3.Spring web依赖 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
    <!-- 4.Spring test依赖:方便做单元测试和集成测试 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
    </dependency>
    <!-- junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- 依赖deme-pojo -->
    <dependency>
        <groupId>com.demo</groupId>
        <artifactId>demo-pojo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

dao need to pojo so dependent pojo.

  • Create a database named demo in the database, and create a table tb_user

CREATE TABLE `tb_user` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  `createtime` datetime DEFAULT NULL,
  `verification` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT '0' COMMENT '0代表未验证,1代表已验证。',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
  • Next, create pojo class, create a package at com.demo.pojo demo-pojo, in accordance with User class and creating table structure.
package com.demo.pojo;

import java.util.Date;

/**
 * @author 依风
 *  
 */
public class User {
    //id主键自增,故无set方法,且不传入构造器。
    private Integer id;
    //用户名及密码
    private String userName;
    private String password;
    //创建时间
    private Date createTime;
    //是否验证,未验证为"0",验证为"1"
    private String verification;
    
    public User() {
        super();
        // TODO Auto-generated constructor stub
    }

    public User(String userName, String password, Date createTime, String verification) {
        super();
        this.userName = userName;
        this.password = password;
        this.createTime = createTime;
        this.verification = verification;
    }

    public Integer getId() {
        return id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Date getCreateTime() {
        return createTime;
    }

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

    public String getVerification() {
        return verification;
    }

    public void setVerification(String verification) {
        this.verification = verification;
    }
}
  • Next we edit the interface file and the corresponding xml file in demo-dao.

    Configuration as shown below:

TbUserMapper.java

package com.demo.mapper;

import com.demo.pojo.User;

/**
 * @author 依风
 * tb_user表的dao操作
 */
public interface TbUserMapper {
    
    /** 向tb_user表中插入一条记录
     * @param user 插入记录
     * @return 受影响的行数
     */
    int insertUser(User user);
}

TbUserMapper.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">

<!-- 注意此处是包名+接口名,xml文件名和接口文件名要一致 -->    
<mapper namespace = "com.demo.mapper.TbUserMapper" >
    <!-- 根据ID查询客户信息 -->
    <!-- id要和接口文件中的方法名一致 -->
    <insert id = "insertUser" parameterType = "com.demo.pojo.User" >
        INSERT INTO tb_user(name,password,createtime,verification) 
        VALUES (#{userName}, #{password}, #{createTime},#{verification}) 
        <!-- 注意参数名要和类中参数名保持一致,否则可能无法获取参数-->
    </insert>
</mapper>
  • Next we want to create in the demo-dao in several folders, and create the corresponding file:

db.properties store driver information database, the connection address, user name, password and so on.

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/demo?characterEncoding=utf-8
jdbc.username=xxxx
jdbc.password=xxxx

Note here that jdbc.driver = com.mysql.cj.jdbc.Driver, different versions of the wording is inconsistent, there is no cj older version.

Fill in the username and password for your password on it.

paalicationContext-dao.xml srping configuration information to the database.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <!-- 数据库连接池 -->
    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath*:properties/*.properties" />
    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        destroy-method="close">
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="maxActive" value="10" />
        <property name="minIdle" value="5" />
    </bean>
    
    <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据库连接池 --> 
        <property name="dataSource" ref="dataSource" />
        <!-- 加载mybatis的全局配置文件 -->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
    </bean>
    <!-- 基于MapperScannerConfigurer,会自动扫描指定包下的接口文件和映射文件 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.demo.mapper" />
    </bean>
</beans>

SqlMapConfig.xml equipped with a pagination tool.

<?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>
    <plugins>
        <!-- com.github.pagehelper 为 PageHelper 类所在包名 -->
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL 六种数据库-->
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>
</configuration>
  • Next, we tested there is no problem at dao

    Create a demo-dao com.demo.dao.test src in the / test / java lower. And create a class DaoTest.java

DaoTest.java

package com.demo.dao.test;

import java.util.Date;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.demo.mapper.TbUserMapper;
import com.demo.pojo.User;

@RunWith(SpringJUnit4ClassRunner.class)
//加载classpath路径下(即src/main/resource目录下)
//srping文件夹中所有已application开头的文件,*代表任意字符。
@ContextConfiguration(locations="classpath:spring/application*.xml")
public class DaoTest {
    
    @Autowired
    private TbUserMapper tbUserMapper;
    
    @Test
    public void insertUserTest() {
        tbUserMapper.insertUser(new User("hcf","123",new Date(),"0"));
    }
    
}

@ContextConfiguration (locations = "classpath: spring / application * .xml") to load the specified xml file, xml file in the main

Configuration data source, matching up interfaces and mapping files. Since the module packaging method for the jar, so the test is required to load xml file.

If this is war, then start tomcat and configure load the specified file in web.xml to (tomcat will automatically load web.xml).

Then click on insertUserTest method name, the right mouse button and choose Run as-> JUnit Test can be.

Nothing else should run a successful, if unsuccessful trying to find the error message bug.

After running a successful record in the database will appear.

Service

After Dao no problem, we have to build the Service.

First edit demo-service in 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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.demo</groupId>
    <artifactId>demo-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>demo-service</artifactId>
  
  <dependencies>
    <!-- Spring依赖 -->
    <!-- 1.Spring核心依赖 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>       
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>        
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>       
    </dependency>
    <!-- 2.Spring dao依赖 -->
    <!-- spring-jdbc包括了一些如jdbcTemplate的工具类 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>       
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>        
    </dependency>
    <!-- 3.Spring web依赖 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>       
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>      
    </dependency>
    <!-- 4.Spring test依赖:方便做单元测试和集成测试 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>       
    </dependency>
    <!-- junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
    <!-- dao  -->
    <dependency>
        <groupId>com.demo</groupId>
        <artifactId>demo-dao</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

Spring is mainly dependent on the above related, note that Service need to reference Dao, it is necessary to rely Dao.

  • Next we create a com.demo.service package in the demo-service, and write UserService class.

UserService.java

package com.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo.mapper.TbUserMapper;
import com.demo.pojo.User;

@Service
public class UserService {
    
    @Autowired
    private TbUserMapper tbUserMapper;
    
    //插入一条数据
    public void insert(User user) {
        tbUserMapper.insertUser(user);
    }
}
  • Next we need to create a folder in the spring under src / main / resources directory.

    Inside there are two required files.

One is applicationContext-dao.xml, is a applicationContext.xml

applicationContext-dao.xml with applicationContext-dao.xml demo-dao the same, can be copied directly.

We look at applicationContext.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.demo.*"></context:component-scan>
</beans>

We can see there is only one scan configuration package, to scan all packages beginning com.demo, * represents any.

This position is primarily configured to scan specified package annotations, such as scanning UserServcie.java in @ Autowired, @ Service notes.

  • The next test under Service

Create a package com.demo.service.test src / test / java, and then create UserServiceTest.java class.

package com.demo.service.test;

import java.util.Date;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.demo.pojo.User;
import com.demo.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/application*.xml")
public class UserServiceTest {
    
    @Autowired
    private UserService userService;
    
    @Test
    public void insertUser() {
        userService.insert(new User("hcf","321",new Date(),"0"));
    }
}

Note the locations = "classpath: spring / application * .xml", Service call Dao, Dao also need to load it, load the finished Dao

Service is injected in the @Autowired to Servcie, Service and then call the Dao. Dao does not automatically load your own, you need to specify the external load.

Then the mouse on the method name, right-click Run as-> JUnit Test.

Then run successfully, the database will record one more.

Controller

Dao and Servcie remaining after the completion of the final Conttroller. Controller need to reference Servcie, and Servcie references Dao, Controller will be indirect reference Dao.

First look at the pom.xml demo-web

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.demo</groupId>
    <artifactId>demo-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>controller</artifactId>
  <packaging>war</packaging>
  
  <dependencies>
    <!-- Spring依赖 -->
    <!-- 1.Spring核心依赖 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <!-- 2.Spring dao依赖 -->
    <!-- spring-jdbc包括了一些如jdbcTemplate的工具类 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
    </dependency>
    <!-- 3.Spring web依赖 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
    <!-- 4.Spring test依赖:方便做单元测试和集成测试 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
    </dependency>
        
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.28</version>
    </dependency>
    <dependency>
        <groupId>javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.11.0.GA</version>
    </dependency>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.10</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>      
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>com.demo</groupId>
        <artifactId>demo-service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
  
  <build>  
      <plugins>
          <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <!-- 指定端口 -->
                    <port>8080</port>
                    <!-- 请求路径 -->
                    <path>/</path>
                </configuration>
          </plugin>
      </plugins>  
    </build>
</project>

pom.xml last configure a tomcat plugin, follow the plugin to run, the equivalent of the web running in the tomcat.

  • Com.demo.controller create a package under src / main / java, and then create UserController class.

UserController

package com.demo.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.demo.pojo.User;
import com.demo.service.UserService;

/** 用户注册,登录控制类
 * @author 依风
 *
 */
//Controller+ReponseBody = RestController
@RestController
public class UserController {
    @Autowired
    private UserService userServcie;
    
    @RequestMapping("/insertUser")
    public String insertUser() {
        userServcie.insert(new User("controller","321",new Date(),"0"));
        return "insert success";
    }
}

Then we create two folders under src / main / resources, srping and config

application.properties the config is an empty folder, there will be no need to, just setting up the first basic shelf.

spring folder applicationContext-dao.xml and applicationContext.xml and demo-servcie in the same, you can copy directly.

We look at the contents of applicationContext-mvc.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
   
    <!-- 扫描包下注解 -->
    <context:component-scan base-package="com.demo.*"></context:component-scan>
    <!-- 加载配置文件-->
    <context:property-placeholder location="classpath:config/application.properties" />
    
    <!--fastjson-->
    <mvc:annotation-driven>
      <mvc:message-converters register-defaults="true">
        <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">  
          <property name="supportedMediaTypes" value="application/json"/>
          <property name="features">
            <array>
              <value>WriteMapNullValue</value>
              <value>WriteDateUseDateFormat</value>
            </array>
          </property>
        </bean>
      </mvc:message-converters>
    </mvc:annotation-driven>
</beans>

This is the main configuration inside fastjson, it returns the controller class data into json string.

We look at the web.xml under the demo-web WEB-INF directory

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <welcome-file-list>
    <welcome-file></welcome-file>
  </welcome-file-list>
  
  <!-- 过滤器统一编码 -->
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- 配置springmvc -->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <!-- 加载src/main/resources下srping文件夹中所有application开头的xml文件 -->
      <param-value>classpath:spring/application*.xml</param-value>
    </init-param>
  </servlet>
  <!-- 拦截所有.do结尾的请求 -->
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

When tomcat will start loading web.xml, while in the web.xml configuration of Spring MVC intercept requests are processed handed over to the front controller (DispatcherServlet) process, and it will load the specified configuration file.

Right-click demo-web, Run as -> Maven build.

Then pop up page, and Goals field, enter tomcat7: run

This plug-in configuration pom.xml call tomca the next demo-web, this time demo-web on the run in the tomcat.

Then we come under test in the address bar http: // localhost: 8080 / insertUser.do

Guess you like

Origin www.cnblogs.com/huang-changfan/p/11517302.html