eclipse下SpringMVC+Maven+Mybatis+MySQL項目搭建html
這篇文章主要講解在eclipse環境下SpringMVC+Maven+Mybatis+MySQL的項目搭建過程。java
右擊-->New->Othermysql
點擊->Manven Porjectweb
點擊->勾選快速框架spring
輸入項目名,包(Packaging,若是隻是普通的項目,選jar就行了,若是是web項目就選war,這裏是web項目,因此選擇war)sql
右擊項目->Properties->Java Build Path->選擇Libraries->點擊選中 JRE System Library->Edit 數據庫
選擇默認JRE,注:必須系統安裝了JDK。apache
點擊Project Facets 去掉勾選 Dynamic Web Module 再把 Java 改成系統設置版本。 api
再打開Project Facets ->勾選 Dynamic Web Module 選擇3.0版本時,下面有個 Further configuration available... ->點擊瀏覽器
,
輸入 content directory 地址: src/main/webapp
建立好的目錄以下:
添加包的依賴,編輯pom.xml文件添加以下依賴:(由於個人JDK版本是1.8因此Spring 版本必須爲4.0以上)
<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.liaoze</groupId> <artifactId>stockmgt_maven_project</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <!-- spring版本號 --> <spring.version>4.1.5.RELEASE</spring.version> <!-- log4j日誌文件管理包版本 --> <slf4j.version>1.6.6</slf4j.version> <log4j.version>1.2.12</log4j.version> <!-- junit版本號 --> <junit.version>4.10</junit.version> <!-- mybatis版本號 --> <mybatis.version>3.2.1</mybatis.version> </properties> <dependencies> <!-- 添加Spring依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</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-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-aspects</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-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <!--單元測試依賴 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- 日誌文件管理包 --> <!-- log start --> <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> <!-- log end --> <!--spring單元測試依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <!--mybatis依賴 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- mybatis/spring包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.0</version> </dependency> <!-- mysql驅動包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.29</version> </dependency> </dependencies> </project>
加入配置文件
jdbc.properties
jdbc_driverClassName=com.mysql.jdbc.Driver jdbc_url=jdbc:mysql://localhost:3306/stockmgt jdbc_username=root jdbc_password=123456
application.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 引入jdbc配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:properties/*.properties</value> <!--要是有多個配置文件,只需在這裏繼續添加便可 --> </list> </property> </bean> <!-- 配置數據源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <!-- 不使用properties來配置 --> <!-- <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/learning" /> <property name="username" value="root" /> <property name="password" value="christmas258@" /> --> <!-- 使用properties來配置 --> <property name="driverClassName"> <value>${jdbc_driverClassName}</value> </property> <property name="url"> <value>${jdbc_url}</value> </property> <property name="username"> <value>${jdbc_username}</value> </property> <property name="password"> <value>${jdbc_password}</value> </property> </bean> <!-- 自動掃描了全部的XxxxMapper.xml對應的mapper接口文件,這樣就不用一個一個手動配置Mpper的映射了,只要Mapper接口類和Mapper映射文件對應起來就能夠了。 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.liaoze.stockmgt.dao" /> </bean> <!-- 配置Mybatis的文件 ,mapperLocations配置**Mapper.xml文件位置,configLocation配置mybatis-config文件位置--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:mapper/*.xml"/> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" /> <!-- <property name="typeAliasesPackage" value="com.tiantian.ckeditor.model" /> --> </bean> <!-- 自動掃描註解的bean --> <context:component-scan base-package="com.liaoze.stockmgt.service" /> </beans>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> </configuration>
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <mvc:annotation-driven /> <!-- 掃描controller(controller層注入) --> <context:component-scan base-package="com.liaoze.stockmgt.controller"/> <!-- 對模型視圖添加先後綴 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/" p:suffix=".jsp"/> </beans>
userMapper.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.liaoze.stockmgt.dao.UserDao"> <!--設置domain類和數據庫中表的字段一一對應,注意數據庫字段和domain類中的字段名稱不致,此處必定要!--> <resultMap id="BaseResultMap" type="com.liaoze.stockmgt.model.User"> <id column="USER_ID" property="userId" jdbcType="INTEGER" /> <result column="USER_NAME" property="userName" jdbcType="CHAR" /> <result column="USER_PASSWORD" property="userPassword" jdbcType="CHAR" /> </resultMap> <!-- 查詢單條記錄 --> <select id="selectUserById" parameterType="int" resultMap="BaseResultMap"> SELECT * FROM t_user WHERE USER_ID = #{userId} </select> </mapper>
進入mysql 數據庫:
/*新建數據庫*/ CREATE DATABASE IF NOT EXISTS stockmgt DEFAULT CHARSET utf8; /*新建表*/ COLLATE utf8_general_ci; CREATE TABLE `t_user` ( `USER_ID` INT(11) NOT NULL AUTO_INCREMENT, `USER_NAME` CHAR(30) NOT NULL, `USER_PASSWORD` CHAR(10) NOT NULL, PRIMARY KEY (`USER_ID`), KEY `IDX_NAME` (`USER_NAME`) ) ENGINE=INNODB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8; /*插入測試數據*/ INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD) VALUES (1, '張三', '123456'); INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD) VALUES (2, '李四', '123456');
編寫java類:
User.java
package com.liaoze.stockmgt.model; public class User { private Integer userId; private String userName; private String userPassword; public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } }
UserDao.java
package com.liaoze.stockmgt.dao; import com.liaoze.stockmgt.model.User; public interface UserDao { /** * @param userId * @return User */ public User selectUserById(Integer userId); }
UserService.java
package com.liaoze.stockmgt.service; import com.liaoze.stockmgt.model.User; public interface UserService { User selectUserById(Integer userId); }
UserServiceImpl.java
package com.liaoze.stockmgt.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.liaoze.stockmgt.dao.UserDao; import com.liaoze.stockmgt.model.User; import com.liaoze.stockmgt.service.UserService; @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; public User selectUserById(Integer userId) { return userDao.selectUserById(userId); } }
編寫測試類JUnit4進行單元測試:
SpringTestCase.java
package com.liaoze.stockmgt.test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; //指定bean注入的配置文件 @ContextConfiguration(locations = { "classpath:application.xml" }) //使用標準的JUnit @RunWith註釋來告訴JUnit使用Spring TestRunner @RunWith(SpringJUnit4ClassRunner.class) public class SpringTestCase extends AbstractJUnit4SpringContextTests { }
UserServiceTest.java
package com.liaoze.stockmgt.test.service; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import com.liaoze.stockmgt.model.User; import com.liaoze.stockmgt.service.UserService; import com.liaoze.stockmgt.test.SpringTestCase; public class UserServiceTest extends SpringTestCase { @Autowired private UserService userService; @Test public void selectUserByIdTest(){ User user = userService.selectUserById(1); System.out.println(user.getUserName() + ":" + user.getUserPassword()); } }
選中方法名->右擊->Run As -> 點擊 JUnit Test
顯示測試運行成功!
控制檯輸出以下:
新建Controller:
新建UserController.java
package com.liaoze.stockmgt.controller; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.liaoze.stockmgt.model.User; import com.liaoze.stockmgt.service.UserService; @Controller public class UserController { @Resource private UserService userService; @RequestMapping("/") public ModelAndView getIndex(){ ModelAndView mav = new ModelAndView("index"); User user = userService.selectUserById(1); mav.addObject("user", user); return mav; } }
添加jsp 頁面:在src/main/webapp/WEB-INF/新建view文件夾
再新建index.jsp 頁面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <body> <h2>Hello World!</h2> 用戶名: ${user.userName}<br> 密碼:${user.userPassword}<br> </body> </html>
運行!
瀏覽器輸入地址:http://127.0.0.1:8080/stockmgt_maven_project/
無亂碼!成功!
源代碼:http://download.csdn.net/detail/liaoze2010/9854936
個人CSDN博客:http://blog.csdn.net/liaoze2010
最近實在是沒分了,因此源代碼下載,所須要1分的積分,見涼!
參考:http://www.open-open.com/lib/view/open1453195087105.html