最近一年一直在做IOS开发,好久没碰java,有个朋友要帮忙开发一个小网站,所以根据需求spring的MVC加上jdbcTemplate不乏是一个很不错的选择,简单!
简单介绍下所用到的lib包:
spring-framework-3.0.5.RELEASE和一些依赖包,外加数据库连接的jar包也是最新的MySQL-connector-java-5.1.22-bin.jar
数据库用的是:
mariadb-5.5.25-win32.msi——算是更新的比较快,以前叫mysql,现在咋改这个名字了,不过安装过后有了图形界面,不错!
下面介绍一下项目的搭建过程,简单介绍主要内容,仅供参考:
(1)我用的是myeclipse6.5(比较怀旧,这个版本还是比较好用的,没有太多需求的情况下而且不懂优化myeclipse的情况下不建议使用一些新版本,吃内存太猛),新建一个Web Project,使用JAVA EE5.0,第一步引入上面的lib包。
(2)修改web.xml,如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5" 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">
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>
- org.springframework.web.servlet.DispatcherServlet
- </servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>*.qq</url-pattern>
- </servlet-mapping>
- </web-app>
- <?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-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <!-- 自动扫描bean,把作了注解的类转换为bean -->
- <context:component-scan base-package="com.test.controller" />
- <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
- <bean
- class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
- <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
- <bean
- class="org.springframework.web.servlet.view.InternalResourceViewResolver"
- p:prefix="/WEB-INF/view/" p:suffix=".jsp" />
- <!-- apache.dbcp连接池的配置 -->
- <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/test"></property>
- <property name="username" value="root"></property>
- <property name="password" value="test"></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="com.test.service.imp.PersonServiceImp">
- <property name="jdbcTemplate" ref="jdbcTemplate"></property>
- </bean>
- </beans>
(4)建jsp页面,我建议最好是建立在WEB-INF下,新建/WEB-INF/view/user/MyJsp.jsp文件,也就是MVC中的V。
- <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
- <%@page import="com.test.model.Person"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- Person person = (Person)request.getAttribute("person");
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'MyJsp.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- </head>
- <body>
- <%=person.getId() %>
- <br>
- <%=person.getName() %>
- </body>
- </html>
(5)现在开始建java文件,MVC中的C和M,外加处理业务的Service层
Model层的Person
- package com.test.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;
- }
- }
Service抽象层,对于简单的项目就没必要再加DAO层了,影响开发效率,而且起不了啥作用。
- package com.test.service;
- import com.test.model.Person;
- public interface PersonService {
- void save(Person person);
- void del(Person person);
- void update(Person person);
- void searchAll();
- }
Serview的Implate实现层,此处将jdbcTemplate在xml中注入进来,数据库的操作全放在这里面实现。
- package com.test.service.imp;
- import org.springframework.jdbc.core.JdbcTemplate;
- import com.test.model.Person;
- import com.test.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 person 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;
- }
- }
最后介绍Controller类,结合上面的spring-servlet.xml会更容易理解些
- package com.test.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 com.test.model.Person;
- import com.test.service.PersonService;
- @Controller
- @RequestMapping("/test")
- public class TestController {
- @Autowired
- PersonService personService;
- @RequestMapping(value="start.qq",method={RequestMethod.GET})
- public String startMVC (HttpServletRequest request,ModelMap modelMap){
- 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";
- }
- }
(7)查询数据库