使用SpringMVC进行数据的传送,数据来自mysql数据库 (1)没有使用hibernate
2017-06-26 11:16
417 查看
使用SpringMVC做的一个简单传值的程序,调用数据库的没有使用hibernate 下一篇就会使用hibernate。
要下载源文件点击:
下载
下面我们开始讲解springmvc的传值过程吧:
一:我们使用eclipse建一个动态web工程
如图:
选择:Dynamic Web Project 下一步输入项目名,然后下一步,下一步把xml那个复选框选上完成。
二:导入Spring jar包
三:修改web.xml文件
四:在src目录下创建一个Spring的配置文件
我创建的是springmvc.xml,如果在其他目录下创建的那么在web.xml中就要修改
比如在WEB-INF目录下的那么web.xml 中的
改为
五:建立包:可以参考下图
六:各个类的源码:
(1)Student类
(2)StudentService接口
(3)实现StudentService接口的类studentImpl
(4)连接数据库Jdbc类
[b](5)StudentController 类
七:Spring配置文件的修改:
八:视图jsp 的书写:
(1)listForm.jsp
(2)editForm.jsp
(3)addForm.jsp
九:数据库的创建
使用命令建数据库
命令如下:
创建数据库test
create database test;创建数据表information,name作为主键use test;create table information(
id int,
name varchar(30) primary key,
sex varchar(8),
age int,
address varchar(200));
添加三条数据:
insert into information(id, name,sex,age,address)
values(1,"A","Man",21,"wuhan"),
(2,"B","Woman",20,"shenzhen"),
(3,"C","Man",2,"yunnan");
十:启动页面的建立
直接在WebContent目录下新建一个index.jsp文件
添加这一句
[b]十一:各个页面个截图
[/b]
点击student启动
listForm.jsp页面
addForm.jsp页面
editForm.jsp页面
执行了添加,修改,删除后的listForm.jsp页面
要下载源文件点击:
下载
下面我们开始讲解springmvc的传值过程吧:
一:我们使用eclipse建一个动态web工程
如图:
选择:Dynamic Web Project 下一步输入项目名,然后下一步,下一步把xml那个复选框选上完成。
二:导入Spring jar包
三:修改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"> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
<filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
四:在src目录下创建一个Spring的配置文件
我创建的是springmvc.xml,如果在其他目录下创建的那么在web.xml中就要修改
比如在WEB-INF目录下的那么web.xml 中的
<param-value>classpath:springmvc.xml</param-value>
改为
<param-value>/WEB-INF/springmvc.xml</param-value>
五:建立包:可以参考下图
六:各个类的源码:
(1)Student类
package com.springmvc.domain; public class Student { private Integer id; private String name; private String sex; private Integer age; private String address; public Student(){ } public Student(Integer id, String name, String sex, Integer age, String address) { super(); this.id = id; this.name = name; this.sex = sex; this.age = age; this.address = address; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
(2)StudentService接口
package com.springmvc.service; import java.util.List; import com.springmvc.domain.Student; public interface StudentService { List<Student> getStudents(); void add(Student student); void udpate(Student student); void delete(String name); Student get(String name); }
(3)实现StudentService接口的类studentImpl
package com.springmvc.service; import java.awt.print.Book; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Service; import com.springmvc.domain.Jdbc; import com.springmvc.domain.Student; @Service public class studentImpl implements StudentService{ private List<Student> students; public studentImpl(){ students = new ArrayList<Student>(); } public List<Student> getStudents() { // TODO 自动生成的方法存根 Jdbc a = new Jdbc(); students = a.getAllStudents(); return students; } public void add(Student student) { Jdbc a = new Jdbc(); a.insert(student); } public void udpate(Student student) { Jdbc a = new Jdbc(); a.update(student); } public void delete(String name) { Jdbc a = new Jdbc(); a.deleteStudent(name); } public Student get(String name) { Jdbc a = new Jdbc(); return a.getStudent(name); } }
(4)连接数据库Jdbc类
package com.springmvc.domain; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import java.sql.ResultSet; import java.sql.SQLException; public class Jdbc { private Connection con; private Statement statement; private ResultSet rest; public Jdbc(){ String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8"; String username = "root"; String password = "123456"; try{ Class.forName(driver); con =DriverManager.getConnection(url, username, password); statement = con.createStatement(); System.out.println("成功连接数据库了,开心得一匹!"); }catch(ClassNotFoundException e){ e.printStackTrace(); }catch(SQLException e){ e.printStackTrace(); } } //从数据库中获取值,转换成Student类型 public List<Student> getAllStudents(){ String sql = "select * from information"; List<Student> students = new ArrayList<Student>(); try{ rest = statement.executeQuery(sql); while(rest.next()){ int id = rest.getInt(1); String name = rest.getString(2); String sex = rest.getString(3); int age = rest.getInt(4); String address = rest.getString(5); students.add(new Student(id,name,sex,age,address)); } con.close(); statement.close(); rest.close(); return students; }catch(SQLException e){ e.printStackTrace(); } return students; } //修改数据库中的数据 public boolean update(Student student){ boolean zhuanhuan =false; String sql ="update information set sex=\""+student.getSex() +"\",age="+student.getAge() +",address=\"" +student.getAddress()+"\" where name=\""+ student.getName()+"\""; try{ int a = statement.executeUpdate(sql); zhuanhuan = true; con.close(); statement.close(); return zhuanhuan; }catch(SQLException e){ e.printStackTrace(); } return zhuanhuan; } //获取指定的某个数据 public Student getStudent(String name){ String sql = "select * from information where name=\""+name+"\""; Student student = null; try{ rest = statement.executeQuery(sql); while(rest.next()){ int id = rest.getInt(1); String username = rest.getString(2); String sex = rest.getString(3); int age = rest.getInt(4); String address = rest.getString(5); student = new Student(id,username,sex,age,address); } con.close(); statement.close(); rest.close(); return student; }catch(SQLException e){ e.printStackTrace(); } return student; } //添加数据的函数 public boolean insert(Student student){ boolean Toadd = false; String sql ="insert into information(id,name,sex,age,address) values("+ student.getId()+",\"" +student.getName()+"\",\"" +student.getSex()+"\"," +student.getAge()+",\"" +student.getAddress()+"\")"; try{ int a = statement.executeUpdate(sql); Toadd = true; con.close(); statement.close(); return Toadd; }catch(SQLException e){ e.printStackTrace(); } return Toadd; } //删除操作根据主键name删除 public boolean deleteStudent(String name){ boolean delete = false; String sql = "delete from information where name=\""+name+"\""; try{ int a = statement.executeUpdate(sql); delete = true; return delete; }catch(SQLException e){ e.printStackTrace(); } return delete; } }
[b](5)StudentController 类
package com.springmvc.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import com.springmvc.domain.Student; import com.springmvc.service.StudentService; @Controller public class StudentController { @Autowired public StudentService studentservice; @RequestMapping("/student_add") public String addStudent(Model model){ final String [] sexs = {"Women","Man"}; model.addAttribute("sexs",sexs); model.addAttribute("student",new Student()); return "addForm"; } @RequestMapping("/student_edit/{name}") public String edit(Model model,@PathVariable String name){ final String [] sexs = {"Women","Man"}; model.addAttribute("sexs",sexs); Student student = studentservice.get(name); System.out.println(student.toString()); model.addAttribute("student",student); return "editForm"; } @RequestMapping("/student_save") public String saveStudent(@ModelAttribute Student student){ studentservice.add(student); return "redirect:/student_list"; } @RequestMapping(value="/student_update") public String updateSuruder(@ModelAttribute Student student){ System.out.println(student.toString()); studentservice.udpate(student); return "redirect:/student_list"; } @RequestMapping("/student_delete/{name}") public String deleteStudent(ModelAndView model,@PathVariable String name){ List<Student> students = studentservice.getStudents(); model.addObject("students",students); model.addObject("student",new Student()); studentservice.delete(name); return "redirect:/student_list"; } @RequestMapping("/student_list") public String liststudent(Model model){ List<Student> students = studentservice.getStudents(); model.addAttribute("students",students); return "listForm"; } }
七:Spring配置文件的修改:
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <context:component-scan base-package="com.springmvc.controller"></context:component-scan> <context:component-scan base-package="com.springmvc.service"></context:component-scan> <bean id="" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
八:视图jsp 的书写:
(1)listForm.jsp
<%@ page language="java" pageEncoding="GB18030"%> <%@ page contentType="text/html;charset=GB18030"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <div> <table> <tr> <td>id</td> <td>name</td> <td>sex</td> <td>age</td> <td>address</td> <td>编辑</td> <td>删除</td> </tr> <c:forEach items="${students }" var="student"> <tr> <td>${student.id }</td> <td>${student.name }</td> <td>${student.sex }</td> <td>${student.age }</td> <td>${student.address }</td> <td><a href="student_edit/${student.name }">Edit</a></td> <td><a href="student_delete/${student.name }">Delete</a></td> </tr> </c:forEach> <a href="student_add">Student add</a> </table> </div> </body> </html>
(2)editForm.jsp
<%@ page language="java" pageEncoding="GB18030"%> <%@ page contentType="text/html;charset=GB18030"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE HTML> <html> <head> <title>编辑页面</title> </head> <body> <div> <form:form commanndName="student" modelAttribute="student" action="http://localhost:8080/fivespringmvclianxi/student_update" method="post"> <fieldset> <legend>Edit a book</legend> <p> <label for="id">Id: </label> <form:input id="id" path="id"/> </p> <p> <form:hidden path="name"/> </p> <p> <label for="sex">Sex: </label> <form:select path="sex" items="${sexs }"> </form:select> </p> <p> <label for="age">Age: </label> <form:input id="age" path="age"/> </p> <p> <label for="address">Address: </label> <form:input id="address" path="address"/> </p> <p> <input id="reset" type="reset" tabindex="4"> <input id="submit" type="submit" tabindex="5" value="update Student"> </p> </fieldset> </form:form> </div> </body> </html>
(3)addForm.jsp
<%@ page language="java" pageEncoding="GB18030"%> <%@ page contentType="text/html;charset=GB18030"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE HTML> <html> <head> <title>添加页面</title> </head> <body> <div > <form:form modelAttribute="student" action="student_save" method="post"> <fieldset> <legend>Add a book</legend> <p> <label for="id">Id: </label> <form:input id="id" path="id"/> </p> <p> <label for="name">Name: </label> <form:input path="name"/> </p> <p> <label for="sex">Sex: </label> <form:select path="sex" items="${sexs }"> </form:select> </p> <p> <label for="age">Age: </label> <form:input id="age" path="age"/> </p> <p> <label for="address">Address: </label> <form:input id="address" path="address"/> </p> <p > <input id="reset" type="reset" tabindex="4"> <input id="submit" type="submit" tabindex="5" value="Add Student"> </p> </fieldset> </form:form> </div> </body> </html>
九:数据库的创建
使用命令建数据库
命令如下:
创建数据库test
create database test;创建数据表information,name作为主键use test;create table information(
id int,
name varchar(30) primary key,
sex varchar(8),
age int,
address varchar(200));
添加三条数据:
insert into information(id, name,sex,age,address)
values(1,"A","Man",21,"wuhan"),
(2,"B","Woman",20,"shenzhen"),
(3,"C","Man",2,"yunnan");
十:启动页面的建立
直接在WebContent目录下新建一个index.jsp文件
添加这一句
[b]十一:各个页面个截图
[/b]
点击student启动
listForm.jsp页面
addForm.jsp页面
editForm.jsp页面
执行了添加,修改,删除后的listForm.jsp页面
相关文章推荐
- 使用SpringMVC进行数据的传送,数据来自mysql数据库 (2)使用hibernate
- android 与struts 使用HttpsUrlConnection进行数据(文件/字符)传送 (客户端例)
- 了解传输信道的使用、传输信道特性、描述来自上层的数据在进行扩频前的处理过程、传输信道向物理信道的映射关系。
- 【springMVC 后台跳转前台】1.使用ajax访问的后台,后台正常执行,返回数据,但是不能进入前台的ajax回调函数中 ----2.前后台都没有报错,不能进入ajax回调函数
- 使用ajax来进行前后传送数据(django框架、Python语言)
- 解决使用HIBERNATE对ORACLE数据库中数据进行排序分页错乱的问题
- Hibernate(3)-使用HQL语句进行数据查询
- springmvc使用JSR-303进行数据校验实例
- 使用hibernate往mysql数据库插入数据中文乱码问题
- SpringMVC使用@Valid注解进行数据验证
- SpringMVC_01 SpringMVC五大组件、SpringMVC编程步骤(不使用注解进行配置)、SpringMVC编程步骤(利用注解进行配置)、参数获取、响应数据
- SpringMVC-Hibernate关于数据更新没有持久化的问题
- 使用JSP连接MySql数据库读取HTML表单数据进行存贮
- SpringMVC使用@Valid注解进行数据验证的方法
- mysql数据库 text类型的长度限制,使用change_column来进行长度的修改并不影响原有数据
- hibernate使用配置文件创建联合主键往MySQL数据库插入数据
- 使用spring、springmvc和hibernate开发博客(二)——数据进出
- 讲解在java环境下使用jQuery进行JSON数据传送的交互过程
- 练习使用Python post请求爬取数据,并用mySQL数据库进行存储
- SpringMVC使用@Valid注解进行数据验证