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 '學生簡表';
- 查詢
- 查詢條件:姓名
- 列表字段:學號,姓名,生日,創建時間
- 要求分頁
select sno,sname,birthday,insert_time from student where sname='學生甲';
- 增加
- 學號自增長生成
- 學生姓名非空,唯一
- 生日可為空,日期控件選擇
insert into student (sname,birthday) values('學生乙','1990-07-17');
- 修改
- 姓名可修改非空,唯一
- 生日可修改
update student set sname = '學生乙',birthday = '1990-07-17' where sno = 1;
- 刪除
- 可單條刪除
- 也可批量刪除
delete from student where sno = 1;
3.2 接着,我們介紹一下開發環境和項目結構
我的環境:Tomcat7 + Eclipse Kepler Service Release 2 + Spring3+ JDK1.7;介紹項目結構前,我畫了一個框架的時序圖,供參考:
項目結構如下: 各種配置文件和jar包引用已經處理好,此處不介紹。
- WebContent\jsp下是jsp頁面
- src\main下是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 頁面:WebContent\jsp下新建列表頁面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。