zoukankan      html  css  js  c++  java
  • 记一次JAVA ssm+mysql 开发

     2017/06/22 14:54

      一、前言  

      最近在做一个项目的设计和外包管理,实现选型是:java,SSM+Mysql,虽然一直在做.net,但是因为项目需要,还是花一周时间进行了解并写了demo和文档供外包开发使用。本次实现采用了其他项目的纯净版框架,包含系统管理(shiro安全框架)内容。

      学完Java入门课五节(环境安装,数据结构),将已有的纯净版代码发布,开发过程不断了解Eclipse使用,Mysql语法,Tomcat使用,war打包等。

      现在进入正题,根据这几天开发经验总结了这几天的开发过程,了解实在有限,欢迎指正。

     

      二、demo效果

      demo功能实现标签的增删改查和子项的操作。

    • 查询

    • 增加

    • 修改

    • 详情

    • 删除

    • 标签项操作

     

      我这样的半门外汉都能完成简单的单表增删改查,现在你是不是迫不及待想进行实践了呢!

     

      三、开始上手

    3.1 首先,你需要知道本次开发的业务信息;

      这里我们以学生简要信息为例,建立数据表如下:

    drop table if exists student;
    -- ----------------------------
    -- Table structure for `student`
    -- ----------------------------
    create table student
    (
        sno       int auto_incremedt not null COMMENT '学号,自增长,主键',
        sname     int COMMENT '姓名',
        birthday     DateTime COMMENT '生日',
        insert_time  DateTime default CURRENT_TIMESTAMP COMMENT '创建时间,默认当前时间',
        primary key (sno)
    ) DEFAULT CHARSET=utf8 COMMENT '学生简表';

    • 查询
    1. 查询条件:姓名
    2. 列表字段:学号,姓名,生日,创建时间
    3. 要求分页
    select sno,sname,birthday,insert_time from student where sname='学生甲';
    • 增加
    1. 学号自增长生成
    2. 学生姓名非空,唯一
    3. 生日可为空,日期控件选择
    insert into student (sname,birthday) values('学生乙','1990-07-17');
    • 修改
    1. 姓名可修改非空,唯一
    2. 生日可修改
    update student set sname = '学生乙',birthday = '1990-07-17' where sno = 1;
    • 删除
    1. 可单条删除
    2. 也可批量删除
    delete from student where sno = 1;

     

    3.2 接着,我们介绍一下开发环境和项目结构 

       我的环境:Tomcat7 + Eclipse Kepler Service Release 2 + Spring3+ JDK1.7;介绍项目结构前,我画了一个框架的时序图,供参考:

      

      项目结构如下: 各种配置文件和jar包引用已经处理好,此处不介绍。

     

    1. WebContentjsp下是jsp页面
    2. srcmain下是JAVA代码

    我们将要操作的的部分是:

    • entity 实体:com.bo.entity.student包下新建实体类Student.java;
    • vo 查询条件:com.bo.vo.student包下新建实体类StudentVo.java;
    • mapping 配置sql:com.bo.mapping包下新建配置文件StudentMapper.xml;
    • dao:com.bo.dao.mybatis.student包下新建接口StudentDao.java;
    • service

      com.bo.service.student包下新建接口StudentService.java;

      com.bo.service.student.impl包下实现接口StudentService,新建类StudentServiceImpl.java;

    • controller 控制器:com.bo.controller.student包下新建接口StudentCcontroller.java;
    • jsp 页面:WebContentjsp下新建列表页面Student_List.jsp;

      基本上我们的开发步骤就是:先建立实体和VO;其次配置sql;接着针对sql编写dao;然后根据dao写service层供控制器调用;最后写controller和jsp,调用service层逻辑呈现页面。
    这节要求注意的一点是命名规则,类要见名知意,驼峰式命名。

    3.3  然后,我们写一个查询方法来练习一下具体的开发步骤:

    3.3.1 学生实体开发

      基本参考student数据结构,并添加Get和Set方法

     1 package com.bo.entity.student;
     2 
     3 import java.io.Serializable;
     4 
     5 /**
     6  * 学生简表
     7  */
     8 public class Student implements Serializable{
     9 
    10     private static final long serialVersionUID = 1L;
    11     /**
    12      * 学号 ,实体必须定义为id,在mapper里可以映射为sno
    13      */    
    14     private Integer id;
    15     /**
    16      * 学生姓名
    17      */
    18     private String sNname;
    19     /**
    20      * 生日
    21      */
    22     private java.util.Date birthday;
    23     /**
    24      * 创建时间
    25      */
    26     private java.util.Date insertTime;
    27 
    28     public Integer getId() {
    29         return id;
    30     }
    31 
    32     public void setId(Integer id) {
    33         this.id = id;
    34     }
    35 
    36     public String getLabelText() {
    37         return labelText;
    38     }
    39 
    40     public void setLabelText(String labelText) {
    41         this.labelText = labelText;
    42     }
    43 
    44     public String getLabelCount() {
    45         return labelCount;
    46     }
    47 
    48     public void setLabelCount(String labelCount) {
    49         this.labelCount = labelCount;
    50     }
    51 
    52     public String getMemo() {
    53         return memo;
    54     }
    55 
    56     public void setMemo(String memo) {
    57         this.memo = memo;
    58     }
    59 
    60     public Integer getId() {
    61         return id;
    62     }
    63 
    64     public void setId(Integer id) {
    65         this.id = id;
    66     }
    67 
    68     public java.util.Date getInsertTime() {
    69         return insertTime;
    70     }
    71 
    72     public void setInsertTime(java.util.Date insertTime) {
    73         this.insertTime = insertTime;
    74     }
    75 }
    3.3.2 学生查询类开发

      StudentVo类继承Student实体,可根据需要添加属性,此处我们不添加。

    1 package com.bo.vo.student.Student;
    2 
    3 import com.bo.entity.student.Student;
    4 
    5 public class StudentVo extends Student{
    6     
    7 }
     
    3.3.3 mapper文件开发

      根据需要编写,注意namespace为相关Dao名,数据结构和实体属性对应配成resultMap;这里我只写了一个查询数据的方法"findListByPage",后续可以随用随写。

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
     3 <mapper namespace="com.bo.dao.mybatis.student.StudentDao">
     4     <!-- Result Map -->
     5     <resultMap type="com.bo.entity.student.Student" id="BaseResultMap">
     6         <id column="Sno" property="id" jdbcType="INTEGER"/>
     7         <result column="Sname" property="sName" jdbcType="VARCHAR"/>
     8         <result column="birthday" property="birthday" jdbcType="TIMESTAMP"/>
     9         <result column="insert_Time" property="insertTime" jdbcType="TIMESTAMP"/>
    10     </resultMap>
    11     
    12     <resultMap id="BaseResultVoMap" extends="BaseResultMap" type="com.bo.vo.student.StudentVo">
    13     </resultMap>
    14     
    15     <sql id="Base_Column_List">
    16         Sno,Sname,birthday,insert_time
    17     </sql>
    18 
    19     <select id="findListByPage" resultMap="BaseResultVoMap" parameterType="com.bo.vo.student.StudentVo">
    20         select
    21         <include refid="Base_Column_List"/>
    22         from student where 1=1
    23         <if test="vo.id != null and '' != vo.id ">and sno = #{ vo.id }</if>
    24         <if test="vo.sName != null and '' != vo.sName ">and Sname = #{ vo.sName }</if>
    25     </select>
    26 </mapper>
    3.3.4 Dao接口开发

      继承BaseMybatisDao,两个泛型一个对应实体类,一个对应主键类型,这里我只写了"findListByPage"方法,同理,随用随写。

     1 package com.bo.dao.mybatis.student;
     2 
     3 import java.util.List;
     4 import java.util.Map;
     5 
     6 import org.apache.ibatis.annotations.Param;
     7 
     8 import com.bo.common.base.BaseMybatisDao;
     9 import com.bo.common.page.Pagination;
    10 import com.bo.entity.student.Student;
    11 import com.bo.vo.student.StudentVo;
    12 
    13 public interface StudentDao extends BaseMybatisDao<Student,Integer>{
    14     
    15     List<StudentVo> findListByPage(@Param("vo") StudentVo vo,@Param("page")Pagination page);
    16 }
    3.3.5 Service层开发
    • service接口:

      返回的Pagination是一个定义好的分页列表,可直接使用。

     1 package com.bo.service.student;
     2 
     3 import java.util.List;
     4 
     5 import com.bo.common.base.BaseMybatisService;
     6 import com.bo.common.page.Pagination;
     7 import com.bo.vo.student.StudentVo;
     8 import com.bo.entity.student.Student;
     9 
    10 public interface StudentService  extends BaseMybatisService<Student,Integer> {
    11 
    12     public Pagination findListByPage(int rows, int page,StudentVo vo);
    13 }
    • Impl实现:

      实现Service并调用Dao里刚刚写的方法。

     1 package com.bo.service.student.impl;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 import org.apache.ibatis.annotations.Param;
     7 import org.springframework.beans.factory.annotation.Autowired;
     8 import org.springframework.stereotype.Service;
     9 import org.springframework.transaction.annotation.Transactional;
    10 
    11 import com.bo.common.page.Pagination;
    12 import com.bo.dao.mybatis.student.StudentDao;
    13 import com.bo.entity.student.Student;
    14 import com.bo.service.student.StudentService;
    15 import com.bo.vo.student.StudentVo;
    16 
    17 @Transactional
    18 @Service("studentService")
    19 public class StudentServiceImpl implements StudentService{
    20     
    21     @Autowired
    22     private StudentDao sDao;
    23 
    24     @Override
    25     public Pagination findListByPage(int rows, int page, StudentVo vo) {
    26         // TODO Auto-generated method stub
    27         Pagination pagination = new Pagination();
    28         pagination.setPageNo(page); //当前页码
    29         pagination.setPageSize(rows);  //每页显示多少行
    30         List<StudentVo>  list = this.sDao.findListByPage(vo,pagination);
    31         pagination.setList(list);
    32         return pagination;
    33     }
    34 }
     
    3.3.6 Controller层开发

      查询列表的方法。

     
     
    3.3.7 JSP页面开发

      页面布局:注意按钮放在<shiro></shiro>里,其中name命名为"Student:操作代号(比如remove)"。

     

      

      四、结语

      个人认为好理解的开发步骤:先实体,再sql(Mpper),接着Dao,然后Service,最后Controller和JSP。 

  • 相关阅读:
    关于Web登陆安全
    HttpWebRequest 忽略证书
    C# 语音识别(文字to语音、语音to文字)
    Microsoft Visual Studio 11(ISO、Web Installer)、.NET Framework 4.5 等下载地址
    ubuntu硬盘安装及启动,menu.lst
    下载虾米音乐的歌词
    sublime text在linux下一键编译c和c++程序的配置文件
    foobar2000专辑封面混乱解决方法
    qt creator纯C或C++项目在windows下的命令行中文乱码解决
    婚姻就是嫁给习惯和性格(转)
  • 原文地址:https://www.cnblogs.com/missfox18/p/7244107.html
Copyright © 2011-2022 走看看