工单系统的设计与实现(4)
生活随笔
收集整理的這篇文章主要介紹了
工单系统的设计与实现(4)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
項目的層次結(jié)構清楚之后,首先進行mapper層的編寫。關于Mybatis的更多知識,可參見之前的博客。
因為要進行ORM映射,首先準備好pojo,也可以用mybatis的逆向工程生成。
OrderInfo的Pojo如下:
package com.example.demo.bean;import java.util.Date;public class OrderInfo {/**** This field was generated by MyBatis Generator.* This field corresponds to the database column order.id** @mbg.generated*/private Integer id;/**** This field was generated by MyBatis Generator.* This field corresponds to the database column order.number** @mbg.generated*/private String number;/**** This field was generated by MyBatis Generator.* This field corresponds to the database column order.title** @mbg.generated*/private String title;/**** This field was generated by MyBatis Generator.* This field corresponds to the database column order.status** @mbg.generated*/private Integer status;/**** This field was generated by MyBatis Generator.* This field corresponds to the database column order.priority** @mbg.generated*/private Integer priority;/**** This field was generated by MyBatis Generator.* This field corresponds to the database column order.category** @mbg.generated*/private Integer category;/**** This field was generated by MyBatis Generator.* This field corresponds to the database column order.userID** @mbg.generated*/private Integer userid;/**** This field was generated by MyBatis Generator.* This field corresponds to the database column order.phone** @mbg.generated*/private String phone;/**** This field was generated by MyBatis Generator.* This field corresponds to the database column order.email** @mbg.generated*/private String email;/**** This field was generated by MyBatis Generator.* This field corresponds to the database column order.attachment** @mbg.generated*/private String attachment;/**** This field was generated by MyBatis Generator.* This field corresponds to the database column order.engineerID** @mbg.generated*/private Integer engineerid;/**** This field was generated by MyBatis Generator.* This field corresponds to the database column order.createTime** @mbg.generated*/private Date createtime;/**** This field was generated by MyBatis Generator.* This field corresponds to the database column order.finishTime** @mbg.generated*/private Date finishtime;/**** This field was generated by MyBatis Generator.* This field corresponds to the database column order.content** @mbg.generated*/private String content;/*** This method was generated by MyBatis Generator.* This method returns the value of the database column order.id** @return the value of order.id** @mbg.generated*/public Integer getId() {return id;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column order.id** @param id the value for order.id** @mbg.generated*/public void setId(Integer id) {this.id = id;}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column order.number** @return the value of order.number** @mbg.generated*/public String getNumber() {return number;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column order.number** @param number the value for order.number** @mbg.generated*/public void setNumber(String number) {this.number = number == null ? null : number.trim();}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column order.title** @return the value of order.title** @mbg.generated*/public String getTitle() {return title;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column order.title** @param title the value for order.title** @mbg.generated*/public void setTitle(String title) {this.title = title == null ? null : title.trim();}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column order.status** @return the value of order.status** @mbg.generated*/public Integer getStatus() {return status;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column order.status** @param status the value for order.status** @mbg.generated*/public void setStatus(Integer status) {this.status = status;}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column order.priority** @return the value of order.priority** @mbg.generated*/public Integer getPriority() {return priority;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column order.priority** @param priority the value for order.priority** @mbg.generated*/public void setPriority(Integer priority) {this.priority = priority;}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column order.category** @return the value of order.category** @mbg.generated*/public Integer getCategory() {return category;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column order.category** @param category the value for order.category** @mbg.generated*/public void setCategory(Integer category) {this.category = category;}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column order.userID** @return the value of order.userID** @mbg.generated*/public Integer getUserid() {return userid;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column order.userID** @param userid the value for order.userID** @mbg.generated*/public void setUserid(Integer userid) {this.userid = userid;}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column order.phone** @return the value of order.phone** @mbg.generated*/public String getPhone() {return phone;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column order.phone** @param phone the value for order.phone** @mbg.generated*/public void setPhone(String phone) {this.phone = phone == null ? null : phone.trim();}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column order.email** @return the value of order.email** @mbg.generated*/public String getEmail() {return email;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column order.email** @param email the value for order.email** @mbg.generated*/public void setEmail(String email) {this.email = email == null ? null : email.trim();}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column order.attachment** @return the value of order.attachment** @mbg.generated*/public String getAttachment() {return attachment;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column order.attachment** @param attachment the value for order.attachment** @mbg.generated*/public void setAttachment(String attachment) {this.attachment = attachment == null ? null : attachment.trim();}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column order.engineerID** @return the value of order.engineerID** @mbg.generated*/public Integer getEngineerid() {return engineerid;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column order.engineerID** @param engineerid the value for order.engineerID** @mbg.generated*/public void setEngineerid(Integer engineerid) {this.engineerid = engineerid;}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column order.createTime** @return the value of order.createTime** @mbg.generated*/public Date getCreatetime() {return createtime;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column order.createTime** @param createtime the value for order.createTime** @mbg.generated*/public void setCreatetime(Date createtime) {this.createtime = createtime;}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column order.finishTime** @return the value of order.finishTime** @mbg.generated*/public Date getFinishtime() {return finishtime;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column order.finishTime** @param finishtime the value for order.finishTime** @mbg.generated*/public void setFinishtime(Date finishtime) {this.finishtime = finishtime;}/*** This method was generated by MyBatis Generator.* This method returns the value of the database column order.content** @return the value of order.content** @mbg.generated*/public String getContent() {return content;}/*** This method was generated by MyBatis Generator.* This method sets the value of the database column order.content** @param content the value for order.content** @mbg.generated*/public void setContent(String content) {this.content = content == null ? null : content.trim();} } View Code然后編寫OrderMapper.xml文件,該文件用于指定特點的SQL操作。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.edu.ustc.sse.workorder.mapper.OrderMapper"><resultMap id="BaseResultMap" type="cn.edu.ustc.sse.workorder.bean.OrderInfo"><!--WARNING - @mbg.generatedThis element is automatically generated by MyBatis Generator, do not modify.--><id column="id" jdbcType="INTEGER" property="id"/><result column="number" jdbcType="VARCHAR" property="number"/><result column="title" jdbcType="VARCHAR" property="title"/><result column="status" jdbcType="INTEGER" property="status"/><result column="priority" jdbcType="INTEGER" property="priority"/><result column="category" jdbcType="INTEGER" property="category"/><result column="userID" jdbcType="INTEGER" property="userid"/><result column="phone" jdbcType="VARCHAR" property="phone"/><result column="email" jdbcType="VARCHAR" property="email"/><result column="attachment" jdbcType="VARCHAR" property="attachment"/><result column="engineerID" jdbcType="INTEGER" property="engineerid"/><result column="createTime" jdbcType="TIMESTAMP" property="createtime"/><result column="finishTime" jdbcType="TIMESTAMP" property="finishtime"/></resultMap><resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="cn.edu.ustc.sse.workorder.bean.OrderInfo"><!--WARNING - @mbg.generatedThis element is automatically generated by MyBatis Generator, do not modify.--><result column="content" jdbcType="LONGVARCHAR" property="content"/></resultMap><sql id="Base_Column_List"><!--WARNING - @mbg.generatedThis element is automatically generated by MyBatis Generator, do not modify.-->id, number, title, status, priority, category, userID, phone, email, attachment,engineerID, createTime, finishTime</sql><sql id="Blob_Column_List"><!--WARNING - @mbg.generatedThis element is automatically generated by MyBatis Generator, do not modify.-->content</sql><!--根據(jù)id查找工單全部信息--><select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="ResultMapWithBLOBs">select<include refid="Base_Column_List"/>,<include refid="Blob_Column_List"/>from OrderInfowhere id = #{id,jdbcType=INTEGER}</select><select id="selectAllOrders" resultType="cn.edu.ustc.sse.workorder.bean.OrderInfo">select *from OrderInfo</select><!--使用工單編號,創(chuàng)建時間,關鍵字查詢工單--><select id="selectByKeywords" parameterType="cn.edu.ustc.sse.workorder.bean.OrderInfoCustom"resultType="cn.edu.ustc.sse.workorder.bean.OrderInfo">select * from OrderInfo<where><if test="userID!=null and userID!='' ">and userID = #{userID}</if><if test="number!=null and number!='' ">and number = #{number}</if><if test="startTime!=null and endTime!=null ">and createTime between #{startTime} and #{endTime}</if><if test="keyword!=null and keyword!='' ">and (title like concat('%' , #{keyword} ,'%') or content like concat('%' , #{keyword} ,'%'))</if></where></select><select id="selectByUserID" parameterType="java.lang.Integer" resultType="cn.edu.ustc.sse.workorder.bean.OrderInfo">select *from OrderInfowhere userID = #{userID}</select><!--根據(jù)id刪除工單信息--><delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"><!--WARNING - @mbg.generatedThis element is automatically generated by MyBatis Generator, do not modify.-->delete from OrderInfowhere id = #{id,jdbcType=INTEGER}</delete><!--插入一條工單信息--><insert id="insert" parameterType="cn.edu.ustc.sse.workorder.bean.OrderInfo"><!--WARNING - @mbg.generatedThis element is automatically generated by MyBatis Generator, do not modify.-->insert into OrderInfo (number, title,status, priority, category,userID, phone, email,attachment, engineerID, createTime,finishTime, content)values (#{number,jdbcType=VARCHAR}, #{title,jdbcType=VARCHAR},#{status,jdbcType=INTEGER}, #{priority,jdbcType=INTEGER}, #{category,jdbcType=INTEGER},#{userid,jdbcType=INTEGER}, #{phone,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR},#{attachment,jdbcType=VARCHAR}, #{engineerid,jdbcType=INTEGER}, #{createtime,jdbcType=TIMESTAMP},#{finishtime,jdbcType=TIMESTAMP}, #{content,jdbcType=LONGVARCHAR})</insert><!--根據(jù)id更新一條工單的信息--><update id="update" parameterType="cn.edu.ustc.sse.workorder.bean.OrderInfo"><!--WARNING - @mbg.generatedThis element is automatically generated by MyBatis Generator, do not modify.-->update OrderInfo<set><if test="number != null">number = #{number,jdbcType=VARCHAR},</if><if test="title != null">title = #{title,jdbcType=VARCHAR},</if><if test="status != null">status = #{status,jdbcType=INTEGER},</if><if test="priority != null">priority = #{priority,jdbcType=INTEGER},</if><if test="category != null">category = #{ategory,jdbcType=INTEGER},</if><if test="userid != null">userID = #{userid,jdbcType=INTEGER},</if><if test="phone != null">phone = #{phone,jdbcType=VARCHAR},</if><if test="email != null">email = #{email,jdbcType=VARCHAR},</if><if test="attachment != null">attachment = #{attachment,jdbcType=VARCHAR},</if><if test="engineerid != null">engineerID = #{engineerid,jdbcType=INTEGER},</if><if test="createtime != null">createTime = #{createtime,jdbcType=TIMESTAMP},</if><if test="finishtime != null">finishTime = #{finishtime,jdbcType=TIMESTAMP},</if><if test="content != null">content = #{content,jdbcType=LONGVARCHAR},</if></set>where id = #{id,jdbcType=INTEGER}</update><!--更新工單的狀態(tài) 頻繁操作 所以單獨一個接口--><update id="updateStatus" parameterType="cn.edu.ustc.sse.workorder.bean.OrderInfo">update OrderInfo set status=#{status,jdbcType=INTEGER} where id= #{id}</update></mapper> View Code然后編寫OrderMapper.java接口文件,名字要與xml對應文件的名字相同,這樣開啟包掃描的時候能夠檢測到,并能夠自動產(chǎn)生實現(xiàn)類。 ?接口中方法的名字就是xml文件中SQL語句的id名稱。
package com.example.demo.mapper;import com.example.demo.bean.OrderInfo;import java.util.List;public interface OrderMapper {public OrderInfo selectByPrimaryKey(int id);public List<OrderInfo> selectAllOrders();public int deleteByPrimaryKey(int id);public int insert(OrderInfo order);public int update(OrderInfo order);// public List<OrderInfo> selectByKeywords(OrderInfoCustom orderInfoCustom);public List<OrderInfo> selectByUserID(int id);public int updateStatus(OrderInfo orderInfo); } View Code?進行完這兩步后,還需要編寫mybatis的主配置文件mybatis_config.xml(名字可以自己隨便定義)。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration><plugins><plugin interceptor="com.github.pagehelper.PageInterceptor"><!-- 設置數(shù)據(jù)庫類型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數(shù)據(jù)庫--><property name="helperDialect" value="mysql"/></plugin></plugins><mappers></mappers></configuration> View Code然后在application.properties文件中配置數(shù)據(jù)庫的連接:
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/workorder?serverTimezone=UTC&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=Test1234_ mybatis.config-location=classpath:/mybatis/mybatis_config.xml View Code?
最后在SpringBoot項目的主入口文件上開啟mapperscan自動掃描。當然也可以在其他的配置類上加上mapperscan注解。
package com.example.demo;import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@MapperScan("com.example.demo.mapper") @SpringBootApplication public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}} View Code?
至此,mapper層的編寫工作已經(jīng)完成。至于其他的接口實現(xiàn)方式類似,就不一一演示。
轉(zhuǎn)載于:https://www.cnblogs.com/ustc-anmin/p/11049765.html
總結(jié)
以上是生活随笔為你收集整理的工单系统的设计与实现(4)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux把用户添加到组
- 下一篇: Lightweight Render P