crm项目创建与搭建MyBatis逆向工程根据数据库生成model

一,crm项目创建

什么是CRM:客户关系管理 (CRM) 是用于管理公司与当前和未来客户之间的互动的系统。

1.使用idea的maven创建项目(具体步骤可参数如下链接):
https://blog.csdn.net/weixin_45703155/article/details/125629863?spm=1001.2014.3001.5501
创建成功如图:
crm项目创建与搭建MyBatis逆向工程根据数据库生成model_第1张图片
2.补全java,test目录如下
crm项目创建与搭建MyBatis逆向工程根据数据库生成model_第2张图片
修改上面目录的类型如下:
crm项目创建与搭建MyBatis逆向工程根据数据库生成model_第3张图片
crm项目创建与搭建MyBatis逆向工程根据数据库生成model_第4张图片
crm项目创建与搭建MyBatis逆向工程根据数据库生成model_第5张图片
crm项目创建与搭建MyBatis逆向工程根据数据库生成model_第6张图片
结果如下:
crm项目创建与搭建MyBatis逆向工程根据数据库生成model_第7张图片
3.添加jar包(添加依赖):
(1) mysql驱动

<!-- MySQL数据库连接驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</version>
</dependency>

(2) JDBC 数据源连接池:Druid

<!-- JDBC数据源连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.1</version>
</dependency>

还有:c3p0连接池、dbcp 连接池等。
(3) Mybatis 框架依赖

<!-- MyBatis框架依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>

(4) Spring 相关依赖配置

<!-- Spring框架依赖的JAR配置 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>

(5) Spring AOP依赖

<!-- Spring AOP支持-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>

(6) Mybatis 与 Spring 整合依赖

<!-- MyBatis与Spring整合依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>

(7) 添加项目对JSP的支持

<!-- servlet及jstl标签库依赖的JAR配置 -->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.1.0</version>
</dependency>
<dependency>
  <groupId>javax.servlet.jsp.jstl</groupId>
  <artifactId>jstl-api</artifactId>
  <version>1.2</version>
</dependency>
<dependency>
  <groupId>org.apache.taglibs</groupId>
  <artifactId>taglibs-standard-spec</artifactId>
  <version>1.2.1</version>
</dependency>
<dependency>
  <groupId>org.apache.taglibs</groupId>
  <artifactId>taglibs-standard-impl</artifactId>
  <version>1.2.1</version>
</dependency>

加载jackson插件依赖
(8) Jackson插件依赖

<!-- 加载jackson插件依赖 -->
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>2.7.3</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.7.3</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
  <version>2.7.3</version>
</dependency>

(9) poi依赖

<!--poi依赖-->
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>3.15</version>
</dependency>

(10) fileupload依赖

<!-- 文件上传 -->
<dependency>
  <groupId>commons-fileupload</groupId>
  <artifactId>commons-fileupload</artifactId>
  <version>1.3.1</version>
</dependency>

(11) Log4j依赖

<!-- Log4j2依赖的JAR配置 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.3</version>
</dependency>

把上面代码复制到pom.xml里的标签内如下:

<?xml version="1.0" encoding="UTF-8"?>

