java搭建 SpringMVC+Mybatis(SMM)+mybatis-generate

搭建SSM系统,首先要了解整个过程:

1、创建spring-mvc项目

2、在maven中添加要引用的jar包(使用框架都是较新的版本:)

3、 jdbc.xml +spring-mybatis.xml的配置,

4、Log4j的配置

5、配置 spring-mvc.xml+web.xml

6、配置 generatorConfig.xml

7、测试及注意事项

 

1、创建项目,在这里首先给出资源文件结构图(红色圈中的为下面要添加修改的文件)

java搭建 SpringMVC+Mybatis(SMM)+mybatis-generate_第1张图片

2、Maven引入需要的JAR包

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3     <modelVersion>4.0.0</modelVersion>
  4     <groupId>com.springapp</groupId>
  5     <artifactId>SSM</artifactId>
  6     <packaging>war</packaging>
  7     <version>1.0-SNAPSHOT</version>
  8     <name>SSM</name>
  9 
 10     <properties>
 11         <spring.version>4.1.1.RELEASE</spring.version>
 12         <mybatis.version>3.2.6</mybatis.version>
 13         <!-- log4j日志文件管理包版本 -->
 14         <slf4j.version>1.7.7</slf4j.version>
 15         <log4j.version>1.2.17</log4j.version>
 16         <logisticTools.version>1.1.1-SNAPSHOT</logisticTools.version>
 17     </properties>
 18 
 19     <dependencies>
 20         <dependency>
 21             <groupId>org.springframework</groupId>
 22             <artifactId>spring-core</artifactId>
 23             <version>${spring.version}</version>
 24         </dependency>
 25 
 26         <dependency>
 27             <groupId>org.springframework</groupId>
 28             <artifactId>spring-web</artifactId>
 29             <version>${spring.version}</version>
 30         </dependency>
 31 
 32         <dependency>
 33             <groupId>javax.servlet</groupId>
 34             <artifactId>servlet-api</artifactId>
 35             <version>2.5</version>
 36         </dependency>
 37 
 38         <dependency>
 39             <groupId>javax.servlet.jsp</groupId>
 40             <artifactId>jsp-api</artifactId>
 41             <version>2.1</version>
 42             <scope>provided</scope>
 43         </dependency>
 44 
 45         <dependency>
 46             <groupId>org.springframework</groupId>
 47             <artifactId>spring-webmvc</artifactId>
 48             <version>${spring.version}</version>
 49         </dependency>
 50 
 51         <dependency>
 52             <groupId>org.springframework</groupId>
 53             <artifactId>spring-test</artifactId>
 54             <version>${spring.version}</version>
 55             <scope>test</scope>
 56         </dependency>
 57 
 58         <dependency>
 59             <groupId>junit</groupId>
 60             <artifactId>junit</artifactId>
 61             <version>4.11</version>
 62             <scope>test</scope>
 63         </dependency>
 64 
 65         <!-- spring核心包 -->
 66         <dependency>
 67             <groupId>org.springframework</groupId>
 68             <artifactId>spring-core</artifactId>
 69             <version>${spring.version}</version>
 70         </dependency>
 71 
 72         <dependency>
 73             <groupId>org.springframework</groupId>
 74             <artifactId>spring-web</artifactId>
 75             <version>${spring.version}</version>
 76         </dependency>
 77         <dependency>
 78             <groupId>org.springframework</groupId>
 79             <artifactId>spring-oxm</artifactId>
 80             <version>${spring.version}</version>
 81         </dependency>
 82         <dependency>
 83             <groupId>org.springframework</groupId>
 84             <artifactId>spring-tx</artifactId>
 85             <version>${spring.version}</version>
 86         </dependency>
 87 
 88         <dependency>
 89             <groupId>org.springframework</groupId>
 90             <artifactId>spring-jdbc</artifactId>
 91             <version>${spring.version}</version>
 92         </dependency>
 93 
 94         <dependency>
 95             <groupId>org.springframework</groupId>
 96             <artifactId>spring-webmvc</artifactId>
 97             <version>${spring.version}</version>
 98         </dependency>
 99         <dependency>
