1、基本概念

 

1.1、Spring 

        Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

 

1.2、SpringMVC     

        Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。

 

1.3、MyBatis

       MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。MyBatis是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。

 

 

2、开发环境搭建以及创建Maven Web项目

1.jdk1.8   

2.idea

3.maven    tomcat

 

settings.xml

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <!--
  4 Licensed to the Apache Software Foundation (ASF) under one
  5 or more contributor license agreements.  See the NOTICE file
  6 distributed with this work for additional information
  7 regarding copyright ownership.  The ASF licenses this file
  8 to you under the Apache License, Version 2.0 (the
  9 "License"); you may not use this file except in compliance
 10 with the License.  You may obtain a copy of the License at
 11 
 12     http://www.apache.org/licenses/LICENSE-2.0
 13 
 14 Unless required by applicable law or agreed to in writing,
 15 software distributed under the License is distributed on an
 16 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17 KIND, either express or implied.  See the License for the
 18 specific language governing permissions and limitations
 19 under the License.
 20 -->
 21 
 22 <!--
 23  | This is the configuration file for Maven. It can be specified at two levels:
 24  |
 25  |  1. User Level. This settings.xml file provides configuration for a single user,
 26  |                 and is normally provided in ${user.home}/.m2/settings.xml.
 27  |
 28  |                 NOTE: This location can be overridden with the CLI option:
 29  |
 30  |                 -s /path/to/user/settings.xml
 31  |
 32  |  2. Global Level. This settings.xml file provides configuration for all Maven
 33  |                 users on a machine (assuming they're all using the same Maven
 34  |                 installation). It's normally provided in
 35  |                 ${maven.conf}/settings.xml.
 36  |
 37  |                 NOTE: This location can be overridden with the CLI option:
 38  |
 39  |                 -gs /path/to/global/settings.xml
 40  |
 41  | The sections in this sample file are intended to give you a running start at
 42  | getting the most out of your Maven installation. Where appropriate, the default
 43  | values (values used when the setting is not specified) are provided.
 44  |
 45  |-->
 46 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
 47           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 48           xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
 49   <!-- localRepository
 50    | The path to the local repository maven will use to store artifacts.
 51    |
 52    | Default: ${user.home}/.m2/repository
 53   <localRepository>/path/to/local/repo</localRepository>
 54   -->
 55   <localRepository>D:\Develop\MvnRepo</localRepository>
 56 
 57   <!-- interactiveMode
 58    | This will determine whether maven prompts you when it needs input. If set to false,
 59    | maven will use a sensible default value, perhaps based on some other setting, for
 60    | the parameter in question.
 61    |
 62    | Default: true
 63   <interactiveMode>true</interactiveMode>
 64   -->
 65 
 66   <!-- offline
 67    | Determines whether maven should attempt to connect to the network when executing a build.
 68    | This will have an effect on artifact downloads, artifact deployment, and others.
 69    |
 70    | Default: false
 71   <offline>false</offline>
 72   -->
 73 
 74   <!-- pluginGroups
 75    | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
 76    | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
 77    | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
 78    |-->
 79   <pluginGroups>
 80     <!-- pluginGroup
 81      | Specifies a further group identifier to use for plugin lookup.
 82     <pluginGroup>com.your.plugins</pluginGroup>
 83     -->
 84   </pluginGroups>
 85 
 86   <!-- proxies
 87    | This is a list of proxies which can be used on this machine to connect to the network.
 88    | Unless otherwise specified (by system property or command-line switch), the first proxy
 89    | specification in this list marked as active will be used.
 90    |-->
 91   <proxies>
 92     <!-- proxy
 93      | Specification for one proxy, to be used in connecting to the network.
 94      |
 95     <proxy>
 96       <id>optional</id>
 97       <active>true</active>
 98       <protocol>http</protocol>
 99       <username>proxyuser</username>