<!-- 头信息
    1.xmlns	命名空间,类似包名,因为xml的标签可自定义,需要命名空间来
    2.xmlns:xsi	xml遵循的标签规范
    3.xsi:schemaLocation	用来定义xmlschema的地址,也就是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">

  <!-- maven的基本信息
      1.modelVersion	声明项目描述符遵循哪一个POM模型版本。模型本身的版本很少改变,虽然如此,但它仍然是必不可少的,这是为了当Maven引入了新的特性或者其他模型变更的时候,确保稳定性。
      2.groupId	公司或者组织的唯一标志,并且配置时生成的路径也是由此生成,
      3.如com.winner.trade,maven会将该项目打成的jar包放本地路径:/com/winner/trade
      4.artifactId	本项目的唯一ID,一个groupId下面可能多个项目,就是靠artifactId来区分的
      5.version	本项目目前所处的版本号
      6.packaging	打包类型,可取值:pom , jar , maven-plugin , ejb , war , ear , rar , par等等
      7.name	项目的名称, Maven产生的文档用,可省略
      8.url	项目主页的URL, Maven产生的文档用 ,可省略
  -->
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycrm.maven</groupId>
  <artifactId>my-crm-web</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>my-crm-web Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <!-- 主要用于POM文件的复用和依赖。依赖关系列表.描述了项目相关的所有依赖。 这些依赖组成了项目构建过程中的一个个环节。它们自动从项目定义的仓库中下载。
      1.groupId	依赖项的组织名
      2.artifactId	依赖项的子项目名
      3.version	依赖项的版本
      4.type	依赖类型一般省略,默认类型是jar,其他还有jar,war,ejb-client和test-jar
      5.scope	依赖项的适用范围 ,包括compile,provided,runtime,test,system,exclusions [^1]
      6.optional	可选依赖,如果你在项目B中把C依赖声明为可选,你就需要在依赖于B的项目(例如项目A)中显式的引用对C的依赖。
      7.exclusions	排除项目中的依赖冲突时使用,不依赖该项目

      scope 依赖项的适用范围:
      1.compile,缺省值,适用于所有阶段,会随着项目一起发布。
      2.provided,类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet.jar。
      3.runtime,只在运行时使用,如JDBC驱动,适用运行和测试阶段。
      4.test,只在测试时使用,用于编译和运行测试代码。不会随项目发布。
      5.system,类似provided,需要显式提供包含依赖的jar,Maven不会在Repository中查找它。
      6.optional: 当项目自身被依赖时,标注依赖是否传递。用于连续依赖时使用
  -->
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- MySQL数据库连接驱动 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.43</version>
    </dependency>

    <!-- JDBC数据源连接池 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.1</version>
    </dependency>

    <!-- MyBatis框架依赖 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.1</version>
    </dependency>

    <!-- Spring框架依赖的JAR配置 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-oxm</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>

    <!-- Spring AOP支持-->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.9</version>
    </dependency>

    <!-- MyBatis与Spring整合依赖 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.0</version>
    </dependency>

    <!-- servlet及jstl标签库依赖的JAR配置 -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp.jstl</groupId>
      <artifactId>jstl-api</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.taglibs</groupId>
      <artifactId>taglibs-standard-spec</artifactId>
      <version>1.2.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.taglibs</groupId>
      <artifactId>taglibs-standard-impl</artifactId>
      <version>1.2.1</version>
    </dependency>

    <!-- 加载jackson插件依赖 -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.7.3</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.7.3</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.7.3</version>
    </dependency>

    <!--poi依赖-->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.15</version>
    </dependency>

    <!-- 文件上传 -->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
    </dependency>

    <!-- Log4j2依赖的JAR配置 -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.3</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.3</version>
    </dependency>

  </dependencies>


  <build>

    <!-- finalName build目标文件的名称,默认情况为${artifactId}-${version} -->
    <finalName>my-crm-web</finalName>

    <!-- pluginManagement的配置和plugins的配置是一样的,只是用于继承,使得可以在孩子pom中使用。 -->
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->

      <!--plugins配置用于指定使用的插件-->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>

    <!-- resource配置是过滤src/main/java,src/main/resources目录下文件**/*.xml,**/*.*,
    若文件中有类似${key}**/*.xml,**/*.*这样的配置,就会根据maven的配置进行覆盖,让其使用真实值来填写。
    -->
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.*</include>
        </includes>
      </resource>

<!--      resource配置是不过滤src/main/resources目录下除了context.xml的其他文件,也就不会用真实值来填写${key}这样的配置。-->
<!--      <resource>-->
<!--        <directory>src/main/resources</directory>-->
<!--        <filtering>false</filtering>-->
<!--        <excludes>-->
<!--          <exclude>context.xml</exclude>-->
<!--        </excludes>-->
<!--      </resource>-->
    </resources>

  </build>
</project>





3.配置编码格式:
crm项目创建与搭建MyBatis逆向工程根据数据库生成model_第8张图片
4.添加配置文件:
添加在如下目录:
crm项目创建与搭建MyBatis逆向工程根据数据库生成model_第9张图片

(1)MyBatis配置文件(mybatis-config.xml):

<?xml version="1.0" encoding="UTF-8" ?>
<!--Mybatis配置文件-->
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <typeAliases>
        <package name="com.mycrm.maven.model"/>
    </typeAliases>
    <mappers>
        <package name="com.mycrm.maven.mapper"/>
    </mappers>
</configuration>

(2)配置数据连接与事务配置文件(applicationContext-datasource.xml):

