本文介紹使用SpringMVC+Spring+MyBatis三大框架使用Maven快速搭建一個demo,實現數據從資料庫中查詢返回到頁面進行展示的過程。 技術選型:SpringMVC+Spring+MyBatis+MySql+Maven+JSP+IDEA+Tomcat7+JDK1.8+Navicat ...
本文介紹使用SpringMVC+Spring+MyBatis三大框架使用Maven快速搭建一個demo,實現數據從資料庫中查詢返回到頁面進行展示的過程。
技術選型:SpringMVC+Spring+MyBatis+MySql+Maven+JSP+IDEA+Tomcat7+JDK1.8+Navicat for MySql
下麵開始搭建項目:
一、資料庫環境搭建
1、首先使用Navicat for MySql連接MySql資料庫,創建一個新的資料庫或者使用某一個已經存在的資料庫,我這裡新創建了一個test資料庫,並新建一張user表,建表SQL和插入測試數據的SQL如下:
<!--SQL建表語句--> CREATE TABLE `user` ( `id` INT (50) NOT NULL, `name` VARCHAR (100) NOT NULL, `age` INT (50) NOT NULL, `address` VARCHAR (200) NOT NULL, PRIMARY KEY (`id`) ); <!--插入數據--> INSERT INTO USER (id,name,age,address)VALUES(1,'小謝',18,'上海市'); INSERT INTO USER (id,name,age,address)VALUES(2,'小尚',18,'上海市'); INSERT INTO USER (id,name,age,address)VALUES(3,'小楠',18,'天津市'); INSERT INTO USER (id,name,age,address)VALUES(4,'小勛',18,'鄭州市'); INSERT INTO USER (id,name,age,address)VALUES(5,'小喬',18,'鄭州市'); INSERT INTO USER (id,name,age,address)VALUES(6,'小威',18,'鄭州市'); INSERT INTO USER (id,name,age,address)VALUES(7,'小歡',18,'武漢市');
二、創建一個Maven項目並補全項目目錄和相關包名
1、打開IDEA,新建一個項目;
2、新建一個為ssmDemo的Maven項目,使用idea創建骨架,選擇webapp進行創建:
3、項目GroupId和ArtifactId自己定義即可:
4、配置本地Maven路徑:
5、配置項目名稱和項目路徑,點擊Finish按鈕即可創建項目,等待項目初始化完成:
6、項目初始化完成後,項目目錄結構如下:
7、補充項目路徑完整,在main文件夾下麵創建java和resources文件夾,並作為項目資源文件夾;在src目錄下新建test文件夾,併在test文件夾下麵創建java和resources文件夾,並作為項目資源文件夾,test文件夾留做單元測試時使用:
8、在src/java目錄下麵創建controller、dao、domain、service、service.impl包;
三、SSM配置文件整合
1、pom.xml文件中的相關jar包的坐標,在pom.xml中添加完jar包坐標後Maven會自動下載相關的jar包,SSM整合所需要的jar包說明在註釋中已經註明;
<?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.xyfer</groupId> <artifactId>ssmDemo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <!-- 統一管理jar包版本 --> <properties> <spring.version>5.0.2.RELEASE</spring.version> <slf4j.version>1.6.6</slf4j.version> <log4j.version>1.2.12</log4j.version> <shiro.version>1.2.3</shiro.version> <mysql.version>5.1.6</mysql.version> <mybatis.version>3.4.5</mybatis.version> <spring.security.version>5.0.1.RELEASE</spring.security.version> <!--編譯編碼--> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- Mybatis 和 mybatis 與 spring 的整合 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.8</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!--junit測試--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!--servlet--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <!--jstl--> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- log --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!--alibaba連接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.9</version> </dependency> </dependencies> </project>
如果項目名下麵有紅色波浪線,則是缺少相應的jar包,使用Maven更新jar即可解決:
2、在src/resources文件夾下麵添加springmvc.xml和applicationContext.xml文件
3、在web.xml文件配置DispatcherServlet和spring監聽器,載入springmvc.xml和applicationContext.xml文件;
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- 前端控制器 載入springmvc容器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <!-- 監聽器 載入 spring 容器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value> </context-param> </web-app>
4、在springmvc.xml中配置controller包掃描和視圖解析器;
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!--配置controller註解掃描--> <context:component-scan base-package="com.xyfer.controller"></context:component-scan> <!-- 配置視圖解析器的首碼和尾碼 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
5、在applicationContext.xml配置資料庫連接信息,dao和service包掃描;
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 資料庫連接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <!-- 驅動 --> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <!-- url --> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <!-- 用戶名 --> <property name="username" value="root" /> <!-- 密碼 --> <property name="password" value="root" /> </bean> <!-- mapper 配置 --> <!-- 讓 spring 管理 sqlsessionfactory 使用mybatis和spring整合包中的 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 資料庫連接池 --> <property name="dataSource" ref="dataSource" /> <!--pojo位置--> <property name="typeAliasesPackage" value="com.xyfer.domain"></property> <!--mapper文件位置--> <property name="mapperLocations" value="classpath:com.xyfer.dao/*Mapper.xml"/> </bean> <!-- mapper 掃描器 :用來產生代理對象--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.xyfer.dao"></property> </bean> <!--配置註解掃描dao代替上麵包名的配置--> <!--<context:component-scan base-package="com.xyfer.dao"/>--> <!--配置註解掃描service--> <context:component-scan base-package="com.xyfer.service"/> </beans>
6、日誌文件log4j.properties放在src/resources文件夾下麵;
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5
p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to
'debug' ###
#在開發階段日誌級別使用 debug
log4j.rootLogger=debug, stdout
### 在日誌中輸出 sql 的輸入參數 ###
log4j.logger.org.hibernate.type=TRACE
至此,SSM(SpringMVC+Spring+MyBatis)三大框架整合搭建,配置文件已經整合完成。
下麵開始創建java類和介面,以實現資料庫數據到前端jsp頁面的展示:
四、實體類和controller、service、dao介面的創建;
1、根據資料庫中user表欄位和實體類的映射關係,在src/main/java/com/xyfer/domain下麵創建實體類User.java;
package com.xyfer.domain; public class User { public int id; public String name; public int age; public String address; 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", address='" + address + '\'' + '}'; } }
2、在src/main/java/com/xyfer/dao下麵創建UserDao,並創建一個findUserById的方法;
package com.xyfer.dao; import com.xyfer.domain.User; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; /** * dao層 */ @Repository public interface UserDao { /** * 根據id查詢user * @param