SSM框架的搭建

  1. 以电商项目为例,创建目录:
    需要创建父目录,然后在父目录下创建子目录如下:
    (父目录为pom工程,back和front是war工程,其他为jar工程,并且创建子目录父目录右键maven下的new maven model project,其中front和back为controller层)
    在这里插入图片描述

创建完毕后需要在controller层创建WEB-INF文件夹并创建web.xml文件,如下图:

在这里插入图片描述
web.xml代码如下:

<?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_3_0.xsd" id="WebApp_ID" version="3.0">

</web-app>
  1. 引入要用的包
    (版本控制只需要再父工程上引即可)
    在父工程下的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.example</groupId>
  <artifactId>parentExample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <modules>
  	<module>daoExample</module>
  	<module>serviceExample</module>
  	<module>pojoExample</module>
  	<module>interfaceExample</module>
  	<module>backExample</module>
  	<module>frontExample</module>
  </modules>
  <properties>
  	<spring.version>4.3.14.RELEASE</spring.version>
  	<junit.version>4.12</junit.version>
  	<lombok.version>1.16.18</lombok.version>
  	<mysql.version>5.1.6</mysql.version>
  	<mybatis.version>3.4.1</mybatis.version>
  	<mybatis-spring.version>1.3.1</mybatis-spring.version>
  	<druid.version>1.0.29</druid.version>
  	<common.version>4.4.0</common.version>
  	<jstl.version>1.2</jstl.version>
  	<fastjson.version>1.2.47</fastjson.version>
  	<pagehelper.version>4.1.6</pagehelper.version>
  	<mybatis-generator.version>1.3.2</mybatis-generator.version>
  	<jedis.version>2.9.0</jedis.version>
  	<commons-fileupload.version>1.3.1</commons-fileupload.version>
  	<jersey-client.versoin>1.19</jersey-client.versoin>
  	<jersey-core.version>1.19</jersey-core.version>
  	<poi-ooxml.version>3.17</poi-ooxml.version>
  	<poi.version>3.17</poi.version>
  	<poi-ooxml-schemas.version>3.17</poi-ooxml-schemas.version> 
  	<xmlbeans.version>2.6.0</xmlbeans.version>
  	<commons-codec.version>1.6</commons-codec.version>
  	<net.sourceforge.jexcelapi.version>2.6.10</net.sourceforge.jexcelapi.version>
  </properties>

  <dependencyManagement>
	  <dependencies>
	  	<dependency>
		    <groupId>org.springframework</groupId>
		    <artifactId>spring-context</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-orm</artifactId>
		    <version>${spring.version}</version>
		</dependency>
		<dependency>
		    <groupId>org.springframework</groupId>
		    <artifactId>spring-test</artifactId>
		    <version>${spring.version}</version>
		</dependency>
		<dependency>
		    <groupId>junit</groupId>
		    <artifactId>junit</artifactId>
		    <version>${junit.version}</version>
		    <scope>test</scope>
		</dependency>
		<dependency>
		    <groupId>org.projectlombok</groupId>
		    <artifactId>lombok</artifactId>
		    <version>${lombok.version}</version>
		    <scope>provided</scope>
		</dependency>
		<dependency>
		    <groupId>mysql</groupId>
		    <artifactId>mysql-connector-java</artifactId>
		    <version>${mysql.version}</version>
		</dependency>
		<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.alibaba</groupId>
		    <artifactId>druid</artifactId>
		    <version>${druid.version}</version>
		</dependency>
		<dependency>
		    <groupId>org.duracloud</groupId>
		    <artifactId>common</artifactId>
		    <version>${common.version}</version>
		</dependency>
		<dependency>
		    <groupId>javax.servlet</groupId>
		    <artifactId>jstl</artifactId>
		    <version>${jstl.version}</version>
		</dependency>
		<dependency>
		    <groupId>com.alibaba</groupId>
		    <artifactId>fastjson</artifactId>
		    <version>${fastjson.version}</version>
		</dependency>
		<dependency>
		    <groupId>com.github.pagehelper</groupId>
		    <artifactId>pagehelper</artifactId>
		    <version>${pagehelper.version}</version>
		</dependency>
		<dependency>
		    <groupId>org.mybatis.generator</groupId>
		    <artifactId>mybatis-generator-core</artifactId>
		    <version>${mybatis-generator.version}</version>
		</dependency>
		<dependency>
		    <groupId>redis.clients</groupId>
		    <artifactId>jedis</artifactId>
		    <version>${jedis.version}</version>
		</dependency>
		<dependency>
		    <groupId>commons-fileupload</groupId>
		    <artifactId>commons-fileupload</artifactId>
		    <version>${commons-fileupload.version}</version>
		</dependency>
	  	<dependency>
		    <groupId>com.sun.jersey</groupId>
		    <artifactId>jersey-client</artifactId>
		    <version>${jersey-client.versoin}</version>
		</dependency>
	  	<dependency>
		    <groupId>com.sun.jersey</groupId>
		    <artifactId>jersey-core</artifactId>
		    <version>${jersey-core.version}</version>
		</dependency>
		<!--  导入和导出excel时需要的jar包 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>${poi-ooxml.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>${poi.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>${poi-ooxml-schemas.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>${xmlbeans.version}</version>
        </dependency>
        <dependency> 
		    <groupId>commons-codec</groupId> 
		    <artifactId>commons-codec</artifactId>  
		    <version>${commons-codec.version}</version> 
		</dependency>
		<dependency>
	        <groupId>net.sourceforge.jexcelapi</groupId>
	        <artifactId>jxl</artifactId>
	        <version>${net.sourceforge.jexcelapi.version}</version>
	    </dependency>
	  </dependencies>
  </dependencyManagement>
</project>

添加完毕后,dao层依赖pojo,service层依赖于dao层、interface层。dao层和service层需要导入mybatis和mysql包、controller层是war工程需要添加很多包(不需要添加版本号),代码如下:
dao层在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.example</groupId>
    <artifactId>parentExample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>daoExample</artifactId>
  <dependencies>
  	<dependency>
  		<groupId>com.Example</groupId>
	    <artifactId>pojo</artifactId>
	    <version>0.0.1-SNAPSHOT</version>
  	</dependency>
  	<dependency>
	    <groupId>org.mybatis</groupId>
	    <artifactId>mybatis</artifactId>
  	</dependency>
  	<dependency>
	    <groupId>mysql</groupId>
	    <artifactId>mysql-connector-java</artifactId>
	</dependency>
  </dependencies>
</project>

service层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.example</groupId>
    <artifactId>parentExample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>serviceExample</artifactId>
  <dependencies>
  	<dependency>
  		<groupId>com.example</groupId>
	    <artifactId>daoExample</artifactId>
	    <version>0.0.1-SNAPSHOT</version>
  	</dependency>
  	<dependency>
  		<groupId>com.example</groupId>
	    <artifactId>interface</artifactId>
	    <version>0.0.1-SNAPSHOT</version>
  	</dependency>
  	<dependency>
	    <groupId>org.mybatis</groupId>
	    <artifactId>mybatis</artifactId>
  	</dependency>
  	<dependency>
	    <groupId>mysql</groupId>
	    <artifactId>mysql-connector-java</artifactId>
	</dependency>
  </dependencies>
</project>
  1. 添加mybatis.xml文件
    在back层的test下添加mybatis.xml文件{mybatis.xml文件生成pojo、mapper和mapper接口。
    mybatis.xml文件代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <context id="testTables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/shop?useUnicode=true&amp;characterEncoding=utf8" 
            userId="root"
            password="123456">
        </jdbcConnection>
        <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
            connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg" 
            userId="yycg"
            password="yycg">
        </jdbcConnection> -->

        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 
            NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- targetProject:生成PO类的位置 -->
        <javaModelGenerator targetPackage="com.yanda.pojo"
            targetProject="..\pojo\src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="com.yanda.dao" 
            targetProject="..\dao\src\main\resources">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- targetPackage:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.yanda.dao" 
            targetProject="..\dao\src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <!-- 指定数据库表 -->
        <table tableName="color"></table>
        <!-- 有些表的字段需要指定java类型
         <table schema="" tableName="">
            <columnOverride column="" javaType="" />
        </table> -->
    </context>
</generatorConfiguration>
  1. **创建要生成的dao层和pojo层 **
    在dao层创建com.yanda.dao和pojo层下创建com.yanda.pojo包
  2. 在back层添加包com.yanda.test再创建MyTest.java文件
    MyTest.java文件代码如下:
    `package com.example.test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

public class MyTest {
public static void main(String[] args) throws Exception {
generator();

}

public static void generator() throws Exception{

    List<String> warnings = new ArrayList<String>();
    boolean overwrite = true;
    //指定逆向工程配置文件
    File configFile = new File(System.getProperty("user.dir")+"/src/test/resources/mybatis.xml");
   ConfigurationParser cp = new ConfigurationParser(warnings);
    Configuration config = cp.parseConfiguration(configFile);
    DefaultShellCallback callback = new DefaultShellCallback(overwrite);
    MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
            callback, warnings);
    myBatisGenerator.generate(null);
    System.out.println("生成完成");
} 

}`

  1. 搭建完成
    在mybatis.xml文件中更换表名,并在mytest.java文件中右键运行,然后右键刷新dao层和pojo层(提醒:数据库中的表需要一个一个添加然后右键运行,添加错误需要删除三个地方【pojo、mapper和mapper接口】并重新添加,最好不要重新添加)

本篇文章纯属作者对ssm框架自己的理解,欢迎大家提出意见和建议

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页