<?xml version="1.0" encoding="UTF-8"?>
<!-- 配置数据连接与事务(spring集成MyBatis) -->
<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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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-4.3.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>  <!--驱动-->
        <property name="username" value="root"/>
        <property name="password" value="123"/>
    <!--jdbc:mysql 数据库协议,固定-->
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/crm2008?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
    </bean>

    <!-- 配置SqlSessionFactory(相当数据库) -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 必须注入属性dataSource -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 加载MyBatis配置文件lasspath:mybatis-config.xml
            如果mybatis没有特殊的配置(比如别名等),configLocation可以省去 ;否则,不能省略
        -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <!-- mapper注解扫描器配置,扫描@MapperScan注解,自动生成代码对象 om.mycrm.maven 项目组织名称-->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.mycrm.maven.settings.mapper,
                                            com.mycrm.maven.workbench.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 配置事务 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.mycrm.maven..service.*.*(..))" id="allMethodPointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="allMethodPointcut"/>
    </aop:config>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="edit*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="do*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="*" propagation="REQUIRED" read-only="true"/>
        </tx:attributes>
    </tx:advice>
</beans>

(3)springMVC配置文件(applicationContext-mvc.xml):

<?xml version="1.0" encoding="UTF-8"?>
<!--springMVC配置文件-->
<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:p="http://www.springframework.org/schema/p"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
    <!-- dispatcherServlet截获所有URL请求 -->
    <mvc:default-servlet-handler />
    <!-- spring mvc 扫描包下的controller -->
    <context:component-scan base-package="com.mycrm.maven.web.controller"/>
    <context:component-scan base-package="com.mycrm.maven.settings.web.controller"/>
    <context:component-scan base-package="com.mycrm.maven.workbench.web.controller"/>
    <!-- 配置注解驱动 -->
    <mvc:annotation-driven/>
    <!-- 配置视图解析器 -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <mvc:interceptors>
        <mvc:interceptor>
            <!--配置拦截的请求-->
            <mvc:mapping path="/settings/**"/>
            <mvc:mapping path="/workbench/**"/>
            <!--配置排除拦截的请求(优先级高)-->
            <mvc:exclude-mapping path="/settings/qx/user/toLogin.do"/>
            <mvc:exclude-mapping path="/settings/qx/user/login.do"/>
            <!--拦截器类-->
            <bean class="com.mycrm.maven.settings.web.interceptor.LoginInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>

    <!-- 配置文件上传解析器 id:必须是multipartResolver-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="#{1024*1024*5}"/>
        <property name="defaultEncoding" value="utf-8"/>
    </bean>
</beans>

(4)spring总配置文件(applicationContext.xml):

<?xml version="1.0" encoding="UTF-8"?>
<!--spring总配置文件-->
<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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:task="http://www.springframework.org/schema/task"
       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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- 加载系统配置文件
    <context:property-placeholder location="classpath:*.properties" />-->
    <!-- 扫描注解 -->
    <context:component-scan base-package="com.mycrm.maven.settings.service" />
    <context:component-scan base-package="com.mycrm.maven.workbench.service" />
    <!-- 导入数据相关配置 加载配置数据连接与事务配置文件applicationContext-datasource.xml-->
    <import resource="applicationContext-datasource.xml" />
</beans>

(5)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="dataservice" version="3.0">
  <display-name>dataservice application</display-name>

  <!--
    spring监听器加载applicationContext.xml  Spring总配置文件
    服务器启动时,就加载Spring总配置文件applicationContext.xml
   -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- spring字符过滤器 -->
  <filter>
    <filter-name>encodingFilter</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>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!-- Spring mvc分发servlet
      加载spring MVC配置文件applicationContext-mvc.xml
   -->
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <!-- 欢迎页,默认进入index controller -->
  <welcome-file-list>
    <welcome-file>/</welcome-file>
  </welcome-file-list>
</web-app>

5.添加静态页面:
在tomocat上web项目结构如下:
crm项目创建与搭建MyBatis逆向工程根据数据库生成model_第10张图片
因为web应用根目录下的内容都是不安全的,外界可以通过url直接访问,所以,一般为了数据的安全,都会把页面放在WEB-INF下,因为WEB-INF目录下的资源是受保护的。所以静态页面资源放置位置如下:
crm项目创建与搭建MyBatis逆向工程根据数据库生成model_第11张图片
6.编写首页和登录页面:
用户控制器(UserController):

