java学习笔记——众筹项目练习——基于maven的基础环境搭建

                                   基于maven的基础环境搭建

前面我们学习了如何使用maven,今天我们就使用maven来搭建一个基础的开发环境:根据项目模块进行拆分与依赖、根据maven整合ssm。

  • 项目模块间的分包与依赖

         为了在项目开发时更加简洁与快速,我们会对整个项目进行拆分,拆分的方式有两种:横线拆分纵向拆分,横向拆分是指根据功能将整个项目拆分成可独立运行的功能模块(web项目),但是这样一来各个功能模块之间想要进行调用就会比较麻烦,我们指定web项目之间是没法进行依赖的,所以还需要使用webservice进行web模块间通信;纵向拆分就简单很多了,就是将之前项目工程中的分包变为分模块。

 

 

        我们的项目怎么拆分呢?横向纵向一起使用来进行拆分,将整个众筹系统分为前台(面向用户)与后台(面向管理员)两个单独的功能模块(横向),然后在这个单独的功能模块中对分包操作进行纵向拆分

 

 

好了 不多说了,我们开始构建环境,首先在很多公司中都会一个父工程(pom形式),这个工程是公司长时间积累下来的 ,作用是让别的工程(即整个公司的项目)进行继承,别的工程的所有jar的版本都是父工程来统一,所以我们先来创建一个这样父工程;

 

首先创建一个我们众筹项目的工作集,然后新建一个maven项目

