SpringMVC+Mybatis(SMM)+mybatis-generate
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、创建项目,在这里首先给出资源文件结构图(红色圈中的为下面要添加修改的文件)
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接口的对象的
SpringMVC+Mybatis(SMM)+mybatis-generate的更多相关文章
- springmvc缓存和mybatis缓存
首先要有一个搭建好的ssm框架,笔者使用的是基于maven搭建的ssm框架. 加入springmvc缓存: 导入相关依赖包: <dependency> <groupId>org ...
- SpringMVC, Spring和Mybatis整合案例一
一 准备工作 包括:spring(包括springmvc).mybatis.mybatis-spring整合包.数据库驱动.第三方连接池. 二 整合思路 Dao层: 1.SqlMapConfig. ...
- spring(一)--spring/springmvc/spring+hibernate(mybatis)配置文件
这篇文章用来总结一下spring,springmvc,spring+mybatis,spring+hibernate的配置文件 1.web.xml 要使用spring,必须在web.xml中定义分发器 ...
- Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:generate (default-cli) on project : <properties> resource does not exist
使用mybatis-generator自动生成mapper.dao等文件时,报错如下: org.apache.maven.lifecycle.LifecycleExecutionException: ...
- springboot集成mybatis及mybatis generator工具使用
原文链接 前言mybatis是一个半自动化的orm框架,所谓半自动化就是mybaitis只支持数据库查出的数据映射到pojo类上,而实体到数据库的映射需要自己编写sql语句实现,相较于hibernat ...
- springboot入门(三)-- springboot集成mybatis及mybatis generator工具使用
前言 mybatis是一个半自动化的orm框架,所谓半自动化就是mybaitis只支持数据库查出的数据映射到pojo类上,而实体到数据库的映射需要自己编写sql语句实现,相较于hibernate这种完 ...
- 【Mybatis】MyBatis之配置多数据源(十)
在做项目的过程中,有时候一个数据源是不够,那么就需要配置多个数据源.本例介绍mybatis多数据源配置 前言 一般项目单数据源,使用流程如下: 单个数据源绑定给sessionFactory,再在Dao ...
- 【Mybatis】MyBatis之整合Spring(八)
创建环境 系统:macOS Java:1.8 软件:eclipse,maven,mysql 创建步骤 本例:创建一个Maven项目(SpringMVC+Spring+Mybatis),页面上展示员工列 ...
- spring boot集成mybatis(3) - mybatis generator 配置
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
随机推荐
- iOS安全攻击和防御(24):敏感的保护方案逻辑(1)
iOS安全攻击和防御(24):敏感的保护方案逻辑(1) Objective-C代码easy被hook.暴露信息太赤裸裸,为了安全,改用C来写吧! 当然不是所有代码都要C来写,我指的是敏感业务逻辑代码. ...
- VSTO学习笔记(七)基于WPF的Excel分析、转换小程序
原文:VSTO学习笔记(七)基于WPF的Excel分析.转换小程序 近期因为工作的需要,要批量处理Excel文件,于是写了一个小程序,来提升工作效率. 小程序的功能是对Excel进行一些分析.验证,然 ...
- poj 2201 构造
这个题目的构造方法应该还算是很好想的,先给a按照从小到大排序,然后按顺序插入数据,构造一棵二叉查找树,而且50000的数据,nlogn的做法,应该还是很好的.不过这个题目的编码比想象中要麻烦一点,并且 ...
- linux内核笔记-内核同步
linux内核就相当于不断对请求进行响应的服务器,这些请求可能来自CPU,可能来自发出中断的外部设备.我们将内核看作两种请求的侍者. (1)老板提出请求,侍者如果空闲,为老板服务.(系统调用或异常) ...
- LeetCode——Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- A Game of Thrones(11) - Daenerys
Daenerys Targaryen wed Khal Drogo with fear and barbaric([bɑː'bærɪk]野蛮的,粗野的) splendor(光彩:壮丽) in a fi ...
- Android监控程序本身被卸载方法汇总
本文章由Jack_Jia编写,转载请注明出处. 文章链接: http://blog.csdn.net/jiazhijun/article/details/10157901 作者:Jack_Jia ...
- 〖Groovy〗语言使用贴士(Tips)(转)
[Groovy]是一门运行在[JVM]之上的动态语言.由[James Strachan]和[Bob McWhirter]于2003年启动开发,之后于2004年3月成为[JSR 241](Java Sp ...
- 单点更新线段树 RMQ
D. Xenia and Bit Operations time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Oracle 数据迁移(从Oracle11G迁移到更高的版本号Oracle10G低版本号)
1.数据库状况 生产环境是11G,linux系统,測试环境是10G,windows系统,须要从生产环境导出一个用户下全部的数据,导入測试环境中. 由于数据量比較小,准备採用EXP和IMP工具来做 ...