package com.mycrm.maven.settings.web.controller;

import com.mycrm.maven.commons.domain.ReturnObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/settings/power/user/")
public class UserController {

    /**
     * 登录页面
     * @return
     */
    @RequestMapping("login")
    public String login(){
        return "settings/power/user/login";
    }

首页(index.jsp)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
</head>
<body>
<script type="text/javascript">
    window.location.href = "settings/power/user/login";
</script>
</body>
</html>

登录页(login.jsp):

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
%>
<html>
<head>
    <base href="<%=basePath%>">
    <meta charset="UTF-8">
    <link href="jquery/bootstrap_3.3.0/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
    <script type="text/javascript" src="jquery/jquery-1.11.1-min.js"></script>
    <script type="text/javascript" src="jquery/bootstrap_3.3.0/js/bootstrap.min.js"></script>
    <script type="text/javascript">
        $(function () {
            //给整个浏览器窗口添加键盘按下事件
            $(window).keydown(function (e) {
                //如果按的是回车键,则提交登录请求
                if(e.keyCode==13){
                    $("#loginBtn").click();
                }
            });

            //给"登录"按钮添加单击事件
            $("#loginBtn").click(function () {
                //收集参数
                var loginAct=$.trim($("#loginAct").val());
                var loginPwd=$.trim($("#loginPwd").val());
                var isRemPwd=$("#isRemPwd").prop("checked");
                //表单验证
                if(loginAct==""){
                    alert("用户名不能为空");
                    return;
                }
                if(loginPwd==""){
                    alert("密码不能为空");
                    return;
                }

                //显示正在验证
                //$("#msg").text("正在努力验证....");
                //发送请求
                $.ajax({
                    url:'settings/qx/user/login.do',
                    data:{
                        loginAct:loginAct,
                        loginPwd:loginPwd,
                        isRemPwd:isRemPwd
                    },
                    type:'post',
                    dataType:'json',
                    success:function (data) {
                        if(data.code=="1"){
                            //跳转到业务主页面
                            window.location.href="workbench/index.do";
                        }else{
                            //提示信息
                            $("#msg").text(data.message);
                        }
                    },
                    beforeSend:function () {//当ajax向后台发送请求之前,会自动执行本函数;
                        //该函数的返回值能够决定ajax是否真正向后台发送请求:
                        //如果该函数返回true,则ajax会真正向后台发送请求;否则,如果该函数返回false,则ajax放弃向后台发送请求。
                        $("#msg").text("正在努力验证....");
                        return true;
                    }
                });
            });
        });
    </script>
</head>
<body>
<div style="position: absolute; top: 0px; left: 0px; width: 60%;">
    <img src="image/IMG_7114.JPG" style="width: 100%; height: 90%; position: relative; top: 50px;">
</div>
<div id="top" style="height: 50px; background-color: #3C3C3C; width: 100%;">
    <div style="position: absolute; top: 5px; left: 0px; font-size: 30px; font-weight: 400; color: white; font-family: 'times new roman'">CRM &nbsp;<span style="font-size: 12px;">&copy;2019&nbsp;动力节点</span></div>
</div>

<div style="position: absolute; top: 120px; right: 100px;width:450px;height:400px;border:1px solid #D5D5D5">
    <div style="position: absolute; top: 0px; right: 60px;">
        <div class="page-header">
            <h1>登录</h1>
        </div>
        <form action="workbench/index.html" class="form-horizontal" role="form">
            <div class="form-group form-group-lg">
                <div style="width: 350px;">
                    <input class="form-control" id="loginAct" type="text" value="${cookie.loginAct.value}" placeholder="用户名">
                </div>
                <div style="width: 350px; position: relative;top: 20px;">
                    <input class="form-control" id="loginPwd" type="password" value="${cookie.loginPwd.value}" placeholder="密码">
                </div>
                <div class="checkbox"  style="position: relative;top: 30px; left: 10px;">
                    <label>
                        <c:if test="${not empty cookie.loginAct and not empty cookie.loginPwd}">
                            <input type="checkbox" id="isRemPwd" checked>
                        </c:if>
                        <c:if test="${empty cookie.loginAct or empty cookie.loginPwd}">
                            <input type="checkbox" id="isRemPwd">
                        </c:if>
                        十天内免登录
                    </label>
                    &nbsp;&nbsp;
                    <span id="msg" style="color: red"></span>
                </div>
                <button type="button" id="loginBtn" class="btn btn-primary btn-lg btn-block"  style="width: 350px; position: relative;top: 45px;">登录</button>
            </div>
        </form>
    </div>
</div>
</body>
</html>

crm项目创建与搭建MyBatis逆向工程根据数据库生成model_第12张图片
上面的启动页写在web.xml上:

<!--   欢迎页,默认进入index controller -->
  <welcome-file-list>
    <welcome-file>/index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

成功如图:
crm项目创建与搭建MyBatis逆向工程根据数据库生成model_第13张图片

二,MyBatis逆向工程根据数据库生成model

在my-crm-web项目下的同级目录下创建maven工程
1.创建项目
crm项目创建与搭建MyBatis逆向工程根据数据库生成model_第14张图片

crm项目创建与搭建MyBatis逆向工程根据数据库生成model_第15张图片
crm项目创建与搭建MyBatis逆向工程根据数据库生成model_第16张图片
crm项目创建与搭建MyBatis逆向工程根据数据库生成model_第17张图片
生成项目如下:
crm项目创建与搭建MyBatis逆向工程根据数据库生成model_第18张图片
在pom.xml添加myBatis逆向工程插件

<build>
        <plugins>
            <!--myBatis逆向工程插件-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
    </build>

添加后pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<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.mycrm.maven</groupId>
    <artifactId>crm-mybatis-generator</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <!--myBatis逆向工程插件-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

在resources添加以下两个配置文件:
crm项目创建与搭建MyBatis逆向工程根据数据库生成model_第19张图片
配置数据库信息,生成的model放在哪里(generatorConfig.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>

    <!--指定mysql数据库驱动-->
    <!--<classPathEntry location="E://repository-p2p//mysql//mysql-connector-java//5.1.43//mysql-connector-java-5.1.43.jar"/>-->

    <!--导入属性配置-->
    <properties resource="generator.properties"></properties>

    <!--指定特定数据库的jdbc驱动jar包的位置-->
    <classPathEntry location="${jdbc.driverLocation}"/>

    <context id="default" targetRuntime="MyBatis3">

        <!-- optional,旨在创建class时,对注释进行控制,false生成注释,true无注释 -->
        <commentGenerator>
            <property name="suppressDate" value="false"/>
            <property name="suppressAllComments" value="false"/>
        </commentGenerator>

        <!--jdbc的数据库连接 (对应的是generator.properties的数据) -->
        <jdbcConnection
                driverClass="${jdbc.driverClass}"
                connectionURL="${jdbc.connectionURL}"
                userId="${jdbc.userId}"
                password="${jdbc.password}">
        </jdbcConnection>


        <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>


        <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
            targetPackage     指定生成的model生成所在的包名
            targetProject     指定在该项目下所在的路径|指定生成到的工程名称
        -->
        <javaModelGenerator targetPackage="com.mycrm.maven.settings.model"
                            targetProject="E:\admin\java\my-crm-web\src\main\java">

            <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
            <property name="enableSubPackages" value="false"/>
            <!-- 是否对model添加 构造函数 true添加,false不添加-->
            <property name="constructorBased" value="false"/>
            <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
            <property name="trimStrings" value="true"/>
            <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->
            <property name="immutable" value="false"/>
        </javaModelGenerator>

        <!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
        <sqlMapGenerator targetPackage="com.mycrm.maven.settings.mapper"
                         targetProject="E:\admin\java\my-crm-web\src\main\java">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
                type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
                type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
                type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
        -->
        <javaClientGenerator targetPackage="com.mycrm.maven.settings.mapper"
                             targetProject="E:\admin\java\my-crm-web\src\main\java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>


      
        <table tableName="tbl_user" domainObjectName="User"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <!--
                      <table tableName="tbl_clue" domainObjectName="Clue"
                             enableCountByExample="false" enableUpdateByExample="false"
                             enableDeleteByExample="false" enableSelectByExample="false"
                             selectByExampleQueryId="false">
                      </table>
      
                      <table tableName="tbl_clue_activity_relation" domainObjectName="ClueActivityRelation"
                             enableCountByExample="false" enableUpdateByExample="false"
                             enableDeleteByExample="false" enableSelectByExample="false"
                             selectByExampleQueryId="false">
                      </table>
      
                      <table tableName="tbl_clue_remark" domainObjectName="ClueRemark"
                             enableCountByExample="false" enableUpdateByExample="false"
                              enableDeleteByExample="false" enableSelectByExample="false"
                              selectByExampleQueryId="false">
                      </table>
      
              <table tableName="tbl_contacts" domainObjectName="Contacts"
                     enableCountByExample="false" enableUpdateByExample="false"
                     enableDeleteByExample="false" enableSelectByExample="false"
                     selectByExampleQueryId="false">
              </table>
      
              <table tableName="tbl_contacts_activity_relation" domainObjectName="ContactsActivityRelation"
                     enableCountByExample="false" enableUpdateByExample="false"
                     enableDeleteByExample="false" enableSelectByExample="false"
                     selectByExampleQueryId="false">
              </table>
      
              <table tableName="tbl_contacts_remark" domainObjectName="ContactsRemark"
                     enableCountByExample="false" enableUpdateByExample="false"
                     enableDeleteByExample="false" enableSelectByExample="false"
                     selectByExampleQueryId="false">
              </table>
      
              <table tableName="tbl_customer" domainObjectName="Customer"
                     enableCountByExample="false" enableUpdateByExample="false"
                     enableDeleteByExample="false" enableSelectByExample="false"
                     selectByExampleQueryId="false">
              </table>
      
                      <table tableName="tbl_customer_remark" domainObjectName="CustomerRemark"
                             enableCountByExample="false" enableUpdateByExample="false"
                             enableDeleteByExample="false" enableSelectByExample="false"
                             selectByExampleQueryId="false">
                      </table>
      
                      <table tableName="tbl_dictionary_type" domainObjectName="DictionaryType"
                             enableCountByExample="false" enableUpdateByExample="false"
                             enableDeleteByExample="false" enableSelectByExample="false"
                             selectByExampleQueryId="false">
                      </table>
      
      
                      <table tableName="tbl_dic_value" domainObjectName="DicValue"
                             enableCountByExample="false" enableUpdateByExample="false"
                             enableDeleteByExample="false" enableSelectByExample="false"
                             selectByExampleQueryId="false">
                      </table>
      
                      <table tableName="tbl_activity" domainObjectName="Activity"
                             enableCountByExample="false" enableUpdateByExample="false"
                             enableDeleteByExample="false" enableSelectByExample="false"
                             selectByExampleQueryId="false">
                      </table>
      
      
                      <table tableName="tbl_activity_remark" domainObjectName="ActivityRemark"
                             enableCountByExample="false" enableUpdateByExample="false"
                             enableDeleteByExample="false" enableSelectByExample="false"
                             selectByExampleQueryId="false">
                      </table>
      
              <table tableName="tbl_tran" domainObjectName="Tran"
                     enableCountByExample="false" enableUpdateByExample="false"
                     enableDeleteByExample="false" enableSelectByExample="false"
                     selectByExampleQueryId="false">
      
              <table tableName="tbl_tran_history" domainObjectName="TranHistory"
                     enableCountByExample="false" enableUpdateByExample="false"
                     enableDeleteByExample="false" enableSelectByExample="false"
                     selectByExampleQueryId="false">
              </table>
      
              <table tableName="tbl_tran_remark" domainObjectName="TranRemark"
                     enableCountByExample="false" enableUpdateByExample="false"
                     enableDeleteByExample="false" enableSelectByExample="false"
                     selectByExampleQueryId="false">
              </table>
               -->

    </context>
</generatorConfiguration>

mysql驱动和数据库信息(generator.properties):

jdbc.driverLocation=D:/java-maven/jar/mysql/mysql-connector-java-5.1.43.jar
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://127.0.0.1:3306/crm
jdbc.userId=root
jdbc.password=123

运行crm-mybatis-generator项目:
crm项目创建与搭建MyBatis逆向工程根据数据库生成model_第20张图片

成功如下,并生成如下几个文件:
crm项目创建与搭建MyBatis逆向工程根据数据库生成model_第21张图片
crm项目创建与搭建MyBatis逆向工程根据数据库生成model_第22张图片

你可能感兴趣的:(mybatis,mybatis,数据库,java)