一個基於java web+spring mvc+mysql+maven的helloworld
阿新 • • 發佈:2019-01-06
首先感謝網上的提供學習資源的朋友,剛開始學習,借鑑了你們很多知識。
剛學java,文章中難免有很多疏漏,我會慢慢改,也希望各位幫忙斧正。
本文使用maven構建專案,編寫了一個java web小程式,在這其中用到了spring mvc框架和連線mysql資料庫技術。
一、
1.mvn archetype:generate //獲取maven可以建立的專案框架列表
2.輸入 maven-archetype-webapp得到webapp專案的專案編號,我獲得的為1
3.輸入1,在當前資料夾下得到了一個webapp的專案
二、
1.更改專案骨架(我的不一定正確)
三、
1.pom檔案修改,新增spring mvc,資料庫,日誌相關操作的jar包,在pom檔案中新增依賴之後,maven會現在本地倉庫中查詢是否有相關jar包,如果沒有會從網上下載到本地倉庫,同時說明下,在pom中新增依賴之後,就不需要在eclipse中將jar包匯入到classpath,即不需要對此介面進行操作。
2.pom檔案程式碼如下:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wcy</groupId>
<artifactId>springWeb</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name> springWeb Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.2.9.RELEASE</spring.version>
<junit.version>4.11</junit.version>
<jdk.version>1.6</jdk.version>
<jetty.version>8.1.8.v20121106</jetty.version>
</properties>
<dependencies>
<!-- Spring 3 dependencies -->
<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-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- mysql-connector-java jar -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.28</version>
</dependency>
<!-- 資料庫連線池 jar -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>springWeb</finalName>
</build>
</project>
四、
1.web.xml檔案程式碼
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Counter Web 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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
2.mvc-dispatcher-servlet.xml檔案程式碼
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--註解驅動 -->
<mvc:annotation-driven/>
<!-- 掃描器 -->
<context:component-scan base-package="com" />
<!-- 獲取配置檔案 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/database.properties</value>
</property>
</bean>
<!-- 獲取資料來源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>${db.driverClassName}</value>
</property>
<property name="url">
<value>${db.url}</value>
</property>
<property name="username">
<value>${db.username}</value>
</property>
<property name="password">
<value>${db.password}</value>
</property>
</bean>
<!--定義JDBC模板Bean-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
p:dataSource-ref="dataSource"/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
3.database.properties檔案程式碼(記得把明顯不是固定格式的內容改成你自己的哈,比如//192.168.0.200:3306/web就是指的資料庫的ip,port,還有資料庫的名稱)
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://192.168.0.200:3306/web?autoReconnect=true&useUnicode=true&characterEncoding=utf8
db.username=root
db.password=xxx
4.WebController.java檔案程式碼(url對映是否成功,可以啟動程式檢視,它會輸出資訊,顯示哪些url繫結在WebController上)
package com.wcy.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.wcy.dao.WebDao;
@Controller
@RequestMapping("/")
public class WebController {
@Autowired
private WebDao webDao;
@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public String welcome(ModelMap model) {
model.addAttribute("message",
"Maven Web Project + Spring 3 MVC - welcome()");
webDao.insertQuery();
// Spring uses InternalResourceViewResolver and return back index.jsp
return "index";
}
@RequestMapping(value = "/hello.do/{name}", method = RequestMethod.GET)
public String welcomeName(@PathVariable String name, ModelMap model) {
model.addAttribute("message", "Maven Web Project + Spring 3 MVC - "
+ name);
return "index";
}
}
5.WebDao.java檔案程式碼
package com.wcy.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
//Spring 在容器初始化時將自動掃描 base-package 指定的包及其子包下的所有 class檔案,所有標註了 @Repository 的類都將被註冊為 Spring Bean。預設bean的名字為首字母小寫的類名。
@Repository
public class WebDao{
private String msg;
@Autowired
private JdbcTemplate jdbcTemplate;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int insertQuery() {
String sql = "insert into user_message values(?,?) ";
return jdbcTemplate.update(sql,new Object[] {"hello", "wcy"});
}
}
五、例子程式中沒有用到UserPo就不貼啦,還有最好寫一個service類來操作資料庫,直接使用dao類操作資料庫太底層了,不好管理,應該還有別的原因,哈哈,暫時不知道。
六、最後再說下,真心求交流,哈哈,大家互相學習。