Spring MVC 学习总结(十一)——IDEA+Maven+多模块实现SSM框架集成
一、ssm概要
与ssh(struts/spring/hibernate/)一样,spring+springmvc+mybatis也有一个简称ssm,spring实现业务对象管理,spring mvc负责请求的转发和视图管理, mybatis作为数据对象持久化引擎。这样搭配的优点是:轻量、*度高、spring与spring mvc契合度更好。通过一个商品管理示例完成ssm框架的集成,可以将前面学习过的一些内容整合起来。
1.1、springmvc
1.客户端发送请求到dispacherservlet(分发器)
2.由dispacherservlet控制器查询handermapping,找到处理请求的controller
3.controller调用业务逻辑处理后,返回modelandview
4.dispacherserclet查询视图解析器,找到modelandview指定的视图
5.视图负责将结果显示到客户端
1.2、spring
1.spring是一个轻量级的控制反转(ioc)和面向切面(aop)的容器架构,ioc容器包含并管理应用对象的配置和生命周期,你可以配置你的每个bean如何被创建,也可以配置每个bean是只有一个实例,还是每次需要时都生成一个新的实例,以及它们是如何相互关联的。
2.ioc思想最核心的地方在于,资源不由使用资源的双方管理,而由不使用资源的第三方管理,这可以带来很多好处。
第一,资源集中管理,实现资源的可配置和易管理。
第二,降低了使用资源双方的依赖程度,也就是我们说的耦合度。
3.容器提供了aop技术,利用它很容易实现如权限拦截、运行期监控等功能。
1.3、mybatis
1.mybatis是支持普通sql查询,存储过程和高级映射的优秀持久层框架。mybatis消除了几乎所有的jdbc代码和参数的手工设置以及结果集的检索。mybatis使用简单的xml或注解用于配置和原始映射,将接口和java的pojos(plan old java objects,普通的java对象)映射成数据库中的记录。
2.mybatis的操作都是围绕一个sqlsessionfactory实例展开的。mybatis通过配置文件关联到各实体类的mapper文件,mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlsessionfactory拿到一个sqlsession,再执行sql命令。
1.4、maven多模块
javaee项目开发中为了便于后期的维护,一般会进行分层开发,分层之后,各个层之间的职责会比较明确,后期维护起来也相对比较容易。
《一个小时学会maven》可以获得更多帮助
二、数据库
打开mysql数据库,创建一个表,这里以goods表为例,一个用于存放商品的表,共4个字段id表示编号,name表示商品名称,picture表示图片,price表示价格。sql脚本如下:
set foreign_key_checks=0; -- ---------------------------- -- table structure for `goods` -- ---------------------------- drop table if exists `goods`; create table `goods` ( `id` int(11) not null auto_increment, `name` varchar(200) not null, `price` decimal(10,2) default '0.00', `picture` varchar(100) default 'default.jpg', primary key (`id`) ) engine=innodb auto_increment=12 default charset=utf8; -- ---------------------------- -- records of goods -- ---------------------------- insert into `goods` values ('1', 'g7 中原g7三合一浓醇咖啡固体饮料1200', '66.50', '1.jpg'); insert into `goods` values ('2', '百草味东北松子200gx2袋 坚果炒货零', '42.90', '2.jpg'); insert into `goods` values ('3', '奈津香 桂圆干500gx2袋莆田特产5a桂', '39.90', '3.jpg'); insert into `goods` values ('4', '益达尊享护齿装草本40粒+冰柠40粒+西', '25.90', '4.jpg'); insert into `goods` values ('5', '猴坑茶业2016新茶原产地手工太平猴魁特', '168.00', '5.jpg'); insert into `goods` values ('6', '嘻鱿记 休闲零食 麻辣香辣奶香炭烧 5种', '39.80', '6.jpg'); insert into `goods` values ('7', '荣业鸿福五分瘦腊肠 香港土特产香肠腊味', '126.80', '7.jpg'); insert into `goods` values ('8', '蓓琳娜(bellina)3l pdo特级初榨橄榄油', '178.00', '8.jpg'); insert into `goods` values ('10', '荣业鸿福五分瘦腊肠 香港土特产香肠腊味', '30.60', '9.jpg');
结果:
三、创建多模块项目
3.1、各模块的功能
common模块:通用模块,不依赖其它任何模块,主要有utils、可以在多个不同项目中得胜的内容
entitie模块:pojo、vo、dto
dao模块:数据持久化,访问数据库,这里使用mybatis
service模块:业务模块,处理业务逻辑
webui模块:b/s结构的表示层,主要用于在浏览器中显示数据,提供用户接口
3.2、创建项目结构
创建父模块,不使用骨架的maven普通项目
创建子模块commons
创建不使用骨架的maven普通项目
entity、dao、service模块创建方法与命名规范同上。
webui需要创建一个使用webapp骨架的maven项目:
完成的项目结构如下:
《一个小时学会maven》可以获得更多帮助
四、添加各模块间的依赖
子模块默认是继承父模块的,这里全部使用按需依赖。
dao模块的pom:
<dependencies> <dependency> <groupid>com.zhangguo.ssm01</groupid> <artifactid>ssm01-common</artifactid> </dependency> <dependency> <groupid>com.zhangguo.ssm01</groupid> <artifactid>ssm01-entity</artifactid> </dependency> </dependencies>
service的pom:
<dependencies> <dependency> <groupid>com.zhangguo.ssm01</groupid> <artifactid>ssm01-dao</artifactid> </dependency> </dependencies>
因为依赖具有传递性,在service中可以间接的依赖到entity与common
webui的pom:
<dependencies> <dependency> <groupid>com.zhangguo.ssm01</groupid> <artifactid>ssm01-service</artifactid> </dependency> </dependencies>
依赖完成的结果:
五、完成pojo实体模块
完成这一层之前先明确一下几个容易混淆的概念:
5.1、pojo
pojo(plain ordinary java object)简单的java对象,实际就是普通javabeans,是为了避免和ejb混淆所创造的简称。 其中有一些属性及其getter、setter方法的类,没有业务逻辑,有时可以作为vo(value-object)或dto(data transfer object)来使用。不允许有业务方法,也不能携带connection之类的方法。
5.2、javabean
javabean是java语言写成的可重用组件。javabean则比 pojo复杂很多, java bean 是可复用的组件,对 java bean 并没有严格的规范,理论上讲,任何一个 java 类都可以是一个 bean 。ejb则是enterprise javabean,是javaee的一部分,定义了一个用于开发基于组件的企业多重应用程序标准。
5.3、entity
实体bean,一般是用于orm对象关系映射,一个实体映射成一张表,一般无业务逻辑代码。 负责将数据库中的表记录映射为内存中的entity对象,事实上,创建一个entitybean对象相当于创建一条记录,删除一个entitybean对象会同时从数据库中删除对应记录,修改一个entity bean时,容器会自动将entity bean的状态和数据库同步。
5.4、domain model
业务对象模型(也叫领域模型 domain model)是描述业务用例实现的对象模型。又称概念模型、领域对象模型、分析对象模型。领域模型中的实体类分为四种类型:vo、dto、do、po
vo(view object):视图对象,用于展示层,它的作用是把某个指定页面(或组件)的所有数据封装起来。
dto(data transfer object):数据传输对象,这个概念来源于j2ee的设计模式,原来的目的是为了ejb的分布式应用提供粗粒度的数据实体,以减少分布式调用的次数,从而提高分布式调用的性能和降低网络负载,但在这里,我泛指用于展示层与服务层之间的数据传输对象。
do(domain object):领域对象,就是从现实世界中抽象出来的有形或无形的业务实体。
po(persistentobject):持久化对象,它跟持久层(通常是关系型数据库)的数据结构形成一一对应的映射关系,如果持久层是关系型数据库,那么,数据表中的每个字段(或若干个)就对应po的一个(或若干个)属性。
5.5、实体模块
goods商品pojo对象如下:
package com.zhangguo.ssm01.entity; import java.io.serializable; import java.math.bigdecimal; /** * 商品pojo */ public class goods implements serializable { private int id; private string name; private bigdecimal price; private string picture; public int getid() { return id; } public void setid(int id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public bigdecimal getprice() { return price; } public void setprice(bigdecimal price) { this.price = price; } public string getpicture() { return picture; } public void setpicture(string picture) { this.picture = picture; } @override public string tostring() { return "goods{" + "id=" + id + ", name='" + name + '\'' + ", price=" + price + ", picture='" + picture + '\'' + '}'; } }
六、完成commons通用工具模块
commons通用模块主要是为项目中其它模块提供辅助功能,如utils工具类等内容:
依赖:
父模块的pom:
<?xml version="1.0" encoding="utf-8"?> <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.zhangguo.ssm01</groupid> <artifactid>ssm01</artifactid> <packaging>pom</packaging> <version>1.0-snapshot</version> <modules> <module>ssm01-common</module> <module>ssm01-entity</module> <module>ssm01-dao</module> <module>ssm01-service</module> <module>ssm01-webui</module> </modules> <properties> <!--项目内模块--> <ssm01-common.version>1.0-snapshot</ssm01-common.version> <ssm01-entity.version>1.0-snapshot</ssm01-entity.version> <ssm01-dao.version>1.0-snapshot</ssm01-dao.version> <ssm01-service.version>1.0-snapshot</ssm01-service.version> <ssm01-webui.version>1.0-snapshot</ssm01-webui.version> <jackson.version>2.7.4</jackson.version> <commons-lang3.version>3.4</commons-lang3.version> <!--通用部分--> <log4j.version>2.6.1</log4j.version> <junit.version>4.10</junit.version> <!--spring--> <spring.version>4.3.0.release</spring.version> <aspectjweaver.version>1.8.9</aspectjweaver.version> <cglib.version>3.2.4</cglib.version> <!--mybatis--> <mybatis-spring.version>1.3.0</mybatis-spring.version> <mysql-connector-java.version>5.1.38</mysql-connector-java.version> <mybatis.version>3.4.1</mybatis.version> <c3p0.version>0.9.1.2</c3p0.version> <!--spring mvc--> <jstl.version>1.2</jstl.version> <servlet-api.version>3.0.1</servlet-api.version> <jsp-api.version>2.1</jsp-api.version> <hibernate.version>5.2.2.final</hibernate.version> <commons-io.version>2.4</commons-io.version> <commons-fileupload.version>1.3.1</commons-fileupload.version> <freemarker.version>2.3.23</freemarker.version> </properties> <dependencymanagement> <dependencies> <!--项目内的依赖--> <dependency> <groupid>com.zhangguo.ssm01</groupid> <artifactid>ssm01-common</artifactid> <version>${ssm01-common.version}</version> </dependency> <dependency> <groupid>com.zhangguo.ssm01</groupid> <artifactid>ssm01-entity</artifactid> <version>${ssm01-entity.version}</version> </dependency> <dependency> <groupid>com.zhangguo.ssm01</groupid> <artifactid>ssm01-dao</artifactid> <version>${ssm01-dao.version}</version> </dependency> <dependency> <groupid>com.zhangguo.ssm01</groupid> <artifactid>ssm01-service</artifactid> <version>${ssm01-service.version}</version> </dependency> <dependency> <groupid>com.zhangguo.ssm01</groupid> <artifactid>ssm01-webui</artifactid> <version>${ssm01-webui.version}</version> </dependency> <!--common模块--> <!--jackson--> <dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-databind</artifactid> <version>${jackson.version}</version> </dependency> <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-annotations</artifactid> <version>${jackson.version}</version> </dependency> <!--apache-commons--> <dependency> <groupid>org.apache.commons</groupid> <artifactid>commons-lang3</artifactid> <version>${commons-lang3.version}</version> </dependency> <!--log4j日志包 --> <dependency> <groupid>org.apache.logging.log4j</groupid> <artifactid>log4j-core</artifactid> <version>${log4j.version}</version> </dependency> <!-- junit单元测试工具 --> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>${junit.version}</version> </dependency> <!--spring框架核心库 --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version>${spring.version}</version> </dependency> <!-- aspectj aop 织入器 --> <dependency> <groupid>org.aspectj</groupid> <artifactid>aspectjweaver</artifactid> <version>${aspectjweaver.version}</version> </dependency> <dependency> <groupid>cglib</groupid> <artifactid>cglib</artifactid> <version>${cglib.version}</version> </dependency> <!--mybatis-spring适配器 --> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis-spring</artifactid> <version>${mybatis-spring.version}</version> </dependency> <!--spring java数据库访问包,在本例中主要用于提供数据源 --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-jdbc</artifactid> <version>${spring.version}</version> </dependency> <!--mysql数据库驱动 --> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>${mysql-connector-java.version}</version> </dependency> <!-- mybatis orm框架 --> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis</artifactid> <version>${mybatis.version}</version> </dependency> <!--c3p0 连接池 --> <dependency> <groupid>c3p0</groupid> <artifactid>c3p0</artifactid> <version>${c3p0.version}</version> </dependency> <!-- spring mvc --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context-support</artifactid> <version>${spring.version}</version> </dependency> <!-- jstl --> <dependency> <groupid>javax.servlet</groupid> <artifactid>jstl</artifactid> <version>${jstl.version}</version> </dependency> <!-- servlet核心包 --> <dependency> <groupid>javax.servlet</groupid> <artifactid>javax.servlet-api</artifactid> <version>${servlet-api.version}</version> </dependency> <!--jsp --> <dependency> <groupid>javax.servlet.jsp</groupid> <artifactid>jsp-api</artifactid> <version>${jsp-api.version}</version> </dependency> <!--jsr303 --> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-validator</artifactid> <version>${hibernate.version}</version> </dependency> <!--文件上传 --> <dependency> <groupid>commons-io</groupid> <artifactid>commons-io</artifactid> <version>${commons-io.version}</version> </dependency> <dependency> <groupid>commons-fileupload</groupid> <artifactid>commons-fileupload</artifactid> <version>${commons-fileupload.version}</version> </dependency> <!-- freemarker --> <dependency> <groupid>org.freemarker</groupid> <artifactid>freemarker</artifactid> <version>${freemarker.version}</version> </dependency> </dependencies> </dependencymanagement> </project>
commons的pom:
<?xml version="1.0" encoding="utf-8"?> <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"> <parent> <artifactid>ssm01</artifactid> <groupid>com.zhangguo.ssm01</groupid> <version>1.0-snapshot</version> </parent> <modelversion>4.0.0</modelversion> <artifactid>ssm01-common</artifactid> <dependencies> <!--jackson--> <dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-databind</artifactid> </dependency> <dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-core</artifactid> </dependency> <dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-annotations</artifactid> </dependency> <!--apache-commons--> <dependency> <groupid>org.apache.commons</groupid> <artifactid>commons-lang3</artifactid> </dependency> </dependencies> </project>
添加相应的工具类:
如封装页面返回结果的json:
package com.zhangguo.ssm01.common.utils; import java.util.hashmap; import java.util.map; /** * 返回数据封装 */ public class r extends hashmap<string, object> { private static final long serialversionuid = 1l; public r() { put("code", 1); put("msg", "success"); } //错误时 public static r error() { return error(500, "未知异常,请联系管理员"); } public static r error(string msg) { return error(500, msg); } public static r error(int code, string msg) { r r = new r(); r.put("code", code); r.put("msg", msg); return r; } //成功时 public static r ok(string msg) { r r = new r(); r.put("msg", msg); return r; } public static r ok(map<string, object> map) { r r = new r(); r.putall(map); return r; } public static r ok() { return new r(); } public static r ok(object data) { return new r().put("data",data); } @override public r put(string key, object value) { super.put(key, value); return this; } }
太多了这里不一一介绍。
结果:
七、完成dao数据访问模块
spring学习总结(六)——spring整合mybatis完整示例
7.1、添加依赖
父模块声明:
<?xml version="1.0" encoding="utf-8"?> <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.zhangguo.ssm01</groupid> <artifactid>ssm01</artifactid> <packaging>pom</packaging> <version>1.0-snapshot</version> <modules> <module>ssm01-common</module> <module>ssm01-entity</module> <module>ssm01-dao</module> <module>ssm01-service</module> <module>ssm01-webui</module> </modules> <properties> <!--项目内模块--> <ssm01-common.version>1.0-snapshot</ssm01-common.version> <ssm01-entity.version>1.0-snapshot</ssm01-entity.version> <ssm01-dao.version>1.0-snapshot</ssm01-dao.version> <ssm01-service.version>1.0-snapshot</ssm01-service.version> <ssm01-webui.version>1.0-snapshot</ssm01-webui.version> <jackson.version>2.7.4</jackson.version> <commons-lang3.version>3.4</commons-lang3.version> <!--通用部分--> <log4j.version>2.6.1</log4j.version> <junit.version>4.10</junit.version> <!--spring--> <spring.version>4.3.0.release</spring.version> <aspectjweaver.version>1.8.9</aspectjweaver.version> <cglib.version>3.2.4</cglib.version> <!--mybatis--> <mybatis-spring.version>1.3.0</mybatis-spring.version> <mysql-connector-java.version>5.1.38</mysql-connector-java.version> <mybatis.version>3.4.1</mybatis.version> <c3p0.version>0.9.1.2</c3p0.version> <!--spring mvc--> <jstl.version>1.2</jstl.version> <servlet-api.version>3.0.1</servlet-api.version> <jsp-api.version>2.1</jsp-api.version> <hibernate.version>5.2.2.final</hibernate.version> <commons-io.version>2.4</commons-io.version> <commons-fileupload.version>1.3.1</commons-fileupload.version> <freemarker.version>2.3.23</freemarker.version> </properties> <dependencymanagement> <dependencies> <!--项目内的依赖--> <dependency> <groupid>com.zhangguo.ssm01</groupid> <artifactid>ssm01-common</artifactid> <version>${ssm01-common.version}</version> </dependency> <dependency> <groupid>com.zhangguo.ssm01</groupid> <artifactid>ssm01-entity</artifactid> <version>${ssm01-entity.version}</version> </dependency> <dependency> <groupid>com.zhangguo.ssm01</groupid> <artifactid>ssm01-dao</artifactid> <version>${ssm01-dao.version}</version> </dependency> <dependency> <groupid>com.zhangguo.ssm01</groupid> <artifactid>ssm01-service</artifactid> <version>${ssm01-service.version}</version> </dependency> <dependency> <groupid>com.zhangguo.ssm01</groupid> <artifactid>ssm01-webui</artifactid> <version>${ssm01-webui.version}</version> </dependency> <!--common模块--> <!--jackson--> <dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-databind</artifactid> <version>${jackson.version}</version> </dependency> <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-annotations</artifactid> <version>${jackson.version}</version> </dependency> <!--apache-commons--> <dependency> <groupid>org.apache.commons</groupid> <artifactid>commons-lang3</artifactid> <version>${commons-lang3.version}</version> </dependency> <!--log4j日志包 --> <dependency> <groupid>org.apache.logging.log4j</groupid> <artifactid>log4j-core</artifactid> <version>${log4j.version}</version> </dependency> <!-- junit单元测试工具 --> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>${junit.version}</version> </dependency> <!--spring框架核心库 --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version>${spring.version}</version> </dependency> <!-- aspectj aop 织入器 --> <dependency> <groupid>org.aspectj</groupid> <artifactid>aspectjweaver</artifactid> <version>${aspectjweaver.version}</version> </dependency> <dependency> <groupid>cglib</groupid> <artifactid>cglib</artifactid> <version>${cglib.version}</version> </dependency> <!--mybatis-spring适配器 --> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis-spring</artifactid> <version>${mybatis-spring.version}</version> </dependency> <!--spring java数据库访问包,在本例中主要用于提供数据源 --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-jdbc</artifactid> <version>${spring.version}</version> </dependency> <!--mysql数据库驱动 --> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>${mysql-connector-java.version}</version> </dependency> <!-- mybatis orm框架 --> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis</artifactid> <version>${mybatis.version}</version> </dependency> <!--c3p0 连接池 --> <dependency> <groupid>c3p0</groupid> <artifactid>c3p0</artifactid> <version>${c3p0.version}</version> </dependency> <!-- spring mvc --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context-support</artifactid> <version>${spring.version}</version> </dependency> <!-- jstl --> <dependency> <groupid>javax.servlet</groupid> <artifactid>jstl</artifactid> <version>${jstl.version}</version> </dependency> <!-- servlet核心包 --> <dependency> <groupid>javax.servlet</groupid> <artifactid>javax.servlet-api</artifactid> <version>${servlet-api.version}</version> </dependency> <!--jsp --> <dependency> <groupid>javax.servlet.jsp</groupid> <artifactid>jsp-api</artifactid> <version>${jsp-api.version}</version> </dependency> <!--jsr303 --> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-validator</artifactid> <version>${hibernate.version}</version> </dependency> <!--文件上传 --> <dependency> <groupid>commons-io</groupid> <artifactid>commons-io</artifactid> <version>${commons-io.version}</version> </dependency> <dependency> <groupid>commons-fileupload</groupid> <artifactid>commons-fileupload</artifactid> <version>${commons-fileupload.version}</version> </dependency> <!-- freemarker --> <dependency> <groupid>org.freemarker</groupid> <artifactid>freemarker</artifactid> <version>${freemarker.version}</version> </dependency> </dependencies> </dependencymanagement> </project>
子模块引用:
<?xml version="1.0" encoding="utf-8"?> <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"> <parent> <artifactid>ssm01</artifactid> <groupid>com.zhangguo.ssm01</groupid> <version>1.0-snapshot</version> </parent> <modelversion>4.0.0</modelversion> <artifactid>ssm01-dao</artifactid> <dependencies> <dependency> <groupid>com.zhangguo.ssm01</groupid> <artifactid>ssm01-common</artifactid> </dependency> <dependency> <groupid>com.zhangguo.ssm01</groupid> <artifactid>ssm01-entity</artifactid> </dependency> <!--log4j日志包 --> <dependency> <groupid>org.apache.logging.log4j</groupid> <artifactid>log4j-core</artifactid> </dependency> <!-- junit单元测试工具 --> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> </dependency> <!--spring框架核心库 --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> </dependency> <!-- aspectj aop 织入器 --> <dependency> <groupid>org.aspectj</groupid> <artifactid>aspectjweaver</artifactid> </dependency> <dependency> <groupid>cglib</groupid> <artifactid>cglib</artifactid> </dependency> <!--mybatis-spring适配器 --> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis-spring</artifactid> </dependency> <!--spring java数据库访问包,在本例中主要用于提供数据源 --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-jdbc</artifactid> </dependency> <!--mysql数据库驱动 --> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> </dependency> <!-- mybatis orm框架 --> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis</artifactid> </dependency> <!--c3p0 连接池 --> <dependency> <groupid>c3p0</groupid> <artifactid>c3p0</artifactid> </dependency> </dependencies> </project>
7.2、数据库配置属性文件
在resources根目录中定义db.properties
#mysql jdbc mysql.driver=com.mysql.jdbc.driver mysql.url=jdbc:mysql://localhost:3306/gomall?useunicode=true&characterencoding=utf-8 mysql.uid=root mysql.pwd=uchr@123
7.3、spring-mybatis集成
applicationcontext.xml,用于完成spring-mybatis集成使用
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!--1 引入属性文件,在配置中占位使用 --> <context:property-placeholder location="classpath*:db.properties" /> <!--2 配置c3p0数据源 --> <bean id="datasource" class="com.mchange.v2.c3p0.combopooleddatasource" destroy-method="close"> <!--驱动类名 --> <property name="driverclass" value="${mysql.driver}" /> <!-- url --> <property name="jdbcurl" value="${mysql.url}" /> <!-- 用户名 --> <property name="user" value="${mysql.uid}" /> <!-- 密码 --> <property name="password" value="${mysql.pwd}" /> <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数 --> <property name="acquireincrement" value="5"></property> <!-- 初始连接池大小 --> <property name="initialpoolsize" value="10"></property> <!-- 连接池中连接最小个数 --> <property name="minpoolsize" value="5"></property> <!-- 连接池中连接最大个数 --> <property name="maxpoolsize" value="20"></property> </bean> <!--3 会话工厂bean sqlsessionfactorybean --> <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean"> <!-- 数据源 --> <property name="datasource" ref="datasource"></property> <!-- 别名 --> <property name="typealiasespackage" value="com.zhangguo.ssm01.entity"></property> <!-- sql映射文件路径 --> <property name="mapperlocations" value="classpath*:mappers/*mapper.xml"></property> <!--mybatis配置文件位置--> <property name="configlocation" value="classpath:mybatis-config.xml"></property> </bean> <!--4 自动扫描对象关系映射 --> <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer"> <!--指定会话工厂,如果当前上下文中只定义了一个则该属性可省去 --> <property name="sqlsessionfactorybeanname" value="sqlsessionfactory"></property> <!-- 指定要自动扫描接口的基础包,实现接口 --> <property name="basepackage" value="com.zhangguo.ssm01.dao"></property> </bean> <!--5 声明式事务管理 --> <!--定义事物管理器,由spring管理事务 --> <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource"></property> </bean> <!--支持注解驱动的事务管理,指定事务管理器 --> <tx:annotation-driven transaction-manager="transactionmanager"/> <!--6 容器自动扫描ioc组件 --> <context:component-scan base-package="com.zhangguo.ssm01.dao"></context:component-scan> <!--7 aspectj支持自动代理实现aop功能 --> <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy> </beans>
7.4、路径引用问题
模块被引用后因为路径问题可能会现在异常,如:java.io.filenotfoundexception: could not open servletcontext resource [/classpath*:mybatis-config.xml]
详细的异常日志如下:
d:\documents\downloads\compressed\apache-tomcat-8.5.33-windows-x64\apache-tomcat-8.5.33\bin\catalina.bat run [2018-11-02 08:59:18,038] artifact ssm01-webui:war exploded: waiting for server connection to start artifact deployment... using catalina_base: "c:\users\administrator\.intellijidea2017.2\system\tomcat\unnamed_ssm01" using catalina_home: "d:\documents\downloads\compressed\apache-tomcat-8.5.33-windows-x64\apache-tomcat-8.5.33" using catalina_tmpdir: "d:\documents\downloads\compressed\apache-tomcat-8.5.33-windows-x64\apache-tomcat-8.5.33\temp" using jre_home: "c:\program files\java\jdk1.8.0_111" using classpath: "d:\documents\downloads\compressed\apache-tomcat-8.5.33-windows-x64\apache-tomcat-8.5.33\bin\bootstrap.jar;d:\documents\downloads\compressed\apache-tomcat-8.5.33-windows-x64\apache-tomcat-8.5.33\bin\tomcat-juli.jar" 02-nov-2018 08:59:22.056 信息 [main] org.apache.catalina.startup.versionloggerlistener.log server version: apache tomcat/8.5.33 02-nov-2018 08:59:22.063 信息 [main] org.apache.catalina.startup.versionloggerlistener.log server built: aug 12 2018 08:20:08 utc 02-nov-2018 08:59:22.063 信息 [main] org.apache.catalina.startup.versionloggerlistener.log server number: 8.5.33.0 02-nov-2018 08:59:22.063 信息 [main] org.apache.catalina.startup.versionloggerlistener.log os name: windows 7 02-nov-2018 08:59:22.063 信息 [main] org.apache.catalina.startup.versionloggerlistener.log os version: 6.1 02-nov-2018 08:59:22.063 信息 [main] org.apache.catalina.startup.versionloggerlistener.log architecture: amd64 02-nov-2018 08:59:22.063 信息 [main] org.apache.catalina.startup.versionloggerlistener.log java home: c:\program files\java\jdk1.8.0_111\jre 02-nov-2018 08:59:22.063 信息 [main] org.apache.catalina.startup.versionloggerlistener.log jvm version: 1.8.0_111-b14 02-nov-2018 08:59:22.063 信息 [main] org.apache.catalina.startup.versionloggerlistener.log jvm vendor: oracle corporation 02-nov-2018 08:59:22.064 信息 [main] org.apache.catalina.startup.versionloggerlistener.log catalina_base: c:\users\administrator\.intellijidea2017.2\system\tomcat\unnamed_ssm01 02-nov-2018 08:59:22.064 信息 [main] org.apache.catalina.startup.versionloggerlistener.log catalina_home: d:\documents\downloads\compressed\apache-tomcat-8.5.33-windows-x64\apache-tomcat-8.5.33 02-nov-2018 08:59:22.064 信息 [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -djava.util.logging.config.file=c:\users\administrator\.intellijidea2017.2\system\tomcat\unnamed_ssm01\conf\logging.properties 02-nov-2018 08:59:22.064 信息 [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -djava.util.logging.manager=org.apache.juli.classloaderlogmanager 02-nov-2018 08:59:22.065 信息 [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -dcom.sun.management.jmxremote= 02-nov-2018 08:59:22.065 信息 [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -dcom.sun.management.jmxremote.port=1099 02-nov-2018 08:59:22.065 信息 [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -dcom.sun.management.jmxremote.ssl=false 02-nov-2018 08:59:22.065 信息 [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -dcom.sun.management.jmxremote.authenticate=false 02-nov-2018 08:59:22.065 信息 [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -djava.rmi.server.hostname=127.0.0.1 02-nov-2018 08:59:22.065 信息 [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -djdk.tls.ephemeraldhkeysize=2048 02-nov-2018 08:59:22.066 信息 [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -djava.protocol.handler.pkgs=org.apache.catalina.webresources 02-nov-2018 08:59:22.066 信息 [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -dignore.endorsed.dirs= 02-nov-2018 08:59:22.066 信息 [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -dcatalina.base=c:\users\administrator\.intellijidea2017.2\system\tomcat\unnamed_ssm01 02-nov-2018 08:59:22.066 信息 [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -dcatalina.home=d:\documents\downloads\compressed\apache-tomcat-8.5.33-windows-x64\apache-tomcat-8.5.33 02-nov-2018 08:59:22.066 信息 [main] org.apache.catalina.startup.versionloggerlistener.log command line argument: -djava.io.tmpdir=d:\documents\downloads\compressed\apache-tomcat-8.5.33-windows-x64\apache-tomcat-8.5.33\temp 02-nov-2018 08:59:22.066 信息 [main] org.apache.catalina.core.aprlifecyclelistener.lifecycleevent loaded apr based apache tomcat native library [1.2.17] using apr version [1.6.3]. 02-nov-2018 08:59:22.067 信息 [main] org.apache.catalina.core.aprlifecyclelistener.lifecycleevent apr capabilities: ipv6 [true], sendfile [true], accept filters [false], random [true]. 02-nov-2018 08:59:22.067 信息 [main] org.apache.catalina.core.aprlifecyclelistener.lifecycleevent apr/openssl configuration: useaprconnector [false], useopenssl [true] 02-nov-2018 08:59:23.148 信息 [main] org.apache.catalina.core.aprlifecyclelistener.initializessl openssl successfully initialized [openssl 1.0.2o 27 mar 2018] 02-nov-2018 08:59:23.582 信息 [main] org.apache.coyote.abstractprotocol.init initializing protocolhandler ["http-nio-8080"] 02-nov-2018 08:59:23.617 信息 [main] org.apache.tomcat.util.net.nioselectorpool.getsharedselector using a shared selector for servlet write/read 02-nov-2018 08:59:23.645 信息 [main] org.apache.coyote.abstractprotocol.init initializing protocolhandler ["ajp-nio-8009"] 02-nov-2018 08:59:23.647 信息 [main] org.apache.tomcat.util.net.nioselectorpool.getsharedselector using a shared selector for servlet write/read 02-nov-2018 08:59:23.648 信息 [main] org.apache.catalina.startup.catalina.load initialization processed in 2577 ms 02-nov-2018 08:59:23.727 信息 [main] org.apache.catalina.core.standardservice.startinternal starting service [catalina] 02-nov-2018 08:59:23.728 信息 [main] org.apache.catalina.core.standardengine.startinternal starting servlet engine: apache tomcat/8.5.33 02-nov-2018 08:59:23.769 信息 [main] org.apache.coyote.abstractprotocol.start starting protocolhandler ["http-nio-8080"] 02-nov-2018 08:59:23.790 信息 [main] org.apache.coyote.abstractprotocol.start starting protocolhandler ["ajp-nio-8009"] 02-nov-2018 08:59:23.827 信息 [main] org.apache.catalina.startup.catalina.start server startup in 178 ms [2018-11-02 08:59:24,194] artifact ssm01-webui:war exploded: artifact is being deployed, please wait... connected to server 02-nov-2018 08:59:33.225 信息 [rmi tcp connection(3)-127.0.0.1] org.apache.jasper.servlet.tldscanner.scanjars at least one jar was scanned for tlds yet contained no tlds. enable debug logging for this logger for a complete list of jars that were scanned but no tlds were found in them. skipping unneeded jars during scanning can improve startup time and jsp compilation time. 02-nov-2018 08:59:33.505 信息 [rmi tcp connection(3)-127.0.0.1] org.springframework.web.context.contextloader.initwebapplicationcontext root webapplicationcontext: initialization started 02-nov-2018 08:59:33.792 信息 [localhost-startstop-1] org.apache.catalina.startup.hostconfig.deploydirectory deploying web application directory [d:\documents\downloads\compressed\apache-tomcat-8.5.33-windows-x64\apache-tomcat-8.5.33\webapps\manager] 02-nov-2018 08:59:34.178 信息 [localhost-startstop-1] org.apache.catalina.startup.hostconfig.deploydirectory deployment of web application directory [d:\documents\downloads\compressed\apache-tomcat-8.5.33-windows-x64\apache-tomcat-8.5.33\webapps\manager] has finished in [385] ms 02-nov-2018 08:59:34.294 信息 [rmi tcp connection(3)-127.0.0.1] org.springframework.web.context.support.xmlwebapplicationcontext.preparerefresh refreshing root webapplicationcontext: startup date [fri nov 02 08:59:34 cst 2018]; root of context hierarchy 02-nov-2018 08:59:34.550 信息 [rmi tcp connection(3)-127.0.0.1] org.springframework.beans.factory.xml.xmlbeandefinitionreader.loadbeandefinitions loading xml bean definitions from url [jar:file:/c:/users/administrator/desktop/mybatis-generator-gui-0.8.4/ssm01/ssm01-webui/target/ssm01-webui/web-inf/lib/ssm01-dao-1.0-snapshot.jar!/applicationcontext.xml] 02-nov-2018 08:59:36.200 信息 [rmi tcp connection(3)-127.0.0.1] org.springframework.context.support.propertysourcesplaceholderconfigurer.loadproperties loading properties file from url [jar:file:/c:/users/administrator/desktop/mybatis-generator-gui-0.8.4/ssm01/ssm01-webui/target/ssm01-webui/web-inf/lib/ssm01-dao-1.0-snapshot.jar!/db.properties] 02-nov-2018 08:59:36.754 信息 [rmi tcp connection(3)-127.0.0.1] com.mchange.v2.log.mlog.<clinit> mlog clients using java 1.4+ standard logging. 02-nov-2018 08:59:36.929 信息 [rmi tcp connection(3)-127.0.0.1] com.mchange.v2.c3p0.c3p0registry.banner initializing c3p0-0.9.1.2 [built 21-may-2007 15:04:56; debug? true; trace: 10] 02-nov-2018 08:59:37.332 警告 [rmi tcp connection(3)-127.0.0.1] org.springframework.web.context.support.xmlwebapplicationcontext.refresh exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.beancreationexception: error creating bean with name 'sqlsessionfactory' defined in url [jar:file:/c:/users/administrator/desktop/mybatis-generator-gui-0.8.4/ssm01/ssm01-webui/target/ssm01-webui/web-inf/lib/ssm01-dao-1.0-snapshot.jar!/applicationcontext.xml]: invocation of init method failed; nested exception is java.io.filenotfoundexception: could not open servletcontext resource [/classpath*:mybatis-config.xml] 02-nov-2018 08:59:37.341 严重 [rmi tcp connection(3)-127.0.0.1] org.springframework.web.context.contextloader.initwebapplicationcontext context initialization failed org.springframework.beans.factory.beancreationexception: error creating bean with name 'sqlsessionfactory' defined in url [jar:file:/c:/users/administrator/desktop/mybatis-generator-gui-0.8.4/ssm01/ssm01-webui/target/ssm01-webui/web-inf/lib/ssm01-dao-1.0-snapshot.jar!/applicationcontext.xml]: invocation of init method failed; nested exception is java.io.filenotfoundexception: could not open servletcontext resource [/classpath*:mybatis-config.xml] at org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.initializebean(abstractautowirecapablebeanfactory.java:1578) at org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:545) at org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:482) at org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:306) at org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) at org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:302) at org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:197) at org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:756) at org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:861) at org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:541) at org.springframework.web.context.contextloader.configureandrefreshwebapplicationcontext(contextloader.java:444) at org.springframework.web.context.contextloader.initwebapplicationcontext(contextloader.java:326) at org.springframework.web.context.contextloaderlistener.contextinitialized(contextloaderlistener.java:107) at org.apache.catalina.core.standardcontext.listenerstart(standardcontext.java:4792) at org.apache.catalina.core.standardcontext.startinternal(standardcontext.java:5256) at org.apache.catalina.util.lifecyclebase.start(lifecyclebase.java:150) at org.apache.catalina.core.containerbase.addchildinternal(containerbase.java:754) at org.apache.catalina.core.containerbase.addchild(containerbase.java:730) at org.apache.catalina.core.standardhost.addchild(standardhost.java:734) at org.apache.catalina.startup.hostconfig.manageapp(hostconfig.java:1736) at sun.reflect.nativemethodaccessorimpl.invoke0(native method) at sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) at sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) at java.lang.reflect.method.invoke(method.java:498) at org.apache.tomcat.util.modeler.basemodelmbean.invoke(basemodelmbean.java:300) at com.sun.jmx.interceptor.defaultmbeanserverinterceptor.invoke(defaultmbeanserverinterceptor.java:819) at com.sun.jmx.mbeanserver.jmxmbeanserver.invoke(jmxmbeanserver.java:801) at org.apache.catalina.mbeans.mbeanfactory.createstandardcontext(mbeanfactory.java:483) at org.apache.catalina.mbeans.mbeanfactory.createstandardcontext(mbeanfactory.java:432) at sun.reflect.nativemethodaccessorimpl.invoke0(native method) at sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) at sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) at java.lang.reflect.method.invoke(method.java:498) at org.apache.tomcat.util.modeler.basemodelmbean.invoke(basemodelmbean.java:300) at com.sun.jmx.interceptor.defaultmbeanserverinterceptor.invoke(defaultmbeanserverinterceptor.java:819) at com.sun.jmx.mbeanserver.jmxmbeanserver.invoke(jmxmbeanserver.java:801) at javax.management.remote.rmi.rmiconnectionimpl.dooperation(rmiconnectionimpl.java:1468) at javax.management.remote.rmi.rmiconnectionimpl.access$300(rmiconnectionimpl.java:76) at javax.management.remote.rmi.rmiconnectionimpl$privilegedoperation.run(rmiconnectionimpl.java:1309) at javax.management.remote.rmi.rmiconnectionimpl.doprivilegedoperation(rmiconnectionimpl.java:1401) at javax.management.remote.rmi.rmiconnectionimpl.invoke(rmiconnectionimpl.java:829) at sun.reflect.nativemethodaccessorimpl.invoke0(native method) at sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) at sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) at java.lang.reflect.method.invoke(method.java:498) at sun.rmi.server.unicastserverref.dispatch(unicastserverref.java:324) at sun.rmi.transport.transport$1.run(transport.java:200) at sun.rmi.transport.transport$1.run(transport.java:197) at java.security.accesscontroller.doprivileged(native method) at sun.rmi.transport.transport.servicecall(transport.java:196) at sun.rmi.transport.tcp.tcptransport.handlemessages(tcptransport.java:568) at sun.rmi.transport.tcp.tcptransport$connectionhandler.run0(tcptransport.java:826) at sun.rmi.transport.tcp.tcptransport$connectionhandler.lambda$run$0(tcptransport.java:683) at java.security.accesscontroller.doprivileged(native method) at sun.rmi.transport.tcp.tcptransport$connectionhandler.run(tcptransport.java:682) at java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) at java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) at java.lang.thread.run(thread.java:745) caused by: java.io.filenotfoundexception: could not open servletcontext resource [/classpath*:mybatis-config.xml] at org.springframework.web.context.support.servletcontextresource.getinputstream(servletcontextresource.java:141) at org.mybatis
相关文章:
-
-
之前我写了一篇《吃人的那些Java名词:对象、引用、堆、栈和堆栈》,本以为凭借自己8年的Java编程经验足够把这些“吃人”的Java名词解释清楚了,... [阅读全文]
-
注:增删改查表数据在 views.py 内 添加表数据: 删表数据: 改表数据: 查表数据: 常用的查询方法: 常用的查询条件: 相当于S... [阅读全文]
-
连接SqlServer 2008R2数据库超时 .Net Core 在 Linux 下连接 SqlServer 需要 SqlServer2008 S... [阅读全文]
-
1.Filter是什么? Filter的基本功能是对Servlet容器调用Servlet的过程进行拦截,从而在Servlet进行响应处理的前后实现一... [阅读全文]
-
1 /// <summary> 2 /// SqlSugar 注入Service的扩展方法 3 /// </summary>... [阅读全文]
-
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
下一篇: AES转码问题
发表评论