100             <groupId>org.springframework</groupId>
101             <artifactId>spring-aop</artifactId>
102             <version>${spring.version}</version>
103         </dependency>
104 
105         <dependency>
106             <groupId>org.springframework</groupId>
107             <artifactId>spring-context-support</artifactId>
108             <version>${spring.version}</version>
109         </dependency>
110 
111         <dependency>
112             <groupId>org.springframework</groupId>
113             <artifactId>spring-test</artifactId>
114             <version>${spring.version}</version>
115         </dependency>
116 
117         <!-- mybatis核心包 -->
118         <dependency>
119             <groupId>org.mybatis</groupId>
120             <artifactId>mybatis</artifactId>
121             <version>${mybatis.version}</version>
122         </dependency>
123         <!-- mybatis/spring包 -->
124         <dependency>
125             <groupId>org.mybatis</groupId>
126             <artifactId>mybatis-spring</artifactId>
127             <version>1.2.2</version>
128         </dependency>
129         <!-- 导入java ee jar 包 -->
130         <dependency>
131             <groupId>javax</groupId>
132             <artifactId>javaee-api</artifactId>
133             <version>7.0</version>
134         </dependency>
135         <!-- oracle -->
136         <dependency>
137             <groupId>com.oracle</groupId>
138             <artifactId>ojdbc6</artifactId>
139             <version>11.2.0.1.0</version>
140         </dependency>
141         <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
142         <dependency>
143             <groupId>commons-dbcp</groupId>
144             <artifactId>commons-dbcp</artifactId>
145             <version>1.2.2</version>
146         </dependency>
147         <!-- JSTL标签类 -->
148         <dependency>
149             <groupId>jstl</groupId>
150             <artifactId>jstl</artifactId>
151             <version>1.2</version>
152         </dependency>
153         <!-- 日志文件管理包 -->
154         <!-- log start -->
155         <dependency>
156             <groupId>log4j</groupId>
157             <artifactId>log4j</artifactId>
158             <version>${log4j.version}</version>
159         </dependency>
160 
161 
162         <!-- 格式化对象,方便输出日志 -->
163         <dependency>
164             <groupId>com.alibaba</groupId>
165             <artifactId>fastjson</artifactId>
166             <version>1.1.41</version>
167         </dependency>
168 
169 
170         <dependency>
171             <groupId>org.slf4j</groupId>
172             <artifactId>slf4j-api</artifactId>
173             <version>${slf4j.version}</version>
174         </dependency>
175 
176         <dependency>
177             <groupId>org.slf4j</groupId>
178             <artifactId>slf4j-log4j12</artifactId>
179             <version>${slf4j.version}</version>
180         </dependency>
181         <!-- log end -->
182         <!-- 映入JSON -->
183         <dependency>
184             <groupId>org.codehaus.jackson</groupId>
185             <artifactId>jackson-mapper-asl</artifactId>
186             <version>1.9.13</version>
187         </dependency>
188         <!-- 上传组件包 -->
189         <dependency>
190             <groupId>commons-fileupload</groupId>
191             <artifactId>commons-fileupload</artifactId>
192             <version>1.3.1</version>
193         </dependency>
194         <dependency>
195             <groupId>commons-io</groupId>
196             <artifactId>commons-io</artifactId>
197             <version>2.4</version>
198         </dependency>
199         <dependency>
200             <groupId>commons-codec</groupId>
201             <artifactId>commons-codec</artifactId>
202             <version>1.9</version>
203         </dependency>
204 
205         <!-- jackson -->
206         <dependency>
207             <groupId>org.codehaus.jackson</groupId>
208             <artifactId>jackson-mapper-lgpl</artifactId>
209             <version>1.9.2</version>
210         </dependency>
211     </dependencies>
212 
213     <build>
214         <finalName>SSM</finalName>
215         <plugins>
216             <plugin>
217                 <artifactId>maven-compiler-plugin</artifactId>
218                 <configuration>
219                     <source>1.6</source>
220                     <target>1.6</target>
221                 </configuration>
222             </plugin>
223             <plugin>
224                 <artifactId>maven-surefire-plugin</artifactId>
225                 <configuration>
226                     <includes>
227                         <include>**/*Tests.java</include>
228                     </includes>
229                 </configuration>
230             </plugin>
231             <plugin>
232                 <groupId>org.mybatis.generator</groupId>
233                 <artifactId>mybatis-generator-maven-plugin</artifactId>
234                 <version>1.3.2</version>
235                 <configuration>
236                     <verbose>true</verbose>
237                     <overwrite>true</overwrite>
238                 </configuration>
239             </plugin>
240         </plugins>
241     </build>
242 </project>

3、Spring与MyBatis的整合

3.1 配置数据库连接(jdbc.properties)

1 jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
2 jdbc.url=jdbc:oracle:thin:@数据库地址:orcl
3 jdbc.username=用户名
4 jdbc.password=密码

3.2 建立spring-mybatis.xml配置文件(这个文件就是用来完成spring和mybatis的整合的。这里面也没多少行配置,主要的就是自动扫描,自动注入,配置数据库。注释也很详细,大家看看就明白了。)

 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" xmlns:p="http://www.springframework.org/schema/p"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5         xmlns:tx="http://www.springframework.org/schema/tx"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans
 7                         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 8                         http://www.springframework.org/schema/context
 9                         http://www.springframework.org/schema/context/spring-context-3.1.xsd
10                          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
11 
12     <!-- 引入配置文件 -->
13     <bean id="propertyConfigurer"
14           class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
15         <property name="location" value="classpath:jdbc.properties" />
16     </bean>
17 
18     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
19           destroy-method="close">
20         <property name="driverClassName" value="${jdbc.driverClassName}" />
21         <property name="url" value="${jdbc.url}" />
22         <property name="username" value="${jdbc.username}" />
23         <property name="password" value="${jdbc.password}" />
24     </bean>
25 
26     <!-- Transaction manager for a single JDBC DataSource -->
27     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
28         <property name="dataSource" ref="dataSource"/>
29     </bean>
30 
31     <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
32 
33     <bean id="transactionTemplate"
34           class="org.springframework.transaction.support.TransactionTemplate">
35         <property name="transactionManager" ref="transactionManager"/>
36     </bean>
37 
38     <!-- Activates scanning of @Autowired -->
39     <context:annotation-config/>
40     <context:component-scan base-package="com.huanshare.mapper"/>
41 
42     <!-- scan for mappers and let them be autowired -->
43     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
44         <property name="basePackage" value="com.huanshare.mapper"/>
45         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
46     </bean>
47 
48     <!-- define the SqlSessionFactory -->
49     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
50         <property name="dataSource" ref="dataSource"/>
51         <property name="typeAliasesPackage" value="com.huanshare.mapper"/>
52         <property name="mapperLocations" value="classpath:/mapper/*.xml"></property>
53     </bean> 
54 </beans>

4、配置log4j.properties(Log4j的配置很简单,而且也是通用的,下面给出一个基本的配置,换到其他项目中也无需做多大的调整)

 1 log4j.rootLogger=INFO,Console,File  
 2 #定义日志输出目的地为控制台  
 3 log4j.appender.Console=org.apache.log4j.ConsoleAppender  
 4 log4j.appender.Console.Target=System.out  
 5 #可以灵活地指定日志输出格式,下面一行是指定具体的格式  
 6 log4j.appender.Console.layout = org.apache.log4j.PatternLayout  
 7 log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n  
 8   
 9 #文件大小到达指定尺寸的时候产生一个新的文件  
10 log4j.appender.File = org.apache.log4j.RollingFileAppender  
11 #指定输出目录  
12 log4j.appender.File.File = logs/ssm.log  
13 #定义文件最大大小  
14 log4j.appender.File.MaxFileSize = 10MB  
15 # 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志  
16 log4j.appender.File.Threshold = ALL  
17 log4j.appender.File.layout = org.apache.log4j.PatternLayout  
18 log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n  

5、最后整合

5.1、配置springmvc.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        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6                         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 7                         http://www.springframework.org/schema/context
 8                         http://www.springframework.org/schema/context/spring-context-3.1.xsd">
 9 
10     <context:component-scan base-package="com.huanshare" />
11 
12     <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
13     <bean id="multipartResolver"
14           class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
15         <!-- 默认编码 -->
16         <property name="defaultEncoding" value="utf-8" />
17         <!-- 文件大小最大值 -->
18         <property name="maxUploadSize" value="10485760000" />
19         <!-- 内存中的最大值 -->
20         <property name="maxInMemorySize" value="40960" />
21     </bean>
22 
23 </beans>

5.2、配置mvc-dispatcher-servlet.xml

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3        xmlns:mvc="http://www.springframework.org/schema/mvc"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd
 6          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
 7 
 8 
 9     <!--引入配置文件-->
10     <import resource="classpath:spring-mvc.xml"/>
11     <import resource="classpath:spring-mybatis.xml"/>

    <!--配置页面加载js,css文件--> 12 <mvc:annotation-driven /> 13 <mvc:resources mapping="/statics/**" location="/statics/" />
14 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 15 <property name="prefix" value="/WEB-INF/pages/"/> 16 <property name="suffix" value=".jsp"/> 17 </bean> 18 </beans>

5.3、配置web.xml

<web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring MVC Application</display-name>
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--配置编码UTF-8-->
    <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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

6、配置 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>
    <!-- C:/Users/jun.li/.m2/repository C:/Documents and Settings/alan/.m2/repository/mysql/mysql-connector-java/5.1.9/mysql-connector-java-5.1.9.jar
        C:/Users/zhiqiang.ma/.m2/repository/com/zhaogang/jdbc/ojdbc14/10.2.0.4.0/ojdbc14-10.2.0.4.0.jar -->
    <classPathEntry location="D:\ojdbc6-11.2.0.1.0.jar"></classPathEntry>

    <context id="context1">
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
                        connectionURL="jdbc:oracle:thin:@数据库地址:orcl" userId="用户名"
                        password="密码"/>

        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和
            NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <javaModelGenerator targetPackage="com.huanshare.model"
                            targetProject="src/main/java"/>
        <!-- mapper接口存放目录 -->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="src/main/resources"/>
        <javaClientGenerator targetPackage="com.huanshare.mapper"
                             targetProject="src/main/java" type="XMLMAPPER"/>

        <!-- 配置不生成Exmaple文件 -->
        <table tableName="T_PERSONS" domainObjectName="TDataCarriers"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>

    </context>
</generatorConfiguration>

 

7、省略,具体测试要看自己的项目情况

不过在这里,我要说,对于各层之间的方法调用,声明对象,采用注解方式@Autowired,new对象是取不到mapper接口的对象的

你可能感兴趣的:(java搭建 SpringMVC+Mybatis(SMM)+mybatis-generate)