多平台统一管理软件接口,如何实现多平台统一管理软件接口
421
2023-03-09
Spring MVC实现mysql数据库增删改查完整实例
最近刚学了springmvc框架,感觉确实方便了不少,减少了大量的冗余代码。就自己做了个小项目练练手,这是个初级的springmvc应用的项目,没有用到mybatis,项目功能还算完善,实现了基本的增删改查的功能。
项目环境:
-系统:win10
-开发环境:eclipseOxygenReleaseCandidate3(4.7)
-jdk版本:java1.8(121)
-mysql:5.7
-spring:4.0
-tomcat:8.5
用到的技术:
springmvcspringjspjdbcjavaBeanjsjstl
访问地址:http://localhost:8080/你的项目名/all
声明:我只是一个刚入门不久的新手,所写代码难免有出错之处,如发现欢迎各位指出,谢谢大家。
下面就贴上详细过程
1.首先创建一个web项目(DynamicWebProject)
项目名字就自己写了,不再详细写
2. 这是我的已完成项目结构
我只是为了实现功能,没有用到接口,只用了简单的三个类,bean包下的实体类,dao层数据库访问类,controller层的界面控制类,
所有引用的jar包都在/WebContent/WEB-INF/lib文件夹下,这点与普通的java项目不同。
3. 具体java代码
1.Student类,实体类 首先要写一个javaBean,我的是Student作为javaBean,详细代码如下:
package bean;
public class Student {
private Integer id;//学生id
private String name;//学生姓名
private Double javaScore;//java成绩
private Double htmlScore;//html成绩
private Double cssScore;//css成绩
private Double totalScore;
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 Double getJavaScore() {
return javaScore;
}
public void setJavaScore(Double javaScore) {
this.javaScore = javaScore;
}
public Double getHtmlScore() {
return htmlScore;
}
public void setHtmlScore(Double htmlScore) {
this.htmlScore = htmlScore;
}
public Double getCssScore() {
return cssScore;
}
public void setCssScore(Double cssScore) {
this.cssScore = cssScore;
}
public Double getTotalScore() {
return totalScore;
}
public void setTotalScore(Double totalScore) {
this.totalScore = totalScore;
}
}
2. StudentDao,数据库访问操作类 然后是dao层即数据访问层的代码,这里使用的是spring封装的一个类(JdbcTemplate),里面有一些操作数据库的方法,不用再自己写大量重复代码,只要写SQL语句。下面是具体代码:
package dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import bean.Student;
public class StudentDao {
/**
* @Fields jdbcTemplate : TODO
*/
private JdbcTemplate jdbcTemplate;
/**
* spring提供的类
*
* @param jdbcTemplate
* 返回值类型: void
* @author janinus
*/
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
/**
* 查询所有学生
*
* @return 返回值类型: List
* @author janinus
*/
public List
String sql = "select id,name,javaScore,htmlScore,cssScore from student";
//将查询结果映射到Student类中,添加到list中,并返回
return jdbcTemplate.query(sql, new StudentMapper());
}
/**
* 通过姓名查询
*
* @param name
* @return 返回值类型: List
* @author janinus
*/
public List
String sql = "select id,name,javaScore,htmlScore,cssScore from student where name like '%" + name + "%'";
return jdbcTemplate.query(sql, new StudentMapper());
}
/**
* 添加学生
*
* @param student
* @return 返回值类型: boolean
* @author janinus
*/
public boolean addStu(Student student) {
String sql = "insert into student(id,name,javaScore,htmlScore,cssScore) values(0,?,?,?,?)";
return jdbcTemplate.update(sql,
new Object[] { student.getName(), student.getJavaScore(), student.getHtmlScore(),
student.getCssScore() },
new int[] { Types.VARCHAR, Types.DOUBLE, Types.DOUBLE, Types.DOUBLE }) == 1;
}
/**
* 删除学生
*
* @param id
* @return 返回值类型: boolean
* @author janinus
*/
public boolean deleteStu(Integer id) {
String sql = "delete from student where id = ?";
return jdbcTemplate.update(sql, id) == 1;
}
/**
* 更新学生信息
*
* @param student
* @return 返回值类型: boolean
* @author janinus
*/
public boolean updateStu(Student student) {
String sql = "update student set name=? ,javaScore=?,htmlScore = ? ,cssScore = ? where id = ?";
Object stuObj[] = new Object[] { student.getName(), student.getJavaScore(), student.getHtmlScore(),
student.getCssScore(), student.getId() };
return jdbcTemplate.update(sql, stuObj) == 1;
}
/**
* 返回总成绩前n名学生
*
* @param num
* @return 返回值类型: List
* @author janinus
*/
public List
String sql = "select id,name,javaScore+htmlScore+cssScore from student order by javaScore+htmlScore+cssScore desc ,name asc limit ?";
return jdbcTemplate.query(sql, new RowMapper
@Override
public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
// TODO Auto-generated method stub
Student student = new Student();
student.setId(rs.getInt(1));
student.setName(rs.getString(2));
student.setTotalScore(rs.getDouble(3));
return student;
}
}, num);
}
/**
*
* StudentMapper数据库映射
*
* @ClassName StudentMapper
* @author janinus
* @date 2017年6月27日
* @Version V1.0
*/
class StudentMapper implements RowMapper
@Override
public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
// TODO Auto-generated method stub
Student student = new Student();
student.setId(rs.getIntYaciIPFVZ(1));
student.setName(rs.getString(2));
student.setJavaScore(rs.getDouble(3));
student.setHtmlScore(rs.getDouble(4));
student.setCssScore(rs.getDouble(5));
return student;
}
}
}
3. StudentController ,前后端交互类 最后是与用户交互有关的控制层StudentController类,这个类主要用来将前后端联合,实现完整的交互。下面是具体代码:
package controller;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import bean.Student;
import dao.StudentDao;
@Controller
public class StudentController {
/**
*
* 从数据库中获取全部学生信息,将数据返回给主页index,jsp
*
* @param model
* @return 返回值类型: String
* @author janinus
*/
@RequestMapping(value = "/all")
public String queryAll(Model model) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//从ioc容器中获取dao
StudentDao dao = (StudentDao) context.getBean("dao");
model.addAttribute("students", dao.queryAll());
model.addAttribute("tops", dao.topNum(3));
return "index.jsp";
}
/**
* 通过姓名查找学生,使用模糊查找,将结果返回给index.jsp
*
* @param name
* @param model
* @return 返回值类型: String
* @author janinus
*/
@RequestMapping(value = "/queryByName")
public String queryByName(String name, Model model) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//从ioc容器中获取dao
StudentDao dao = (StudentDao) context.getBean("dao");
model.addAttribute("students", dao.queryByName(name));
model.addAttribute("tops", dao.topNum(3));
return "index.jsp";
}
/**
* 添加新学生,并将结果返回给all页面,由all转发到主页
* @param name
* @param javaScore
* @param htmlScore
* @param cssScore
* @param model
* @return 返回值类型: String
* @author janinus
*/
@RequestMapping(value = "/add")
public String addStu(String name, String javaScore, String htmlScore, String cssScore, Model model) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentDao dao = (StudentDao) context.getBean("dao");
Student student = new Student();
student.setName(name);
student.setJavaScore(Double.parseDouble(javaScore));
student.setHtmlScore(Double.parseDouble(htmlScore));
student.setCssScore(Double.parseDouble(cssScore));
boolean result = dao.addStu(student);
if (result)
model.addAttribute("msg", "");
else
model.addAttribute("msg", "");
return "all";
}
/**
* 通过id删除学生
* @param id
* @param model
* @return 返回值类型: String
* @author janinus
*/
@RequestMapping(value = "/deleteById")
public String deleteById(String id, Model model) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentDao dao = (StudentDao) context.getBean("dao");
boolean result = dao.deleteStu(Integer.parseInt(id));
if (result)
model.addAttribute("msg", "");
else
model.addAttribute("msg", "");
return "all";
}
/**
*
* @param id
* @param name
* @param javaScore
* @param htmlScore
* @param cssScore
* @param model
* @return 返回值类型: String
* @author janinus
*/
@RequestMapping(value = "/update")
public String updateStu(String id, String name, String javaScore, String htmlScore, String cssScore, Model model) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentDao dao = (StudentDao) context.getBean("dao");
Student student = new Student();
student.setId(Integer.parseInt(id));
student.setName(name);
student.setJavaScore(Double.parseDouble(javaScore));
student.setHtmlScore(Double.parseDouble(htmlScore));
student.setCssScore(Double.parseDouble(cssScore));
boolean result = dao.updateStu(student);
if (result)
model.addAttribute("msg", msg("修改成功"));
else
model.addAttribute("msg", msg("修改失败"));
return "all";
}
/**
* 要弹出的页面消息
* @param msg
* @return 返回值类型: String
* @author janinus
*/
public String msg(String msg) {
return "";
}
}
所有的java代码已经完成,下面只剩下具体的xml配置和前端页面。
4.前端页面
由于是一个简单的小项目,我的js,css都在同一个页面,没有分开,只有两个页面,
1.index.jsp
主页,截图
编辑
详细代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fn"
uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
body{
text-align: center;
}
.all{
width:40%;
margin: 20px 100px;
text-align: center;
height: 300px;
float: left;
}
table{
width: 80%;
margin: 20px auto;
font-size: 14px;
overflow: auto;
}
#tab02{
width: 80%;
margin: 20px auto;
font-size: 14px;
}
table th,table td{
border-bottom: 1px #000 solid;
line-height: 23px;
}
#edit_comm{
width: 500px;
margin: 20px auto;
border-left: 3px solid #000;
display: none;
}
#add_comm{
width: 500px;
margin: 20px auto;
border-left: 3px solid #000;
}
#all_comm{
height:600px;
}
.edit_stu{
width:200px;
height: 30px;
background: #fff;
font-family: "微软雅黑 Light", "Arial Black";
font-size: 18px;
border: none;
border-bottom: 1px solid #000;
margin: 20px 10px;
}
$(function(){
$("#cancel").click(function(){
$("#add_comm").fadeIn();
$("#edit_comm").fadeOut();
})
$("input").addClass("edit_stu");
})
function refush(){
window.location.href="all" rel="external nofollow" rel="external nofollow" ;
}
function add_reg(){
var name = $("#add_edit_name").val();
var javaScore = $("#add_edit_java").val();
var htmlScore = $("#add_edit_html").val();
var cssScore=$("#add_edit_css").val();
var nameNot = name!=null&&name!='';
var javaScoreNot = javaScore!=null && javaScore != '';
var htmlScoreNot = htmlScore!=null && htmlScore !='';
var cssScoreNot = cssScore !=null && cssScore != '';
if(nameNot&&javaScoreNot&&htmlScoreNot&&cssScoreNot)
return true;
else
return false;
}
function delete_stu(id){
var result = confirm("是否删除?");
if(result)
window.location.href="deleteById?id=" rel="external nofollow" +id;
}
function edit_stu(id){
var name = $("#name"+id).text();
var java = $("#java"+id).text();
var html = $("#html"+id).text();
var css = $("#css"+id).text();
$("#edit_id").val( id);
$("#edit_name").val(name);
$("#edit_java").val(java);
$("#edit_html").val(html);
$("#edit_css").val(css);
$("#add_comm").fadeOut();
$("#edit_comm").fadeIn();
}
${msg }
如不显示请:点此刷新
2. login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
5. 详细文件配置
1. applicationContext.xml
这是spring的ioc容器的配置文件,用来实现依赖注入,下面是具体代码:
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:tx="http://springframework.org/schema/tx" xmlns:aop="http://springframework.org/schema/aop" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-2.5.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-2.5.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byName" default-lazy-init="true" >
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:tx="http://springframework.org/schema/tx"
xmlns:aop="http://springframework.org/schema/aop"
xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-2.5.xsd
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-2.5.xsd
http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="byName" default-lazy-init="true" >
2. springMVC-servlet.xml,spring mvc配置类,
为我们实现了servlet的大部分代码,我们只需要写业务实现即可。下面是具体代码
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p" xmlns:context="http://springframework.org/schema/context" xsi:schemaLocation=" http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"
xmlns:context="http://springframework.org/schema/context"
xsi:schemaLocation="
http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans-3.0.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context-3.0.xsd">
3. web.xml
这是web工程的配置文件,下面是主要代码:
xmlns:xsi="http://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">
xmlns:xsi="http://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">
6.项目总结及附录
这个项目是个我的日常练习项目,为了更加熟练,我把完整的过程又回顾了一遍,又熟悉了很多,
项目用的jar包附录:
除了spring的包外,还有mysql-jbdc的jar包和jstl的jar包
下载地址:
spring框架jar包(可选版本):spring官网
mysql-jdbc.jar(可选版本):MySQL官网
jstl.jar(可选版本):maven官方地址
以上就是本文关于Spring MVC实现mysql数据库增删改查完整实例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:
Spring之WEB模块配置详解
springmvc Rest风格介绍及实现代码示例
SpringMVC拦截器实现单点登录
如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~