搭建Spring MVC+ jdbcTemplate框架

开发环境:

JDK:1.8

Tomcat:8.0.35

MySql:5.6.28

Myeclipse:2015 Stable 2.0

Maven:3.39

工具安装过程不再赘述,下面直接从零开始搭建Spring MVC+ jdbcTemplate框架

Maven默认配置下载会有点慢,点这里参看优化配置方法。


1.进入命令行,切换到目标文件夹下,注意:此步不用创建项目的文件夹。

2. 在命令行输入:mvn  archetype:generate  -DgroupId=公司名或公司域名  -DartifactId=项目名称   -DarchetypeArtifactId=maven-archetype-webapp   -DinteractivMode=false      -DarchetypeCatalog=internal,出现下图说明创建成功,文件夹内会出现项目文件夹


3.运行Myeclipse,点击菜单栏File-》Import-》,选择“Existing Maven Projects”



点击“Finish”

4.在/src/main/下创建java文件夹

5.对项目名称点右键-》Properties-》Java Build Path,选择Libraries选项卡:

a.Add Library -》JRE System Library,添加系统的JRE版本,这里用的是Spring 4.X,需要使用1.6以上的JRE版本。

b.Add Library -》MyEclipse Server Library,选择系统中安装的Tomcat8

6.双击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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>公司名或公司域名</groupId>
  <artifactId>项目名称</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>springtest Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <profiles>
    <profile>
        <id>jdk-1.8</id>
        <!-- 另外一种激活方式 -->
        <activation>
            <activeByDefault>true</activeByDefault>
            <jdk>1.8</jdk>
        </activation>
        <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
			<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
        </properties>
    </profile>
   </profiles> 
   
   <properties>  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
        <spring.version>4.1.4.RELEASE</spring.version>  
        <jackson.version>2.5.0</jackson.version>  
    </properties>  
  
  <dependencies>
	  <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>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-core</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-context</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-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>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-test</artifactId>  
            <version>${spring.version}</version>  
            <scope>test</scope>  
        </dependency>  
  
        <!-- log4j -->  
        <dependency>  
            <groupId>log4j</groupId>  
            <artifactId>log4j</artifactId>  
            <version>1.2.17</version>  
        </dependency>  
  
        <!-- mysql连接 -->  
        <dependency>  
            <groupId>mysql</groupId>  
            <artifactId>mysql-connector-java</artifactId>  
            <version>5.1.34</version>  
        </dependency>  
  
        <!-- c3p0数据源 -->  
        <dependency>  
            <groupId>com.mchange</groupId>  
            <artifactId>c3p0</artifactId>  
            <version>0.9.5-pre10</version>  
        </dependency>  
  
        <!-- json -->  
        <dependency>  
            <groupId>com.alibaba</groupId>  
            <artifactId>fastjson</artifactId>  
            <version>1.2.3</version>  
        </dependency>  
  
        <dependency>  
            <groupId>com.fasterxml.jackson.core</groupId>  
            <artifactId>jackson-annotations</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-databind</artifactId>  
            <version>${jackson.version}</version>  
        </dependency>  
  
        <!-- servlet -->  
        <dependency>  
            <groupId>javax.servlet</groupId>  
            <artifactId>servlet-api</artifactId>  
            <version>3.0-alpha-1</version>  
            <scope>provided</scope>  
        </dependency>  
  
        <dependency>  
            <groupId>javax.servlet</groupId>  
            <artifactId>jstl</artifactId>  
            <version>1.2</version>  
        </dependency>  
  
        <!-- 文件上传 -->  
        <dependency>  
            <groupId>commons-io</groupId>  
            <artifactId>commons-io</artifactId>  
            <version>2.4</version>  
        </dependency>  
  
        <dependency>  
            <groupId>commons-fileupload</groupId>  
            <artifactId>commons-fileupload</artifactId>  
            <version>1.2.2</version>  
        </dependency>  
  </dependencies>
  <build>
    <finalName>项目名称</finalName>
  </build>
</project>
7.对项目名称点右键,选择Maven4Myeclipse-》Update Project,更新完成后,可部署至Tomcat浏览默认的Index.jsp页面。

8.在/springtest/src/main/java下(这里的项目名称为springtest),分别创建包:springtest.controller,springtest.model,springtest.service,springtest.Service.Impl。

9.在springtest.model下创建Person.java:

package springtest.model;

public class Person {  
    
    private String name;  
      
    private int id;  
      
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  
  
}  
10.在springtest.service下创建PersonService.java:

package springtest.service;

import springtest.model.Person;  

public interface PersonService {  
    void save(Person person);    
    void del(Person person);    
    void update(Person person);    
    void searchAll();  
}  
11.在springtest.service.Impl下创建PersonServiceImp.java:

