从头开始基于Maven搭建SpringMVC+Mybatis项目(1)
技术发展日新月异,许多曾经拥有霸主地位的流行技术短短几年间已被新兴技术所取代。
在Java的世界中,框架之争可能比语言本身的改变更让人关注。近几年,SpringMVC凭借简单轻便、开发效率高、与spring框架无缝整合等特点,逐渐击败前辈Struts/Struts2,成为最常用的Web框架。而Mybatis相对于hibernate,同样具有开发简单、效率高的优势,而且对SQL的可控性更好,有利于性能调优,逐渐也积累了挑战Hibernate的实力和人气。
当前SpringMVC+Mybatis已经成为最流行的框架组合之一。
好的框架可以帮我们简化开发,但实际的工作中还有大量的时间是花在编译、测试、打包、部署等构建工作上,即使IDE变得越来越强大,但这些工作仍然需要我们一步一步去操作,而不能变成自动化的流水线作业。每天、每个项目重复这些步骤是毫无意义的,所以我们需要构建工具的帮助。构建工具的可选择性并不是很多,Maven是目前是主流的选择,它可以帮助开发者自动化构建、标准化结构、管理依赖等,极大的提高代码以外的工作效率。
Maven、SpringMVC、Mybatis的组合优雅而强大,但对于初学者来说,把三者整合在一起并不是一件简单的事。本文对这一复杂过程做拆解,从最基础的步骤开始,一步步搭建起一个支持增、删、改、分页查询的Web项目。
本文以Eclipse为开发工具,并需要安装好Maven及Maven Integration for Eclipse插件,数据库使用MySQL。
如果没有安装过Maven可以参考:
http://blog.csdn.net/autfish/article/details/51008788
下面正式开始。
打开Eclipse,依次点击 File -> New -> Maven Project
这一步不做任何修改,直接下一步
接下来是选择Archetype。Maven提倡约定优于配置,对于源代码目录结构、配置文件位置、测试用户目录等内容都有既定的规则。遵循这些规则的好处就是不同开发者建立的项目结构是一致的,减少了在加入新项目时额外的熟悉和学习成本。
Maven内置的多种archetype可以针对不同类型的项目帮助开发者迅速勾勒出项目的骨架,例如Web项目可以选择maven-archetype-webapp,自动建立的目录中包括webapp等。这里从最基础的开始,选择maven-archetype-quickstart,其他选项保持不变,点击下一步。
这一步中填写项目的基本信息。Group Id和Artifact Id用于标注项目或模块的坐标,Maven的一大功能是管理依赖(jar包),Maven的中央仓库中有成千上万的的开源项目,在仓库中找到所需的项目文件就需要给每个项目分配一个唯一的标识。Group Id用于定义当前的项目,每个项目下按功能可能划分多个模块,Artifact Id定义具体的模块。例如Spring项目的Group Id是org.springframework,其下面划分了Artifact Id为spring-core、spring-context、spring-jdbc等多个模块。
输入Group Id和Artifact Id后点击完成。完成后生成的项目结构如下:
默认生成了src/main和src/test两个文件夹,根据约定,main放置项目源代码,test目录放置测试用例。默认还生成了两个演示文件(App.java和AppTest.java)可以直接删除。
本例中我们使用Mybatis操作MySQL数据库,并使用Spring集成,这些都需要添加依赖,即jar包。借助Maven,开发者不再需要去各个网站下载文件,而只需要配置pom.xml即可。打开pom.xml,默认代码如下:
- <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.example</groupId>
- <artifactId>petstore</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>petstore</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </project>
其中,<dependencies>节点中的内容即依赖,Archetype默认添加了junit-3.8.1,我们按需要补充Spring、Mybatis、MySQL驱动等,完成后代码如下
- ...
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <springframework.version>4.2.6.RELEASE</springframework.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>${springframework.version}</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${springframework.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${springframework.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-beans</artifactId>
- <version>${springframework.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>${springframework.version}</version>
- </dependency>
- <dependency>
- <groupId>c3p0</groupId>
- <artifactId>c3p0</artifactId>
- <version>0.9.1.2</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>3.4.1</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- <version>1.3.0</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.18</version>
- </dependency>
- </dependencies>
- </project>
注意这里使用了一个小技巧,spring有多个模块且使用相同的版本号,所以设置了一个<springframework.version>属性常量,如果需要修改版本只需要修改此处常量值即可。
至此Maven配置完成,下面开始Mybatis部分。
本例中要实现的功能很简单,基于关系型数据库对某种商品做插入和查询的管理,商品仅有名称和价格两个属性。首先准备好数据库。
建库:
- CREATE SCHEMA `petstore` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin ;
建表:
- CREATE TABLE `petstore`.`t_product` (
- `p_id` INT NOT NULL AUTO_INCREMENT,
- `p_name` VARCHAR(45) NOT NULL,
- `p_price` FLOAT NOT NULL,
- PRIMARY KEY (`p_id`))
- ENGINE = InnoDB
- DEFAULT CHARACTER SET = utf8;
Mybatis作为ORM框架,在这里的作用是把对t_product表的操作映射成对Java类的操作,依据约定,我们需要编码三个文件:
Product.java - 映射实体类
ProductMapper.java - 接口,定义可调用的方法
Product.xml - 实际执行的SQL映射文件
添加代码文件后的目录结构如下:
三个文件的代码如下:
Product.java
- package com.example.petstore.model;
- public class Product {
- private int id;
- private String name;
- private float price;
- 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 float getPrice() {
- return price;
- }
- public void setPrice(float price) {
- this.price = price;
- }
- }
ProductMapper.java
- package com.example.petstore.model;
- public interface ProductMapper {
- void addProduct(Product product);
- Product selectById(int id);
- }
Product.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.example.petstore.model.ProductMapper">
- <resultMap type="com.example.petstore.model.Product" id="productMap">
- <id column="p_id" property="id" />
- <result column="p_name" property="name" />
- <result column="p_price" property="price" />
- </resultMap>
- <insert id="addProduct" parameterType="com.example.petstore.model.Product" useGeneratedKeys="true" keyProperty="id">
- insert into t_product(p_name,p_price) values(#{name},#{price})
- </insert>
- <select id="selectById" parameterType="int" resultType="com.example.petstore.model.Product" resultMap="productMap">
- select * from t_product where p_id=#{id}
- </select>
- </mapper>
可以看出,Product.java和ProductMapper.java非常普通,没有任何与数据库操作有关联的内容。关键代码在Product.xml中,其中,insert元素的id指明其匹配的方法是ProductMapper.java中的addProduct方法,参数类型是Product的实例,使用了数据库自增字段,并且把自增的值绑定到id属性上。select元素匹配的方法是selectById,resultMap元素定义了字段和Product类的属性的对应关系。
代码中并没有生成ProductMapper的实现类,而是在运行期间动态创建代理类来完成实例化。
到这里Mybatis代码就开发完成了,但是我们还想知道这些代码是否可以正常工作,需要编写一些测试用例。
如果仅使用Mybatis(而不使用Spring),那么还要添加Mybatis配置文件设置数据库连接等,但这里不打算这么做,而是用Spring接管,所以接下来的代码是Spring核心配置文件applicationContext.xml。注意这里是用于测试,所以文件将添加在src/test/java目录。
applicationContext.xml
- <?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:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
- destroy-method="close">
- <property name="driverClass" value="com.mysql.jdbc.Driver" />
- <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/petstore?useUnicode=true&characterEncoding=UTF8" />
- <property name="user" value="root" />
- <property name="password" value="root123456" />
- <property name="minPoolSize" value="2" />
- <property name="maxPoolSize" value="10" />
- </bean>
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mapperLocations" value="classpath*:com/example/petstore/model/*.xml" />
- </bean>
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.example.petstore.model" />
- </bean>
- </beans>
其中,数据库的连接串及用户名、密码是我的测试机的设置,你需要按你的环境进行修改。
然后是测试用例,在src/test/java下添加类com.example.petstore.test.ProductTest.java
- package com.example.petstore.test;
- import static org.junit.Assert.*;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
- import com.example.petstore.model.Product;
- import com.example.petstore.model.ProductMapper;
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = "classpath:applicationContext.xml")
- public class ProductTest {
- @Autowired
- private ProductMapper productMapper;
- @Test
- public void insertAndSelect() {
- Product product1 = new Product();
- product1.setName("tom");
- product1.setPrice(99.9f);
- productMapper.addProduct(product1);
- assertTrue(product1.getId() > 0);
- Product product2 = productMapper.selectById(product1.getId());
- assertTrue(product2.getId() == product1.getId());
- assertTrue(product2.getName().equals(product1.getName()));
- }
- }
大功告成,本节中的所有代码都写完了,完成后的目录结构如下:
运行一下看看,在ProductTest.java上点右键 -> Run As -> Junit Test
测试通过,来看一下数据库里的内容:
总结
上述示例中完成了通过Maven创建简单项目、对数据库表的插入和读取操作,尽管没什么实用性,但也能够了解到了Mybatis的基本用法及与Spring的集成方式。
项目以jar包形式打包发布,这样做有利于代码复用,但是显然无法再增加Web部分的内容。传统的做法是另建一个Web项目并引用此项目,但我们接下来将使用Maven的聚合和继承功能构建包括一个数据库持久层模块和一个基于SpringMVC的Web模块的聚合项目,这些内容在下一节中介绍。
从头开始基于Maven搭建SpringMVC+Mybatis项目(1)的更多相关文章
- 从头开始基于Maven搭建SpringMVC+Mybatis项目(3)
接上文内容,本节介绍基于Mybatis的查询和分页功能,并展示一个自定义的分页标签,可重复使用以简化JSP页面的开发. 从头阅读传送门 在上一节中,我们已经使用Maven搭建好了项目的基础结构,包括一 ...
- 从头开始基于Maven搭建SpringMVC+Mybatis项目(2)
接上文内容,本节介绍Maven的聚合和继承. 从头阅读传送门 互联网时代,软件正在变得越来越复杂,开发人员通常会对软件划分模块,以获得清晰的设计.良好的分工及更高的可重用性.Maven的聚合特性能把多 ...
- 从头开始基于Maven搭建SpringMVC+Mybatis项目(4)
接上文内容,上一节中的示例中完成了支持分页的商品列表查询功能,不过我们的目标是打造一个商品管理后台,本节中还需要补充添加.修改.删除商品的功能,这些功能依靠Mybatis操作数据库,并通过Spring ...
- maven搭建springmvc+mybatis项目
上一篇中已经成功使用maven搭建了一个web项目,本篇描述在此基础上怎么搭建一个基于springmvc+mybatis环境的项目. 说了这么久,为什么那么多人都喜欢用maven搭建项目?我们都知道m ...
- Maven搭建SpringMVC+Mybatis项目详解
前言 最近比较闲,复习搭建一下项目,这次主要使用spring+SpringMVC+Mybatis.项目持久层使用Mybatis3,控制层使用SpringMVC4.1,使用Spring4.1管理控制器, ...
- Maven搭建SpringMVC+MyBatis+Json项目(多模块项目)
一.开发环境 Eclipse:eclipse-jee-luna-SR1a-win32; JDK:jdk-8u121-windows-i586.exe; MySql:MySQL Server 5.5; ...
- Maven搭建SpringMVC+Hibernate项目详解 【转】
前言 今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这 ...
- Maven搭建SpringMVC + SpringJDBC项目详解
前言 上一次复习搭建了SpringMVC+Mybatis,这次搭建一下SpringMVC,采用的是SpringJDBC,没有采用任何其他的ORM框架,SpringMVC提供了一整套的WEB框架,所以如 ...
- Maven搭建SpringMVC+Hibernate项目详解
前言 今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这 ...
随机推荐
- Python3入门笔记(1) —— windows安装与运行
Python的设计哲学是"优雅"."明确"."简单".这也是我喜欢Python的理由之一 Python的安装: 1.进入Python官方网站 ...
- xml生成方式二(Xml序列化器XmlSerializer)
一.andoirdAPI提供了xml生成和解析的API: XmlSerializer xs = Xml.newSerializer();和XmlPullParser xmlPullParser = X ...
- HDU4992 求所有原根
Primitive Roots Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- bzoj 4198: [Noi2015]荷马史诗
Description 追逐影子的人,自己就是影子. --荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的<荷马史诗>.但是由& ...
- nova创建虚拟机源码系列分析之二 wsgi模型
openstack nova启动时首先通过命令行或者dashborad填写创建信息,然后通过restful api的方式调用openstack服务去创建虚拟机.数据信息从客户端到达openstack服 ...
- 【WebGL】《WebGL编程指南》读书笔记——第2章
一.前言 最近看了<WebGL编程指南>这本书,发现还是很有意思的,故每章阅读后做个笔记. 二.正文 Example1:在canvas中绘制矩形 <!DOCTYPE html> ...
- Python学习(四):模块入门
1.模块介绍 模块:代码实现的某个功能的集合 模块分类: 自定义模块 内置标准模块 开源模块 模块的常用方法: 是否为主文件:__name__ == '__main__' 如果是直接执行的某程序,那么 ...
- jmeter通过org.sqlite.JDBC驱动连接db数据库
最近遇到个项目,默认业务库为内置db数据库,在性能脚本编辑过程中要通过正则表达式提取器(关联)获取对应的id号,通过该id号到db数据库中查找对应的数据源name字段内容,为下一个post请求做par ...
- 阅读MDN文档之CSS选择器介绍(一)
本文为阅读MDN文档笔记 目录 Different types of Selectors Attribute Selectors Presence and value attribute select ...
- Oracle学习笔记_10_判断是否为日期类型
FUNCTION isdate (datestr VARCHAR2, format VARCHAR2) RETURN number IS p_date DATE; BEGIN SELECT TO_DA ...