这个父工程的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.gome</groupId>
  <artifactId>project-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  
  <!-- 父工程中来做依赖管理;jar包的版本控制等都在这里 -->
	<properties>
		<!-- 公共依赖 -->
		<commons-io.version>2.5</commons-io.version>
		<commons-lang3.version>3.6</commons-lang3.version>
		<commons-codec.version>1.10</commons-codec.version>
		<commons-beanutils.version>1.9.3</commons-beanutils.version>
		<commons-collections.version>3.2.2</commons-collections.version>
		<commons-math3.version>3.6.1</commons-math3.version>
		<commons.fileupload>1.3.2</commons.fileupload>
		<commons-email.version>1.4</commons-email.version>
		
		<!-- junit -->
		<junit.version>4.12</junit.version>
		<!-- 时间日期操作 -->
		<joda-time.version>2.9.9</joda-time.version>
		<!-- httpclient -->
		<httpclient.version>4.5.3</httpclient.version>
		<!-- 功能组件 -->
		<poi.version>3.16</poi.version>
		<quartz.version>2.2.3</quartz.version>
		<!-- 数据库 -->
		<!--druid:德鲁伊  -->
		<druid.version>1.1.0</druid.version>
		<mysql.connector>5.1.42</mysql.connector>

		<!-- 基础框架 -->
		<spring.version>4.3.8.RELEASE</spring.version>
		<mybatis.version>3.3.1</mybatis.version>
		<mybatis.spring.version>1.3.1</mybatis.spring.version>

		<!-- 分页 -->
		<pagehelper.version>5.0.3</pagehelper.version>
		<!-- jackson -->
		<jackson.version>2.7.4</jackson.version>

		<!-- MBG -->
		<mbg.version>1.3.5</mbg.version>

		<!-- 日志 -->
		<log4j.version>1.2.17</log4j.version>
		<slf4j.version>1.7.6</slf4j.version>

		<!-- servlet-api,jsp-api,jstl -->
		<servlet-api.version>2.5</servlet-api.version>
		<jsp-api.version>2.2</jsp-api.version>
		<jstl.version>1.2</jstl.version>

		<!-- email,commons,httpclient...activiti... -->
		<activiti.version>5.22.0</activiti.version>
		<activiti.spring.version>5.22.0</activiti.spring.version>
	</properties>
	<!-- 依赖管理 -->
	<dependencyManagement>
		<dependencies>
			<!-- 公共依赖 -->
			<dependency>
				<groupId>commons-io</groupId>
				<artifactId>commons-io</artifactId>
				<version>${commons-io.version}</version>
			</dependency>
			<dependency>
				<groupId>org.apache.commons</groupId>
				<artifactId>commons-lang3</artifactId>
				<version>${commons-lang3.version}</version>
			</dependency>
			<dependency>
				<groupId>commons-codec</groupId>
				<artifactId>commons-codec</artifactId>
				<version>${commons-codec.version}</version>
			</dependency>
			<dependency>
				<groupId>commons-beanutils</groupId>
				<artifactId>commons-beanutils</artifactId>
				<version>${commons-beanutils.version}</version>
			</dependency>
			<dependency>
				<groupId>commons-collections</groupId>
				<artifactId>commons-collections</artifactId>
				<version>${commons-collections.version}</version>
			</dependency>
			<dependency>
				<groupId>org.apache.commons</groupId>
				<artifactId>commons-math3</artifactId>
				<version>${commons-math3.version}</version>
			</dependency>
			<dependency>
				<groupId>commons-fileupload</groupId>
				<artifactId>commons-fileupload</artifactId>
				<version>${commons.fileupload}</version>
			</dependency>
			<dependency>
				<groupId>org.apache.commons</groupId>
				<artifactId>commons-email</artifactId>
				<version>${commons-email.version}</version>
			</dependency>

			<!--公共依赖结束 -->

			<!-- junit -->
			<dependency>
				<groupId>junit</groupId>
				<artifactId>junit</artifactId>
				<version>${junit.version}</version>
				<scope>test</scope>
			</dependency>

			<!-- 时间日期 -->
			<dependency>
				<groupId>joda-time</groupId>
				<artifactId>joda-time</artifactId>
				<version>${joda-time.version}</version>
			</dependency>

			<!-- httpclient -->
			<dependency>
				<groupId>org.apache.httpcomponents</groupId>
				<artifactId>httpclient</artifactId>
				<version>${httpclient.version}</version>
			</dependency>


			<!-- 其他功能性组件 -->
			<!-- poi:文档操作 -->
			<dependency>
				<groupId>org.apache.poi</groupId>
				<artifactId>poi</artifactId>
				<version>${poi.version}</version>
			</dependency>
			<!-- quartz:石英调度 -->
			<dependency>
				<groupId>org.quartz-scheduler</groupId>
				<artifactId>quartz</artifactId>
				<version>${quartz.version}</version>
			</dependency>


			<!-- 数据库模块 -->
			<!-- 连接池 -->
			<dependency>
				<groupId>com.alibaba</groupId>
				<artifactId>druid</artifactId>
				<version>${druid.version}</version>
			</dependency>
			<!-- 驱动 -->
			<dependency>
				<groupId>mysql</groupId>
				<artifactId>mysql-connector-java</artifactId>
				<version>${mysql.connector}</version>
			</dependency>

			<!-- 基础框架 -->
			<!-- Spring配置 -->
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-beans</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-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-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-orm</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-webmvc-portlet</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-test</artifactId>
				<version>${spring.version}</version>
				<scope>test</scope>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context-support</artifactId>
				<version>${spring.version}</version>
			</dependency>

			<!-- mybatis配置 -->
			<dependency>
				<groupId>org.mybatis</groupId>
				<artifactId>mybatis-spring</artifactId>
				<version>${mybatis.spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.mybatis</groupId>
				<artifactId>mybatis</artifactId>
				<version>${mybatis.version}</version>
			</dependency>
			<!-- 基础框架完成 -->

			<!-- 分页 -->
			<dependency>
				<groupId>com.github.pagehelper</groupId>
				<artifactId>pagehelper</artifactId>
				<version>${pagehelper.version}</version>
			</dependency>

			<!-- jackson -->
			<dependency>
				<groupId>com.fasterxml.jackson.core</groupId>
				<artifactId>jackson-core</artifactId>
				<version>${jackson.version}</version>
			</dependency>
			<dependency>
				<groupId>com.fasterxml.jackson.core</groupId>
				<artifactId>jackson-databind</artifactId>
				<version>${jackson.version}</version>
			</dependency>

			<!-- MBG -->
			<dependency>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-core</artifactId>
				<version>${mbg.version}</version>
			</dependency>

			<!-- 工作流 -->
			<dependency>
				<groupId>org.activiti</groupId>
				<artifactId>activiti-engine</artifactId>
				<version>${activiti.version}</version>
			</dependency>
			<dependency>
				<groupId>org.activiti</groupId>
				<artifactId>activiti-spring</artifactId>
				<version>${activiti.spring.version}</version>
			</dependency>
			<!-- 日志 -->
			<dependency>
				<groupId>log4j</groupId>
				<artifactId>log4j</artifactId>
				<version>${log4j.version}</version>
			</dependency>
			<dependency>
				<groupId>org.slf4j</groupId>
				<artifactId>slf4j-log4j12</artifactId>
				<version>${slf4j.version}</version>
			</dependency>
			<dependency>
				<groupId>org.slf4j</groupId>
				<artifactId>slf4j-api</artifactId>
				<version>${slf4j.version}</version>
			</dependency>

			<!-- 依赖的WEB类库 -->
			<dependency>
				<groupId>javax.servlet.jsp</groupId>
				<artifactId>jsp-api</artifactId>
				<version>${jsp-api.version}</version>
				<scope>provided</scope>
			</dependency>
			<dependency>
				<groupId>javax.servlet</groupId>
				<artifactId>servlet-api</artifactId>
				<version>${servlet-api.version}</version>
				<scope>provided</scope>
			</dependency>
			<dependency>
				<groupId>javax.servlet</groupId>
				<artifactId>jstl</artifactId>
				<version>${jstl.version}</version>
			</dependency>
		</dependencies>
	</dependencyManagement>
</project>

公司中除了有一个公共的父工程外,可能还会有一个公共的依赖库(jar包的形式),其中包含很多工具类供大家来使用 ,别的项目想要使用,只需要添加依赖这个jar包就可以了;

我们顺便来创建一个工具类,供我们测试时使用;

 

这个公共依赖库很可能继承与公共的父工程,为了达到版本控制的效果,所以他的pom.xml文件中的依赖都可能来自于 公共父工程中的pom.xml文件中,因为是继承来的,所以不需要填写版本号,只需要说明id即可;

<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.gome</groupId>
    <artifactId>project-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>com.gome</groupId>
  <artifactId>project-commons</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
  	<!-- 公共依赖 -->
			<dependency>
				<groupId>commons-io</groupId>
				<artifactId>commons-io</artifactId>
			</dependency>
			<dependency>
				<groupId>org.apache.commons</groupId>
				<artifactId>commons-lang3</artifactId>
			</dependency>
			<dependency>
				<groupId>commons-codec</groupId>
				<artifactId>commons-codec</artifactId>
			</dependency>
			<dependency>
				<groupId>commons-beanutils</groupId>
				<artifactId>commons-beanutils</artifactId>
			</dependency>
			<dependency>
				<groupId>commons-collections</groupId>
				<artifactId>commons-collections</artifactId>
			</dependency>
			<dependency>
				<groupId>org.apache.commons</groupId>
				<artifactId>commons-math3</artifactId>
			</dependency>
			<dependency>
				<groupId>commons-fileupload</groupId>
				<artifactId>commons-fileupload</artifactId>
			</dependency>
			<dependency>
				<groupId>org.apache.commons</groupId>
				<artifactId>commons-email</artifactId>
			</dependency>

			<!--公共依赖结束 -->
  </dependencies>
</project>

到这里,我们已经创建了(很多公司都已经提供了)一个公共的父工程和一个公共依赖库,现在我们来创建我们众筹项目的工程;

 

因为整个众筹项目我们拆分出来两个功能模块,一个前台模块(面向用户),一个后台模块(面向管理员),所以我们的整个众筹项目应该是一个父工程,这个父工程中有两个能够独立运行的子模块,并且这个父工程继承与公司的公共父工程,先来创建这个众筹项目的父工程;

再来创建其中的manager模块(后台模块),manager模块是一个可以独立运行的子模块,是不是觉得它应该是一个war包呢?但是因为我们还需要将manager拆分出几个小模块,所以它也只能是个pom啦。。。

好,只后我们一次为manager添加pojo模块(jar包供别人使用)、dao模块(jar包供别人使用)、service模块(jar包供别人使用)与web模块(war包可以运行的web工程);

pojo

dao

service

web

为web模块添加web.xml文件

先取消勾选、应用、再勾选、

就会出现下图

修改生成web.xml的路径,点击ok

 

这样我们的manager模块就聚合了pojo、dao、service、web这四个模块;然后我们为这四个模块添加依赖关系,依赖关系为web依赖service、service依赖dao、dao依赖pojo;

为web模块添加springMVC,做一个简单的测试;

导包

<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.gome.scw</groupId>
    <artifactId>scw-manager</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>manager-web</artifactId>
  <packaging>war</packaging>
  
  <dependencies>
  	<dependency>
  		<groupId>com.gome.scw</groupId>
  		<artifactId>managet-service</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  	
  	<!-- junit -->
			<dependency>
				<groupId>junit</groupId>
				<artifactId>junit</artifactId>
				<version>${junit.version}</version>
				<scope>test</scope>
			</dependency>

			<!-- 数据库模块 -->
			<!-- 连接池 -->
			<dependency>
				<groupId>com.alibaba</groupId>
				<artifactId>druid</artifactId>
			</dependency>
			<!-- 驱动 -->
			<dependency>
				<groupId>mysql</groupId>
				<artifactId>mysql-connector-java</artifactId>
			</dependency>

			<!-- 基础框架 -->
			<!-- Spring配置 -->
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-beans</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-tx</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-core</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</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-orm</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-webmvc</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-webmvc-portlet</artifactId>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-test</artifactId>
				<scope>test</scope>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context-support</artifactId>
			</dependency>

			<!-- mybatis配置 -->
			<dependency>
				<groupId>org.mybatis</groupId>
				<artifactId>mybatis-spring</artifactId>
			</dependency>
			<dependency>
				<groupId>org.mybatis</groupId>
				<artifactId>mybatis</artifactId>
			</dependency>
			<!-- 基础框架完成 -->

			<!-- 分页 -->
			<dependency>
				<groupId>com.github.pagehelper</groupId>
				<artifactId>pagehelper</artifactId>
			</dependency>

			<!-- jackson -->
			<dependency>
				<groupId>com.fasterxml.jackson.core</groupId>
				<artifactId>jackson-core</artifactId>
			</dependency>
			<dependency>
				<groupId>com.fasterxml.jackson.core</groupId>
				<artifactId>jackson-databind</artifactId>
			</dependency>

			<!-- MBG -->
			<dependency>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-core</artifactId>
			</dependency>


			<!-- 日志 -->
			<dependency>
				<groupId>log4j</groupId>
				<artifactId>log4j</artifactId>
			</dependency>
			<dependency>
				<groupId>org.slf4j</groupId>
				<artifactId>slf4j-log4j12</artifactId>
			</dependency>
			<dependency>
				<groupId>org.slf4j</groupId>
				<artifactId>slf4j-api</artifactId>
			</dependency>

			<!-- 依赖的WEB类库 -->
			<dependency>
				<groupId>javax.servlet.jsp</groupId>
				<artifactId>jsp-api</artifactId>
				<scope>provided</scope>
			</dependency>
			<dependency>
				<groupId>javax.servlet</groupId>
				<artifactId>servlet-api</artifactId>
				<scope>provided</scope>
			</dependency>
			<dependency>
				<groupId>javax.servlet</groupId>
				<artifactId>jstl</artifactId>
			</dependency>
  </dependencies>
</project>

写配置

新建一个springmvc.xml

编写web.xml

添加Person类和HelloController类

添加index.jsp页面与success.jsp页面

 运行

当当当当。成功!!!!

再贴一个结构图

 

  • 使用maven整合ssm

好了,我们项目的分包在上边已经做好了,只是在上边导入三大框架与一些其他的基础包时,建议还是按需要导入,不同的模块导入不同的包,不要全部都放在一起,会比较乱不容易查看。下面整合开始,因为之前有过ssm的整合,所以不清楚的地方我们可以查看一下之前的文章。

  1. 整合mybatis

mybatis的整合比较简单,所有需要的包都已经在上边导入过来,只写配件文件就可以了,配置文件的内容也很简单,基本上只需要写<settings>和<plugins>这两个标签就可以了,数据源和扫描文件都可以在spring中完成。在编写配置文件之前我们还需要dao接口和sql的xml文件与相关的数据库表,我们可以使用mbg根据我们创建的数据库表逆向生成dao接口和sql的xml文件,并且pojo也同时一起生产,省去了我们自己编写的工作。

首先,创建数据库表,这里创建了多张数据库表,为了便于理解大家只看其中的一个就可以了。

复制sql语句直接在mysql中创建就可以啦

/*==============================================================*/
/* DBMS name:      MySQL 5.0                                    */
/* Created on:     2017/6/12 21:44:22                           */
/*==============================================================*/


drop table if exists t_account_type_cert;

drop table if exists t_advertisement;

drop table if exists t_cert;

drop table if exists t_dictionary;

drop table if exists t_member;

drop table if exists t_member_address;

drop table if exists t_member_cert;

drop table if exists t_member_project_follow;

drop table if exists t_message;

drop table if exists t_order;

drop table if exists t_param;

drop table if exists t_permission;

drop table if exists t_project;

drop table if exists t_project_tag;

drop table if exists t_project_type;

drop table if exists t_return;

drop table if exists t_role;

drop table if exists t_role_permission;

drop table if exists t_tag;

drop table if exists t_type;

drop table if exists t_user;

drop table if exists t_user_role;

/*==============================================================*/
/* Table: t_account_type_cert                                   */
/*==============================================================*/
create table t_account_type_cert
(
   id                   int(11) not null auto_increment,
   accttype             char(1),
   certid               int(11),
   primary key (id)
);

/*==============================================================*/
/* Table: t_advertisement                                       */
/*==============================================================*/
create table t_advertisement
(
   id                   int(11) not null auto_increment,
   name                 varchar(255),
   iconpath             varchar(255),
   status               char(1),
   url                  varchar(255),
   userid               int(11),
   primary key (id)
);

/*==============================================================*/
/* Table: t_cert                                                */
/*==============================================================*/
create table t_cert
(
   id                   int(11) not null auto_increment,
   name                 varchar(255),
   primary key (id)
);

/*==============================================================*/
/* Table: t_dictionary                                          */
/*==============================================================*/
create table t_dictionary
(
   id                   int(11) not null auto_increment,
   name                 varchar(255),
   code                 varchar(255),
   subcode              varchar(255),
   val                  varchar(255),
   primary key (id)
);

/*==============================================================*/
/* Table: t_member                                              */
/*==============================================================*/
create table t_member
(
   id                   int(11) not null auto_increment,
   loginacct            varchar(255) not null,
   userpswd             char(32) not null,
   username             varchar(255) not null,
   email                varchar(255) not null,
   authstatus           char(1) not null,
   usertype             char(1) not null,
   realname             varchar(255),
   cardnum              varchar(255),
   accttype             char(1),
   primary key (id)
);

/*==============================================================*/
/* Table: t_member_address                                      */
/*==============================================================*/
create table t_member_address
(
   id                   int(11) not null auto_increment,
   memberid             int(11),
   address              varchar(255),
   primary key (id)
);

/*==============================================================*/
/* Table: t_member_cert                                         */
/*==============================================================*/
create table t_member_cert
(
   id                   int(11) not null auto_increment,
   memberid             int(11),
   certid               int(11),
   iconpath             varchar(255),
   primary key (id)
);

/*==============================================================*/
/* Table: t_member_project_follow                               */
/*==============================================================*/
create table t_member_project_follow
(
   id                   int(11) not null auto_increment,
   projectid            int(11),
   memberid             int(11),
   primary key (id)
);

/*==============================================================*/
/* Table: t_message                                             */
/*==============================================================*/
create table t_message
(
   id                   int(11) not null auto_increment,
   memberid             int(11),
   content              varchar(255),
   senddate             char(19),
   primary key (id)
);

/*==============================================================*/
/* Table: t_order                                               */
/*==============================================================*/
create table t_order
(
   id                   int(11) not null auto_increment,
   memberid             int(11),
   projectid            int(11),
   returnid             int(11),
   ordernum             varchar(255),
   createdate           char(19),
   money                int(11),
   rtncount             int(11),
   status               char(1),
   address              varchar(255),
   invoice              char(1),
   invoictitle          varchar(255),
   remark               varchar(255),
   primary key (id)
);

/*==============================================================*/
/* Table: t_param                                               */
/*==============================================================*/
create table t_param
(
   id                   int(11) not null auto_increment,
   name                 varchar(255),
   code                 varchar(255),
   val                  varchar(255),
   primary key (id)
);

/*==============================================================*/
/* Table: t_permission                                          */
/*==============================================================*/
create table t_permission
(
   id                   int(11) not null auto_increment,
   pid                  int(11),
   name                 varchar(255),
   icon                 varchar(255),
   url                  varchar(255),
   primary key (id)
);

/*==============================================================*/
/* Table: t_project                                             */
/*==============================================================*/
create table t_project
(
   id                   int(11) not null auto_increment,
   name                 varchar(255),
   remark               varchar(255),
   money                bigint (11),
   day                  int(11),
   status               char(1),
   deploydate           char(10),
   supportmoney         bigint(11),
   supporter            int(11),
   completion           int(3),
   memberid             int(11),
   createdate           char(19),
   follower             int(11),
   primary key (id)
);

/*==============================================================*/
/* Table: t_project_tag                                         */
/*==============================================================*/
create table t_project_tag
(
   id                   int(11) not null auto_increment,
   projectid            int(11),
   tagid                int(11),
   primary key (id)
);

/*==============================================================*/
/* Table: t_project_type                                        */
/*==============================================================*/
create table t_project_type
(
   id                   int not null auto_increment,
   projectid            int(11),
   typeid               int(11),
   primary key (id)
);

/*==============================================================*/
/* Table: t_return                                              */
/*==============================================================*/
create table t_return
(
   id                   int(11) not null auto_increment,
   projectid            int(11),
   type                 char(1),
   supportmoney         int(11),
   content              varchar(255),
   count                int(11),
   signalpurchase       int(11),
   purchase             int(11),
   freight              int(11),
   invoice              char(1),
   rtndate              int(11),
   primary key (id)
);

/*==============================================================*/
/* Table: t_role                                                */
/*==============================================================*/
create table t_role
(
   id                   int(11) not null,
   name                   varchar(255),
   primary key (id)
);

/*==============================================================*/
/* Table: t_role_permission                                     */
/*==============================================================*/
create table t_role_permission
(
   id                   int(11) not null auto_increment,
   roleid               int(11),
   permissionid         int(11),
   primary key (id)
);

/*==============================================================*/
/* Table: t_tag                                                 */
/*==============================================================*/
create table t_tag
(
   id                   int(11) not null auto_increment,
   pid                  int(11),
   name                 varchar(255),
   primary key (id)
);

/*==============================================================*/
/* Table: t_type                                                */
/*==============================================================*/
create table t_type
(
   id                   int(11) not null auto_increment,
   name                 varchar(255),
   primary key (id)
);

/*==============================================================*/
/* Table: t_user                                                */
/*==============================================================*/
create table t_user
(
   id                   int not null auto_increment,
   loginacct            varchar(255) not null,
   userpswd             char(32) not null,
   username             varchar(255) not null,
   email                varchar(255) not null,
   createtime           char(19),
   primary key (id)
);

/*==============================================================*/
/* Table: t_user_role                                           */
/*==============================================================*/
create table t_user_role
(
   id                   int(11) not null auto_increment,
   userid               int(11),
   roleid               int(11),
   primary key (id)
);

alter table t_project_tag add constraint FK_Reference_7 foreign key (projectid)
      references t_project (id) on delete restrict on update restrict;

alter table t_project_tag add constraint FK_Reference_8 foreign key (tagid)
      references t_tag (id) on delete restrict on update restrict;

alter table t_project_type add constraint FK_Reference_5 foreign key (projectid)
      references t_project (id) on delete restrict on update restrict;

alter table t_project_type add constraint FK_Reference_6 foreign key (typeid)
      references t_type (id) on delete restrict on update restrict;

alter table t_role_permission add constraint FK_Reference_3 foreign key (roleid)
      references t_role (id) on delete restrict on update restrict;

alter table t_role_permission add constraint FK_Reference_4 foreign key (permissionid)
      references t_permission (id) on delete restrict on update restrict;

alter table t_user_role add constraint FK_Reference_1 foreign key (userid)
      references t_user (id) on delete restrict on update restrict;

alter table t_user_role add constraint FK_Reference_2 foreign key (roleid)
      references t_role (id) on delete restrict on update restrict;

 

编写mbg配置文件

<?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>
	
	<!-- 
	MyBatis3Simple:基础班CRUD
	MyBatis3:复杂版CRUD
	 -->
	<context id="DB2Tables" targetRuntime="MyBatis3">
		<commentGenerator>
			<!--  关闭自动生成的注释  -->
			<property name="suppressAllComments" value="true"/>
		</commentGenerator>
		<!-- jdbcConnection:指导连接到哪个数据库 -->
		<jdbcConnection
			driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/zcw"
			userId="root" 
			password="123456">
		</jdbcConnection>

		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!-- javaModelGenerator:生成pojo 
		targetPackage:生成的pojo放在哪个包
		targetProject:放在哪个工程下
		-->
		<javaModelGenerator targetPackage="com.gome.scw.manager.bean"
			targetProject="..\manager-pojo\src\main\java">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>

		<!--sqlMapGenerator:sql映射文件生成器;指定xml生成的地方  -->
		<sqlMapGenerator targetPackage="mybatis.mapper"
			targetProject=".\src\main\resources">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>

		<!-- javaClientGenerator:dao接口生成的地方 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.gome.scw.manager.dao" 
			targetProject=".\src\main\java">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>

		<!-- 
		table:指定要逆向生成哪个数据表
		tableName="t_cat":表名
		domainObjectName="":这个表对应的对象名
		 -->
		<table tableName="t_user" domainObjectName="User"></table>
		<!-- <table tableName="employeedao" domainObjectName="EmployeeDao"></table> -->
		<!-- <table tableName="catfood" domainObjectName="Catfood"></table>-->

	</context>
</generatorConfiguration>

我们选择使用java程序运行mbg生成pojo和dao,创建一个运行mbg的java并运行

package com.gome.scw.manager.dao.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 MBGText {
	public static void main(String[] args) throws Exception {
		
			List<String> warnings = new ArrayList<String>();
	        boolean overwrite = true;
	        File configFile = new File("mbg.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("生成ok了!");
		
	}
}

 

好了,逆向工程生成pojo和dao完成,废了这么大的功夫现在终于可以编写mybatis的配置文件了.xml。

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

<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
  
<configuration>
	<settings>
		<setting name="mapUnderscoreToCamelCase" value="TRUE"/>
		<setting name="lazyLoadingEnabled" value="TRUE"/>
		<setting name="aggressiveLazyLoading" value="false"/>
	</settings>
	
	<!-- 配置分页插件 -->
	<plugins>
		<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
	</plugins>
</configuration>

这样mybatis的部分就配置好了,只需要在spring中添加就可以了。

  1. 整合spring

由于spring需要配置的东西比较多,为了直观与更好的阅读性,我们把spring的配置分为三个xml,在web模块中添加spring的配置文件spring-beans.xml、spring-mybatis.xml和spring-tx.xml三个配置文件,spring-beans.xml用于包扫描与添加组件、spring-mybatis.xml用于添加mybatis与其整合、spring-tx.xml用于事务控制;

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	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">
<!-- 用于包扫描,添加组件等 -->
	<!-- 1,包扫描不扫描控制器 -->
	<context:component-scan base-package="com.gome.scw.manager">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- 2,添加数据库连接池 -->
	<bean id="ds" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/zcw"></property>
		<property name="username" value="root"></property>
		<property name="password" value="123456"></property>
		
	</bean>

</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	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.xsd">

	<!-- 配置用mybatis操作数据库 -->
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 指定全局配置文件的位置 -->
		<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
		<!-- 指定数据源 -->
		<property name="dataSource" ref="ds"></property>
		<!-- 指定所以mapperxml配置文件的位置 -->
		<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property>
	</bean>
	
	<!-- 将所有mapper接口的实现类加入到ioc容器中 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.gome.scw.manager.dao"></property>
	</bean>
</beans>

 

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

<!-- 配置事务 -->
	<!-- 1,配置事务管理器 -->
	<bean id="tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="ds"></property>
	</bean>
	
	<!-- 2,配置事务切面-->
	<aop:config>
		<!-- 切入点表达式 -->
		<aop:pointcut expression="execution(* com.gome.scw.manager.service.*.*(..))" id="txPoint"/>
		
		<!-- 事务增强也叫事务建议 -->
		<aop:advisor advice-ref="myAdvice" pointcut-ref="txPoint"/>
	</aop:config>
	
	<!-- 3,配置事务传播特效 -->
	<tx:advice id="myAdvice" transaction-manager="tm">
		<!-- 事务属性 -->
		<tx:attributes>
			<!-- 所以方法都可作为事务 -->
			<tx:method name="*"/>
			<!-- 支持以get开头的方法作为事务,没有事务也没关系(SUPPORTS),并且只读 -->
			<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
		</tx:attributes>
	</tx:advice>
</beans>

 

 

 

好了,spring的配置文件写好了,只需要在web.xml中加入spring的配置文件就可以了;

 

 

  1. 整合springMVC

编写springmvc.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	<!-- 关闭默认的倒入所有的包 -->
	<context:component-scan base-package="com.gome.scw.manager" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="./WEB-INF/jsps/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- 映射动态资源,开启开挂模式 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- 映射静态资源,将静态资源交给tomcat -->
	<mvc:default-servlet-handler/>
</beans>

 最后修改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">

<!-- needed for ContextLoaderListener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-*.xml</param-value>
	</context-param>

	<!-- Bootstraps the root web application context before servlet initialization -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<!-- 添加字符编码过滤器 -->
	<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>forceRequestEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
		<!-- 进行响应乱码解决 -->
		<init-param>
			<param-name>forceResponseEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>characterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- 支持rest风格 -->
	<!-- HiddenHttpMethodFilter过滤器可以将POST请求转化为put请求和delete请求! -->
	<filter>
		<filter-name>hiddenHttpMethodFilter</filter-name>
		<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>hiddenHttpMethodFilter</filter-name>
	   	<url-pattern>/*</url-pattern>
	</filter-mapping>

	
</web-app>

好了,整合完毕。

  • 基础环境整合测试

现在我们来进行测试。

在service模块添加代码 UserService.java和UserServiceImpl.java

package com.gome.scw.manager.service;

import com.gome.scw.manager.bean.User;

public interface UserService {
	public User getUserById(Integer id);
}
package com.gome.scw.manager.service.impl;

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

import com.gome.scw.manager.bean.User;
import com.gome.scw.manager.dao.UserMapper;
import com.gome.scw.manager.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	UserMapper userMapper;

	@Override
	public User getUserById(Integer id) {
		// TODO Auto-generated method stub
		
		return userMapper.selectByPrimaryKey(id);
	}

}

修改web模块中的HelloController.java文件

package com.gome.scw.manager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gome.scw.manager.bean.User;
import com.gome.scw.manager.bean.person;
import com.gome.scw.manager.service.UserService;

@Controller
public class HelloController {
	@Autowired
	UserService userService;

	@RequestMapping(value="/hello")
	public String hello(@RequestParam(value="id",defaultValue="1")
	Integer id, Model dModel) {
		User user = userService.getUserById(id);
		
		dModel.addAttribute("user", user);
		return "success";
	}
}

接着写个web模块中的success.jsp页面,直接输出user的值,需要在pojo模块的user.java中重写tostring()方法;

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
你好!!!!
${user }
</body>
</html>

在数据库中添加测试数据

运行。。:
当当当当。。。。。。

 

 

 

成功!!!!  搞定。

最后特别提示:  

druid数据库连接池的名字不要学错了哦

 

还有配置MVC时,加载controller包的时候 ,关闭默认加载也容易大意出错。。

 

因为一时大意,这两个问题 我足足查了一整天才查到,发现问题时我都要疯啦,犯了这么低级的问题也是没谁了,呵呵呵。。。

最后把我做的工程地址放在这里以备需要时查看https://download.csdn.net/download/qq_25106373/11022543

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值