package springtest.service.Impl;

import org.springframework.jdbc.core.JdbcTemplate;  
import springtest.model.Person;  
import springtest.service.PersonService; 

public class PersonServiceImp implements PersonService{
	private JdbcTemplate jdbcTemplate;    
    
    public void del(Person person) {  
        // TODO Auto-generated method stub  
          
    }  
      
    public void save(Person person) {  
          
        // TODO Auto-generated method stub  
          
        jdbcTemplate.update("insert into user_info values(?,?)",new Object[]{person.getId(),person.getName()});  
          
    }  
  
    public void searchAll() {  
        // TODO Auto-generated method stub  
          
    }  
  
    public void update(Person person) {  
        // TODO Auto-generated method stub  
          
    }  
  
    public JdbcTemplate getJdbcTemplate() {  
        return jdbcTemplate;  
    }  
  
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {  
        this.jdbcTemplate = jdbcTemplate;  
    }  
  
}
12.现在配置Web.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_3_0.xsd" id="WebApp_ID" version="3.0">   
    <display-name>springtest</display-name>  
    
    <welcome-file-list>  
        <welcome-file>/index.jsp</welcome-file>  
    </welcome-file-list>      
    
    <servlet>  
        <servlet-name>rest</servlet-name>  
        <servlet-class>  
            org.springframework.web.servlet.DispatcherServlet  
        </servlet-class>  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>/WEB-INF/classes/rest-servlet.xml</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>rest</servlet-name>  
        <url-pattern>*.qq</url-pattern>  
        <url-pattern>*.html</url-pattern>  
    </servlet-mapping>  
</web-app>  
13.在/springtest/src/main/resources下创建Servlet.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:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
   
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
http://www.springframework.org/schema/context   
http://www.springframework.org/schema/context/spring-context-4.0.xsd">  
        
      <context:component-scan base-package="springtest.controller" />  
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />  
    
   <bean  
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
        p:prefix="/WEB-INF/views/" p:suffix=".jsp" />  
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
        destroy-method="close">  
        <property name="driverClassName" value="com.mysql.jdbc.Driver">  
        </property>  
        <property name="url" value="jdbc:mysql://localhost:3306/数据库名"></property>  
        <property name="username" value="root"></property>  
        <property name="password" value="tiger"></property>  
        <property name="maxActive" value="100"></property>  
        <property name="maxIdle" value="30"></property>  
        <property name="maxWait" value="500"></property>  
        <property name="defaultAutoCommit" value="true"></property>  
    </bean>  
    
    <!-- jdbc注入 -->  
    <bean id="jdbcTemplate"  
        class="org.springframework.jdbc.core.JdbcTemplate">  
        <property name="dataSource">  
            <ref bean="dataSource" />  
        </property>  
    </bean>  
      
    <!-- dao注入 -->  
    <bean id="personService" class="springtest.service.Impl.PersonServiceImp">  
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>  
    </bean>  
</beans>  

14.在上面代码指定的数据库中创建表user_info,字段:


15.在springtest.controller下创建NewController.java:

package springtest.controller;

import javax.servlet.http.HttpServletRequest;  

import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Controller;  
import org.springframework.ui.ModelMap;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestMethod;  
  
import springtest.model.Person;  
import springtest.service.PersonService;  

@Controller
@RequestMapping("/city")  
public class NewController {  
	@Autowired  
    PersonService personService;  
      
    @RequestMapping(value="start.qq",method={RequestMethod.GET})  
    public String startMVC (HttpServletRequest request,ModelMap modelMap){  

        System.out.println("ok");  
        Person person = new Person();  
        person.setId(Integer.parseInt(request.getParameter("id").toString()));  
        person.setName(request.getParameter("name"));  
        personService.save(person);  
        System.out.println(person.getId());  
        System.out.println(person.getName());  
        modelMap.put("person",person);  
          
        return "user/MyJsp";  
    }   
}  
16.创建如下图所示的jsp文件:

17.home.jsp代码如下:

<html>  
<head>  
<title>Spring MVC Tutorial by Defonds - Hello World Spring MVC  
    Example</title>  
<style type="text/css">  
body {  
    background-image: url('https://img-blog.csdn.net/20150827184458936');  
}  
</style>  
</head>  
<body>${message}  
   
    <br>  
    <br>  
</body>  
</html>  

好了,重启tomcat,在浏览器访问http://127.0.0.1:8080/springtest/city/start.qq?id=111&name=daleiwang,效果如下:


这个时候再去看数据库,就已经添加了这条数据啦。


这里感谢两位大神的博文指导:

http://blog.csdn.net/defonds/article/details/48031593

http://blog.csdn.net/daleiwang/article/details/8100800











发布了62 篇原创文章 · 获赞 26 · 访问量 24万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览