100       <password>proxypass</password>
101       <host>proxy.host.net</host>
102       <port>80</port>
103       <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
104     </proxy>
105     -->
106   </proxies>
107 
108   <!-- servers
109    | This is a list of authentication profiles, keyed by the server-id used within the system.
110    | Authentication profiles can be used whenever maven must make a connection to a remote server.
111    |-->
112   <servers>
113     <!-- server
114      | Specifies the authentication information to use when connecting to a particular server, identified by
115      | a unique name within the system (referred to by the 'id' attribute below).
116      |
117      | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
118      |       used together.
119      |
120     <server>
121       <id>deploymentRepo</id>
122       <username>repouser</username>
123       <password>repopwd</password>
124     </server>
125     -->
126 
127     <!-- Another sample, using keys to authenticate.
128     <server>
129       <id>siteServer</id>
130       <privateKey>/path/to/private/key</privateKey>
131       <passphrase>optional; leave empty if not used.</passphrase>
132     </server>
133     -->
134   </servers>
135 
136   <!-- mirrors
137    | This is a list of mirrors to be used in downloading artifacts from remote repositories.
138    |
139    | It works like this: a POM may declare a repository to use in resolving certain artifacts.
140    | However, this repository may have problems with heavy traffic at times, so people have mirrored
141    | it to several places.
142    |
143    | That repository definition will have a unique id, so we can create a mirror reference for that
144    | repository, to be used as an alternate download site. The mirror site will be the preferred
145    | server for that repository.
146    |-->
147   <mirrors>
148     <mirror>  
149       <id>nexus-aliyun</id>
150       <mirrorOf>central</mirrorOf>
151       <name>Nexus aliyun</name>
152       <url>http://maven.aliyun.com/nexus/content/repositories/central</url>        
153     </mirror>  
154     <!-- mirror
155      | Specifies a repository mirror site to use instead of a given repository. The repository that
156      | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
157      | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
158      |
159     <mirror>
160       <id>mirrorId</id>
161       <mirrorOf>repositoryId</mirrorOf>
162       <name>Human Readable Name for this Mirror.</name>
163       <url>http://my.repository.com/repo/path</url>
164     </mirror>
165      -->
166   </mirrors>
167 
168   <!-- profiles
169    | This is a list of profiles which can be activated in a variety of ways, and which can modify
170    | the build process. Profiles provided in the settings.xml are intended to provide local machine-
171    | specific paths and repository locations which allow the build to work in the local environment.
172    |
173    | For example, if you have an integration testing plugin - like cactus - that needs to know where
174    | your Tomcat instance is installed, you can provide a variable here such that the variable is
175    | dereferenced during the build process to configure the cactus plugin.
176    |
177    | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
178    | section of this document (settings.xml) - will be discussed later. Another way essentially
179    | relies on the detection of a system property, either matching a particular value for the property,
180    | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
181    | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
182    | Finally, the list of active profiles can be specified directly from the command line.
183    |
184    | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
185    |       repositories, plugin repositories, and free-form properties to be used as configuration
186    |       variables for plugins in the POM.
187    |
188    |-->
189   <profiles>
190     <!-- profile
191      | Specifies a set of introductions to the build process, to be activated using one or more of the
192      | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
193      | or the command line, profiles have to have an ID that is unique.
194      |
195      | An encouraged best practice for profile identification is to use a consistent naming convention
196      | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
197      | This will make it more intuitive to understand what the set of introduced profiles is attempting
198      | to accomplish, particularly when you only have a list of profile id's for debug.
199      |
200      | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
201     <profile>
202       <id>jdk-1.4</id>
203 
204       <activation>
205         <jdk>1.4</jdk>
206       </activation>
207 
208       <repositories>
209         <repository>
210           <id>jdk14</id>
211           <name>Repository for JDK 1.4 builds</name>
212           <url>http://www.myhost.com/maven/jdk14</url>
213           <layout>default</layout>
214           <snapshotPolicy>always</snapshotPolicy>
215         </repository>
216       </repositories>
217     </profile>
218     -->
219 
220     <!--
221      | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
222      | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
223      | might hypothetically look like:
224      |
225      | ...
226      | <plugin>
227      |   <groupId>org.myco.myplugins</groupId>
228      |   <artifactId>myplugin</artifactId>
229      |
230      |   <configuration>
231      |     <tomcatLocation>${tomcatPath}</tomcatLocation>
232      |   </configuration>
233      | </plugin>
234      | ...
235      |
236      | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
237      |       anything, you could just leave off the <value/> inside the activation-property.
238      |
239     <profile>
240       <id>env-dev</id>
241 
242       <activation>
243         <property>
244           <name>target-env</name>
245           <value>dev</value>
246         </property>
247       </activation>
248 
249       <properties>
250         <tomcatPath>/path/to/tomcat/instance</tomcatPath>
251       </properties>
252     </profile>
253     -->
254   </profiles>
255 
256   <!-- activeProfiles
257    | List of profiles that are active for all builds.
258    |
259   <activeProfiles>
260     <activeProfile>alwaysActiveProfile</activeProfile>
261     <activeProfile>anotherAlwaysActiveProfile</activeProfile>
262   </activeProfiles>
263   -->
264 </settings>
settings.xml

 

