前言:本文主要介绍利用IDEA如何搭建SSM环境,并使用mybatis的逆向生成功能,根据数据表生成对应mapper接口和sql映射文件。具体步骤如下。

开发环境:

    IDEA 14.1.7

    maven 3.5.2


1.搭建web环境

利用idea搭建web环境的具体步骤,请参考笔者的另一篇博文:使用IntelliJ IDEA和Maven管理搭建Web开发环境(以Spring MVC为例)(一)文中有详细介绍。

2.通过maven添加相关依赖包

搭建ssm环境,需要spring、mybatis、数据库驱动等相关依赖包。具体如下pom文件。

  1    <!--相关文件版本-->
  2     <properties>
  3         <spring.version>4.2.1.RELEASE</spring.version>
  4         <aspectj.version>1.8.1</aspectj.version>
  5         <servlet.version>2.5</servlet.version>
  6         <jstl.version>1.2</jstl.version>
  7         <mybatis.version>3.4.5</mybatis.version>
  8         <mybatis-spring.version>1.3.1</mybatis-spring.version>
  9         <junit.version>4.12</junit.version>
 10         <mysql-driver.version>5.1.20</mysql-driver.version>
 11         <log4j.version>1.2.17</log4j.version>
 12         <slf4j.version>1.7.25</slf4j.version>
 13         <c3p0.version>0.9.0.2</c3p0.version>
 14         <mchange.version>0.2.14</mchange.version>
 15         <mbg.version>1.3.6</mbg.version>
 16     </properties>
 17 
 18     <!--相关依赖-->
 19     <dependencies>
 20         <!--spring相关jar包-->
 21         <dependency>
 22             <groupId>org.springframework</groupId>
 23             <artifactId>spring-core</artifactId>
 24             <version>${spring.version}</version>
 25         </dependency>
 26         <dependency>
 27             <groupId>org.springframework</groupId>
 28             <artifactId>spring-context-support</artifactId>
 29             <version>${spring.version}</version>
 30         </dependency>
 31 
 32         <dependency>
 33             <groupId>org.springframework</groupId>
 34             <artifactId>spring-jdbc</artifactId>
 35             <version>${spring.version}</version>
 36         </dependency>
 37 
 38         <dependency>
 39             <groupId>org.springframework</groupId>
 40             <artifactId>spring-tx</artifactId>
 41             <version>${spring.version}</version>
 42         </dependency>
 43 
 44         <dependency>
 45             <groupId>org.springframework</groupId>
 46             <artifactId>spring-web</artifactId>
 47             <version>${spring.version}</version>
 48         </dependency>
 49 
 50         <dependency>
 51             <groupId>org.springframework</groupId>
 52             <artifactId>spring-webmvc</artifactId>
 53             <version>${spring.version}</version>
 54         </dependency>
 55 
 56         <dependency>
 57             <groupId>org.aspectj</groupId>
 58             <artifactId>aspectjweaver</artifactId>
 59             <version>${aspectj.version}</version>
 60         </dependency>
 61 
 62         <!--mybatis依赖包-->
 63         <dependency>
 64             <groupId>org.mybatis</groupId>
 65             <artifactId>mybatis</artifactId>
 66             <version>${mybatis.version}</version>
 67         </dependency>
 68         <!--mybatis整合spring依赖包-->
 69         <dependency>
 70             <groupId>org.mybatis</groupId>
 71             <artifactId>mybatis-spring</artifactId>
 72             <version>${mybatis-spring.version}</version>
 73         </dependency>
 74         <!--数据源依赖-->
 75         <dependency>
 76             <groupId>c3p0</groupId>
 77             <artifactId>c3p0</artifactId>
 78             <version>${c3p0.version}</version>
 79         </dependency>
 80 
 81         <dependency>
 82             <groupId>com.mchange</groupId>
 83             <artifactId>mchange-commons-java</artifactId>
 84             <version>${mchange.version}</version>
 85         </dependency>
 86         <!--mysql驱动依赖-->
 87         <dependency>
 88             <groupId>mysql</groupId>
 89             <artifactId>mysql-connector-java</artifactId>
 90             <version>${mysql-driver.version}</version>
 91         </dependency>
 92 
 93         <!-- servlet相关依赖-->
 94         <dependency>
 95             <groupId>javax.servlet</groupId>
 96             <artifactId>servlet-api</artifactId>
 97             <version>${servlet.version}</version>
 98         </dependency>
 99 
100         <dependency>
101             <groupId>javax.servlet</groupId>
102             <artifactId>jstl</artifactId>
103             <version>${jstl.version}</version>
104         </dependency>
105         <!--日志依赖-->
106         <dependency>
107             <groupId>log4j</groupId>
108             <artifactId>log4j</artifactId>
109             <version>${log4j.version}</version>
110         </dependency>
111 
112         <dependency>
113             <groupId>org.slf4j</groupId>
114             <artifactId>slf4j-log4j12</artifactId>
115             <version>${slf4j.version}</version>
116         </dependency>
117 
118         <!--junit测试包依赖-->
119         <dependency>
120             <groupId>junit</groupId>
121             <artifactId>junit</artifactId>
122             <version>${junit.version}</version>
123         </dependency>
124     </dependencies>
125     <build>
126         <plugins>
127             <plugin>
128                 <!--Mybatis-generator插件,用于自动生成Mapper和POJO-->
129                 <groupId>org.mybatis.generator</groupId>
130                 <artifactId>mybatis-generator-maven-plugin</artifactId>
131                 <version>${mbg.version}</version>
132                 <configuration>
133                     <!--配置文件的位置-->
134                     <configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile>
135                     <verbose>true</verbose>
136                     <overwrite>true</overwrite>
137                 </configuration>
138                 <executions>
139                     <execution>
140                         <id>Generate MyBatis Artifacts</id>
141                         <goals>
142                             <goal>generate</goal>
143                         </goals>
144                     </execution>
145                 </executions>
146                 <dependencies>
147                     <dependency>
148                         <groupId>org.mybatis.generator</groupId>
149                         <artifactId>mybatis-generator-core</artifactId>
150                         <version>${mbg.version}</version>
151                     </dependency>
152                 </dependencies>
153             </plugin>
154         </plugins>
155     </build>
View Code

相关文章: