栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

IDEA+springmvc第一章+超详细使用三层架构完成表业务操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

IDEA+springmvc第一章+超详细使用三层架构完成表业务操作

IDEA+springmvc+三层架构+增删改查 IDEA配置mvc框架
  • 创建项目
  • 配置lib jar包依赖
  • 配置tomcat
  • 配置前端总控制器:ClassNotFountException
  • 搭建框架
  • 编写hello测试接口
springmv框架搭建步骤
  • 正确创建项目 正确配置框架
  • 前端总控制器->web.xml
  • 映射器,适配器->app.xml
  • 扫描包->app.xml
  • 静态资源放行->app.xml
  • 数据源->app.xml
  • queryrunner->app.xml
具体操作如下,首先创建一个项目名为Springmvc01






到此我们IDEA配置项目搭建完成了。

现在我们来开始配置app.xml与web.xml









    
    
    
    



    




    
    
        springDispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            
            contextConfigLocation
            classpath:app.xml
        
        1
    
    
        springDispatcherServlet
        
        /
    


编写实体类 编写持久层代码
package demo.dao;

import demo.bean.Course;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;


@Repository
public class CourseDao {

    @Autowired
    QueryRunner queryRunner;

    
    public List Select() throws SQLException{
        String sql="select * from course";
        return  queryRunner.query(sql,new BeanListHandler(Course.class));
    }

    
    public int delete(int cid)throws  SQLException{
        String sql="delete from course where cid=?";
        return queryRunner.update(sql,cid);
    }

    
    public int add(String cname) throws SQLException{
        String sql="INSERT INTO course (cname) VALUES (?)";
        return  queryRunner.update(sql,cname);
    }

    
    public Course selectByCid(int cid) throws SQLException{
        String sql="select * from course where cid=?";
        return queryRunner.query(sql,new BeanHandler(Course.class),cid);
    }

    
    public int update(String cname,int cid)throws SQLException{
        String sql="UPDATE course SET cname=? WHERe cid=?";
        return queryRunner.update(sql,cname,cid);
    }



}

package demo.dao;

import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
@Repository
public class ScoreDao {
    @Autowired
    QueryRunner queryRunner;

    public void deleteScoreByCid(int cid) throws SQLException {

        String sql="delete from score where cid =?";

        queryRunner.update(sql,cid);

    }
}

编写业务层代码
package demo.service;

import demo.bean.Course;
import demo.dao.CourseDao;
import demo.dao.ScoreDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;


@Service
public class CourseService {

    @Autowired
    CourseDao courseDao;

    @Autowired
    ScoreDao scoreDao;

    
    public List Select() throws SQLException {
        return courseDao.Select();
    }

    
    public int delete(String cid)throws SQLException{
        int i = courseDao.delete(Integer.parseInt(cid));
        if(i>0){
            scoreDao.deleteScoreByCid(Integer.parseInt(cid));
        }
        return i;
    }

    
    public int add(String cname)throws  SQLException{
        return courseDao.add(cname);
    }

    
    public Course selectByCid(String cid)throws  SQLException{
        return courseDao.selectByCid(Integer.parseInt(cid));
    }

    
    public int update( String cname,String cid)throws SQLException{
        return courseDao.update(cname,Integer.parseInt(cid));
    }



}

编写控制层代码
package demo.controller;

import demo.bean.Course;
import demo.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.List;


@RestController
@RequestMapping("/CourseController")
public class CourseController {

    @Autowired
    CourseService courseService;


    @RequestMapping("Select")
    public List Select() throws SQLException {
        return courseService.Select();
    }

    @RequestMapping("delete")
    public int delete(String cid) throws SQLException {
        int result =courseService.delete(cid);
        return result;
    }

    @RequestMapping("add")
    public int add(String cname)throws  SQLException{
        int result=courseService.add(cname);
        return result;
    }

    @RequestMapping("selectByCid")
    public Course selectByCid(String cid)throws SQLException{
        return courseService.selectByCid(cid);
    }

    @RequestMapping("update")
    public int update(String cname, String cid)throws SQLException{
        return courseService.update(cname,cid);
    }


}

前端页面



    
    Title
    



添加课程名称:


课程编号 课程名称 操作
数据库表sql

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for course
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course`  (
  `cid` int(11) NOT NULL AUTO_INCREMENT COMMENT '课程主键',
  `cname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '课程名称',
  PRIMARY KEY (`cid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of course
-- ----------------------------
INSERT INTO `course` VALUES (1, 'JAVA');
INSERT INTO `course` VALUES (2, 'C语言');

SET FOREIGN_KEY_CHECKS = 1;


SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for score
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score`  (
  `sid` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `score` int(255) NULL DEFAULT NULL COMMENT '分数',
  `cid` int(11) NULL DEFAULT NULL COMMENT '课程表关联键',
  `sname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '学生名称',
  PRIMARY KEY (`sid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of score
-- ----------------------------
INSERT INTO `score` VALUES (1, 50, 1, '张三');
INSERT INTO `score` VALUES (2, 60, 2, '李四');
INSERT INTO `score` VALUES (3, 70, 1, '王五');

SET FOREIGN_KEY_CHECKS = 1;

以上我们用的springmvc三层架构思想(控制层Controller,业务层Service,持久层dao): MVC架构分层的主要作用是解耦。采用分层架构的好处,普遍接受的是系统分层有利于系统的维护,系统的扩展。增强系统的可维护性。

复制以上代码可以直接使用。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/872533.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号