3.开始建立maven工程

 

 

 

配置正确的话就会出现    刚开始配maven工程可能会久一点

 

  3.1 在pom.xml中引入jar包

 

 

 

 之后的内容修改一下就好了,我把pom.xml文件放上

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5   <modelVersion>4.0.0</modelVersion>
  6   <groupId>com</groupId>
  7   <artifactId>blgg</artifactId>
  8   <version>1.0-SNAPSHOT</version>
  9   <packaging>war</packaging>
 10   <name>tables</name>
 11 
 12 
 13   <properties>
 14     <spring.version>4.1.4.RELEASE</spring.version>
 15     <mybatis.version>3.2.8</mybatis.version>
 16     <slf4j.version>1.7.7</slf4j.version>
 17     <log4j.version>1.2.17</log4j.version>
 18     <quartz.version>2.2.2</quartz.version>
 19     <activiti.version>5.15.1</activiti.version>
 20   </properties>
 21 
 22 
 23   <dependencies>
 24 
 25     <dependency>
 26       <groupId>org.springframework</groupId>
 27       <artifactId>spring-core</artifactId>
 28       <version>${spring.version}</version>
 29     </dependency>
 30     <dependency>
 31       <groupId>org.springframework</groupId>
 32       <artifactId>spring-context</artifactId>
 33       <version>${spring.version}</version>
 34     </dependency>
 35     <!-- 添加spring-tx包 -->
 36     <dependency>
 37       <groupId>org.springframework</groupId>
 38       <artifactId>spring-tx</artifactId>
 39       <version>${spring.version}</version>
 40     </dependency>
 41     <!-- 添加spring-jdbc包 -->
 42     <dependency>
 43       <groupId>org.springframework</groupId>
 44       <artifactId>spring-jdbc</artifactId>
 45       <version>${spring.version}</version>
 46     </dependency>
 47     <!-- 为了方便进行单元测试,添加spring-test包 -->
 48     <dependency>
 49       <groupId>org.springframework</groupId>
 50       <artifactId>spring-test</artifactId>
 51       <version>${spring.version}</version>
 52     </dependency>
 53     <!--添加spring-web包 -->
 54     <dependency>
 55       <groupId>org.springframework</groupId>
 56       <artifactId>spring-web</artifactId>
 57       <version>${spring.version}</version>
 58     </dependency>
 59     <dependency>
 60       <groupId>org.springframework</groupId>
 61       <artifactId>spring-webmvc</artifactId>
 62       <version>${spring.version}</version>
 63     </dependency>
 64     <dependency>
 65       <groupId>org.springframework</groupId>
 66       <artifactId>spring-aop</artifactId>
 67       <version>${spring.version}</version>
 68     </dependency>
 69     <dependency>
 70       <groupId>org.springframework</groupId>
 71       <artifactId>spring-context-support</artifactId>
 72       <version>${spring.version}</version>
 73     </dependency>
 74     <!--添加aspectjweaver包 -->
 75     <dependency>
 76       <groupId>org.aspectj</groupId>
 77       <artifactId>aspectjweaver</artifactId>
 78       <version>1.8.5</version>
 79     </dependency>
 80     <!-- 添加mybatis的核心包 -->
 81     <dependency>
 82       <groupId>org.mybatis</groupId>
 83       <artifactId>mybatis</artifactId>
 84       <version>${mybatis.version}</version>
 85     </dependency>
 86     <!-- 添加mybatis与Spring整合的核心包 -->
 87     <dependency>
 88       <groupId>org.mybatis</groupId>
 89       <artifactId>mybatis-spring</artifactId>
 90       <version>1.2.2</version>
 91     </dependency>
 92     <!-- 添加servlet3.0核心包 -->
 93     <dependency>
 94       <groupId>javax.servlet</groupId>
 95       <artifactId>javax.servlet-api</artifactId>
 96       <version>3.0.1</version>
 97     </dependency>
 98     <dependency>
 99       <groupId>javax.servlet.jsp</groupId>
