最近一年一直在做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,如下:
index.jsp
spring
org.springframework.web.servlet.DispatcherServlet
1
spring
*.qq
(3)在WEB-INF目录下新建spring-servlet.xml
(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");
%>
My JSP 'MyJsp.jsp' starting page
<%=person.getId() %>
<%=person.getName() %>
(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;
}
}
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();
}
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;
}
}
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";
}
}
(6)浏览器访问 http://localhost:8080/testV1/test/start.qq?id=111&name=daleiwang
(7)查询数据库