100       <artifactId>javax.servlet.jsp-api</artifactId>
101       <version>2.3.2-b01</version>
102     </dependency>
103     <!-- jstl -->
104     <dependency>
105       <groupId>javax.servlet</groupId>
106       <artifactId>jstl</artifactId>
107       <version>1.2</version>
108     </dependency>
109     <!-- 添加mysql驱动包 -->
110     <dependency>
111       <groupId>mysql</groupId>
112       <artifactId>mysql-connector-java</artifactId>
113       <version>5.1.34</version>
114     </dependency>
115     <!-- 添加druid连接池包 -->
116     <dependency>
117       <groupId>com.alibaba</groupId>
118       <artifactId>druid</artifactId>
119       <version>1.0.12</version>
120     </dependency>
121     <!-- 日志文件管理包 -->
122     <!-- log start -->
123     <dependency>
124       <groupId>log4j</groupId>
125       <artifactId>log4j</artifactId>
126       <version>${log4j.version}</version>
127     </dependency>
128 
129 
130     <!-- 格式化对象,方便输出日志 -->
131     <dependency>
132       <groupId>com.alibaba</groupId>
133       <artifactId>fastjson</artifactId>
134       <version>1.1.41</version>
135     </dependency>
136 
137 
138     <dependency>
139       <groupId>org.slf4j</groupId>
140       <artifactId>slf4j-api</artifactId>
141       <version>${slf4j.version}</version>
142     </dependency>
143 
144     <dependency>
145       <groupId>org.slf4j</groupId>
146       <artifactId>slf4j-log4j12</artifactId>
147       <version>${slf4j.version}</version>
148     </dependency>
149     <!-- log end -->
150     <!--apache shiro  -->
151     <dependency>
152       <groupId>org.apache.shiro</groupId>
153       <artifactId>shiro-core</artifactId>
154       <version>1.2.2</version>
155     </dependency>
156     <dependency>
157       <groupId>org.apache.shiro</groupId>
158       <artifactId>shiro-web</artifactId>
159       <version>1.2.2</version>
160     </dependency>
161     <dependency>
162       <groupId>org.apache.shiro</groupId>
163       <artifactId>shiro-ehcache</artifactId>
164       <version>1.2.2</version>
165     </dependency>
166     <dependency>
167       <groupId>org.apache.shiro</groupId>
168       <artifactId>shiro-spring</artifactId>
169       <version>1.2.2</version>
170     </dependency>
171     <dependency>
172       <groupId>org.apache.shiro</groupId>
173       <artifactId>shiro-quartz</artifactId>
174       <version>1.2.2</version>
175     </dependency>
176     <!--apache shiro  end-->
177     <!-- 映入JSON -->
178     <dependency>
179       <groupId>org.codehaus.jackson</groupId>
180       <artifactId>jackson-mapper-asl</artifactId>
181       <version>1.9.13</version>
182     </dependency>
183     <!-- 上传组件包 -->
184     <dependency>
185       <groupId>commons-fileupload</groupId>
186       <artifactId>commons-fileupload</artifactId>
187       <version>1.3.1</version>
188     </dependency>
189     <dependency>
190       <groupId>commons-io</groupId>
191       <artifactId>commons-io</artifactId>
192       <version>2.4</version>
193     </dependency>
194     <dependency>
195       <groupId>commons-codec</groupId>
196       <artifactId>commons-codec</artifactId>
197       <version>1.9</version>
198     </dependency>
199     <dependency>
200       <groupId>org.quartz-scheduler</groupId>
201       <artifactId>quartz</artifactId>
202       <version>${quartz.version}</version>
203     </dependency>
204     <dependency>
205       <groupId>org.apache.velocity</groupId>
206       <artifactId>velocity</artifactId>
207       <version>1.7</version>
208     </dependency>
209     <dependency>
210       <groupId>org.apache.velocity</groupId>
211       <artifactId>velocity-tools</artifactId>
212       <version>2.0</version>
213     </dependency>
214 
215     <dependency>
216       <groupId>org.mybatis.generator</groupId>
217       <artifactId>mybatis-generator-core</artifactId>
218       <version>1.3.2</version>
219       <scope>test</scope>
220     </dependency>
221     <!--activity工作流依赖-->
222     <dependency>
223       <groupId>org.activiti</groupId>
224       <artifactId>activiti-engine</artifactId>
225       <version>${activiti.version}</version>
226     </dependency>
227     <!-- activiti 与 Spring 集成 -->
228     <dependency>
229       <groupId>org.activiti</groupId>
230       <artifactId>activiti-spring</artifactId>
231       <version>${activiti.version}</version>
232     </dependency>
233   </dependencies>
234 
235 
236   <build>
237     <finalName>blgg</finalName>
238 
239     <resources>
240       <resource>
241         <directory>src/main/java</directory>
242         <includes>
243           <include>**/*.xml</include>
244         </includes>
245       </resource>
246     </resources>
247 
248     <plugins>
249       <plugin>
250         <groupId>org.mybatis.generator</groupId>
251         <artifactId>mybatis-generator-maven-plugin</artifactId>
252         <version>1.3.2</version>
253         <configuration>
254           <verbose>true</verbose>
255           <overwrite>true</overwrite>
256         </configuration>
257       </plugin>
258       <plugin>
259         <groupId>org.apache.maven.plugins</groupId>
260         <artifactId>maven-compiler-plugin</artifactId>
261         <configuration>
262           <source>1.8</source>
263           <target>1.8</target>
264         </configuration>
265       </plugin>
266     </plugins>
267   </build>
268 
269 </project>
pom.xml

 

 

 成功导入jar就是这样,如果没有就点击idea右下角右边的按钮Enable import

 

3.2 目录结构配置

在src-main目录创建一个java和resource

 

并且在左上角File-Project Structure-Modules-Source

 

 

 

之后apply-ok一下就好了  

 

 

3.2 整合ssm

3.2.1 配置spring-mvc.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns:context="http://www.springframework.org/schema/context"
 3        xmlns:mvc="http://www.springframework.org/schema/mvc"
 4        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 7       http://www.springframework.org/schema/context
 8       http://www.springframework.org/schema/context/spring-context.xsd
 9       http://www.springframework.org/schema/mvc
10       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
11 
12     <mvc:annotation-driven>
13         <mvc:message-converters>
14             <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
15             <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
16                 <property name="supportedMediaTypes">
17                     <list>
18                         <value>text/html;charset=UTF-8</value>
19                         <value>application/json;charset=UTF-8</value>  <!-- JSON转换器 -->
20                     </list>
21                 </property>
22             </bean>
23         </mvc:message-converters>
24     </mvc:annotation-driven>
25     
26     <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
27     <context:component-scan base-package="com.blgg.controller"/>
28     <!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 -->
29     <mvc:annotation-driven/>
30     <!-- 静态资源处理  css js imgs -->
31     <mvc:default-servlet-handler/>
32     
33     <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
34     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
35         <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
36         <property name="prefix" value="/WEB-INF/views/" />
37         <property name="suffix" value=".vm" />
38     </bean>
39     
40 </beans>

3.2.2、配置web.xml文件


 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xmlns="http://java.sun.com/xml/ns/javaee"
 4          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 5          version="3.0">
 6   <display-name>tables</display-name>
 7 
 8     <!-- Spring和mybatis的配置文件 -->
 9     <context-param>
10        <param-name>contextConfigLocation</param-name>
11        <param-value>classpath:spring-mybatis.xml</param-value>
12    </context-param>
13 
14   <!-- 编码过滤器 -->
15   <filter>
16     <filter-name>encodingFilter</filter-name>
17     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
18     <async-supported>true</async-supported>
19     <init-param>
20       <param-name>encoding</param-name>
21       <param-value>UTF-8</param-value>
22     </init-param>
23   </filter>
24   <filter-mapping>
25     <filter-name>encodingFilter</filter-name>
26     <url-pattern>/*</url-pattern>
27   </filter-mapping>
28 
29   <!-- Spring监听器 -->
30   <listener>
31     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
32   </listener>
33 
34   <!-- Spring MVC servlet -->
35   <servlet>
36     <servlet-name>SpringMVC</servlet-name>
37     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
38     <init-param>
39       <param-name>contextConfigLocation</param-name>
40       <param-value>classpath:spring-mvc.xml</param-value>
41     </init-param>
42     <load-on-startup>1</load-on-startup>
43     <async-supported>true</async-supported>
44   </servlet>
45   <servlet-mapping>
46     <servlet-name>SpringMVC</servlet-name>
47     <url-pattern>/</url-pattern>
48   </servlet-mapping>
49   
50   <servlet-mapping>
51     <servlet-name>default</servlet-name>
52     <url-pattern>/static/*</url-pattern>
53   </servlet-mapping>
54 
55 </web-app>

 

 

 

3.2.3、Log4j的配置

 1 log4j相关配置
 2 ######################################
 3 #日志输出级别
 4 log4j.rootLogger=DEBUG,stdout,other
 5 #设置stdout的日志输出控制台
 6 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
 7 #输出日志到控制台的方式,默认为System.out
 8 log4j.appender.stdout.Target=System.out
 9 #设置使用灵活布局
10 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
11 #灵活定义输出格式
12 log4j.appender.stdout.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH:mm:ss}] %l %m %n
13 #设置other的日志输出控制台
14 log4j.appender.other=org.apache.log4j.RollingFileAppender
15 #设置other的输出日志
16 log4j.appender.other.File=/tables/logs/log.txt
17 #设置other的日志最大限制
18 log4j.appender.other.MaxFileSize=1024KB
19 #最多只保存20个备份文件
20 log4j.appender.other.MaxBackupIndex=1000  
21 #输出INFO级别以上的日志
22 log4j.appender.other.Threshold=INFO
23 #设置使用灵活布局
24 log4j.appender.other.layout=org.apache.log4j.PatternLayout  
25 #灵活定义输出格式
26 log4j.appender.other.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH:mm:ss}] %l %t %m %n    
27 
28 ###显示SQL语句部分
29 log4j.logger.com.mybatis=DEBUG
30 log4j.logger.com.mybatis.common.jdbc.SimpleDataSource=DEBUG
31 log4j.logger.com.mybatis.common.jdbc.ScriptRunner=DEBUG
32 log4j.logger.com.mybatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
33 log4j.logger.java.sql.Connection=DEBUG
34 log4j.logger.java.sql.Statement=DEBUG
35 log4j.logger.java.sql.PreparedStatement=DEBUG
36 log4j.logger.java.sql.ResultSet=DEBUG  

 

3.3 Spring与MyBatis的整合

3.3.1、建立JDBC属性文件

jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/blgg?useUnicode=true&characterEncoding=UTF-8
username=root
password=root

 

3.3.2、建立spring-mybatis.xml配置文件

取消3.2.2 web.xml中注释的代码 

 


 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:aop="http://www.springframework.org/schema/aop"
 6        xmlns:tx="http://www.springframework.org/schema/tx"
 7        xsi:schemaLocation="http://www.springframework.org/schema/beans
 8     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 9     http://www.springframework.org/schema/context
10     http://www.springframework.org/schema/context/spring-context-4.0.xsd
11     http://www.springframework.org/schema/aop
12     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
13     http://www.springframework.org/schema/tx
14     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
15 
16     <!--开启切面编程自动代理-->
17     <aop:aspectj-autoproxy proxy-target-class="true"/>
18     <!--包扫描-->
19     <context:component-scan base-package="com.blgg"/>
20     <!--扫描注解生成bean-->
21     <context:annotation-config/>
22 
23     <context:property-placeholder location="classpath:jdbc.properties"/>
24 
25     <!--数据库设置-->
26     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
27           destroy-method="close" init-method="init">
28         <property name="url" value="${url}"/>
29         <property name="username" value="${username}"/>
30         <property name="password" value="${password}"/>
31         <!-- 初始化连接大小 -->
32         <property name="initialSize" value="0"/>
33         <!-- 连接池最大使用连接数量 -->
34         <property name="maxActive" value="20"/>
35         <!-- 连接池最小空闲 -->
36         <property name="minIdle" value="0"/>
37         <!-- 获取连接最大等待时间 -->
38         <property name="maxWait" value="60000"/>
39         <!--
40         <property name="poolPreparedStatements" value="true" />
41         <property name="maxPoolPreparedStatementPerConnectionSize" value="33" />
42         -->
43         <property name="testOnBorrow" value="false"/>
44         <property name="testOnReturn" value="false"/>
45         <property name="testWhileIdle" value="true"/>
46         <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
47         <property name="timeBetweenEvictionRunsMillis" value="60000"/>
48         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
49         <property name="minEvictableIdleTimeMillis" value="25200000"/>
50         <!-- 打开removeAbandoned功能 -->
51         <property name="removeAbandoned" value="true"/>
52         <!-- 1800秒,也就是30分钟 -->
53         <property name="removeAbandonedTimeout" value="1800"/>
54         <!-- 关闭abanded连接时输出错误日志 -->
55         <property name="logAbandoned" value="true"/>
56         <!-- 监控数据库 -->
57         <!-- <property name="filters" value="stat" /> -->
58         <property name="filters" value="mergeStat"/>
59     </bean>
60 
61     <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
62     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
63         <property name="dataSource" ref="dataSource" />
64         <!-- 自动扫描mapping.xml文件 -->
65         <property name="mapperLocations" value="classpath:com/blgg/dao/*.xml"/>
66     </bean>
67 
68     <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
69     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
70         <property name="basePackage" value="com.blgg.dao" />
71         <property name="sqlSessionFactoryBeanName"  value="sqlSessionFactory"></property>
72     </bean>
73 
74     <!--声明事务管理 采用注解方式-->
75     <tx:annotation-driven transaction-manager="transactionManager"/>
76     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
77         <property name="dataSource" ref="dataSource"/>
78     </bean>
79 </beans>

 

 

3.4 包目录

3.4.1数据库表 student

 

 

3.4.2利用 Generator自动创建代码

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
 3         "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
 4 <generatorConfiguration>
 5 
 6     <!--classPathEntry:数据库的JDBC驱动 -->
 7     <classPathEntry
 8             location="D:\Develop\MvnRepo\mysql\mysql-connector-java\5.1.34\mysql-connector-java-5.1.34.jar" />
 9 
10     <context id="MysqlTables" targetRuntime="MyBatis3">
11 
12         <!-- 注意这里面的顺序确定的,不能随变更改 -->
13         <!-- 自定义的分页插件 <plugin type="com.deppon.foss.module.helloworld.shared.PaginationPlugin"/> -->
14 
15         <!-- 可选的(0 or 1) -->
16         <!-- 注释生成器 -->
17         <commentGenerator>
18             <!-- 是否去除自动生成的注释 true:是 : false:否 -->
19             <property name="suppressAllComments" value="true" />
20         </commentGenerator>
21 
22         <!-- 必须的(1 required) -->
23         <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
24         <jdbcConnection driverClass="com.mysql.jdbc.Driver"
25                         connectionURL="jdbc:mysql://localhost:3306/blgg"
26                         userId="root" password="root">
27         </jdbcConnection>
28 
29         <!-- 可选的(0 or 1) -->
30         <!-- 类型转换器或者加类型解析器 -->
31         <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和
32             NUMERIC 类型解析为java.math.BigDecimal -->
33         <javaTypeResolver>
34             <property name="forceBigDecimals" value="false" />
35         </javaTypeResolver>
36 
37 
38         <!-- 必须的(1 required) -->
39         <!-- java模型生成器 -->
40         <!-- targetProject:自动生成代码的位置 -->
41         <javaModelGenerator targetPackage="com.blgg.entity"
42                             targetProject="D:\IdeaProjects\tables\src\main\java"
43         >
44             <!-- TODO enableSubPackages:是否让schema作为包的后缀 -->
45             <property name="enableSubPackages" value="true" />
46             <!-- 从数据库返回的值被清理前后的空格 -->
47             <property name="trimStrings" value="true" />
48         </javaModelGenerator>
49 
50         <!-- 必须的(1 required) -->
51         <!-- map xml 生成器 -->
52         <sqlMapGenerator targetPackage="com.blgg.dao"
53                          targetProject="D:\IdeaProjects\tables\src\main\java">
54             <property name="enableSubPackages" value="true" />
55         </sqlMapGenerator>
56 
57         <!-- 可选的(0 or 1) -->
58         <!-- mapper 或者就是dao接口生成器 -->
59         <javaClientGenerator targetPackage="com.blgg.dao"
60                              targetProject="D:\IdeaProjects\tables\src\main\java"
61                              type="XMLMAPPER">
62             <property name="enableSubPackages" value="true" />
63         </javaClientGenerator>
64 
65         <!-- 必须的(1...N) -->
66         <!-- pojo 实体生成器 -->
67         <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->
68         <!-- schema即为数据库名 可不写 -->
69         <table  tableName="teacher" domainObjectName="Teacher"
70                 enableInsert="true" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
71                 enableSelectByExample="false" selectByExampleQueryId="false">
72             <!-- 忽略字段 可选的(0 or 1) -->
73             <!-- <ignoreColumn column="is_use" /> -->
74             <!--//无论字段是什么类型,生成的类属性都是varchar。 可选的(0 or 1) 测试无效 -->
75             <!-- <columnOverride column="city_code" jdbcType="VARCHAR" /> -->
76         </table>
77 
78 
79     </context>
80 </generatorConfiguration>
generator.xml

 

 

 

 

 targetPackage填写包路径    targetProject填写工程路径

 

 

 

 

 

 idea右上角设置一下,然后启动,失败报错的话会提示这个xml文件does not exist,在pom.xml最下面加上具体路径

 

 

 

 

 

 此时我的项目文件目录如下:

 

 

 

 不全的可以去自行创建!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2018-08-17 22:11  蓝绿绿  阅读(779)  评论(0编辑  收藏  举报