zoukankan      html  css  js  c++  java
  • SpringBoot开源项目学习总结

    一、实现日期格式数据类型的转换

    首先,定义DateConverter实现Converter<String, Date>接口:

     1 package com.stevlu.common;
     2 
     3 import org.springframework.core.convert.converter.Converter;
     4 
     5 import java.text.ParseException;
     6 import java.text.SimpleDateFormat;
     7 import java.util.Date;
     8 
     9 /**
    10  * Created by Steven Lu on 2018/11/21.
    11  */
    12 public class DateConverter implements Converter<String, Date> {
    13     private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    14 
    15     @Override
    16     public Date convert(String s) {
    17         if ("".equals(s) || s == null) {
    18             return null;
    19         }
    20         try {
    21             return simpleDateFormat.parse(s);
    22         } catch (ParseException e) {
    23             e.printStackTrace();
    24         }
    25         return null;
    26     }
    27 }
    View Code

     然后,在一个JAVA配置类中将该转换器注册进去:

     1 package com.stevlu.config;
     2 
     3 import org.springframework.context.annotation.Bean;
     4 import org.springframework.context.annotation.Configuration;
     5 import org.springframework.format.FormatterRegistry;
     6 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
     7 
     8 import com.stevlu.common.DateConverter;
     9 
    10 import java.util.concurrent.ExecutorService;
    11 import java.util.concurrent.Executors;
    12 
    13 /**
    14  * Created by Steven Lu on 2018/11/21.
    15  */
    16 @Configuration
    17 public class WebMvcConfig extends WebMvcConfigurerAdapter {
    18     @Override
    19     public void addFormatters(FormatterRegistry registry) {
    20         registry.addConverter(new DateConverter());
    21     }
    22 
    23     @Bean
    24     public ExecutorService executorService() {
    25         return Executors.newCachedThreadPool();
    26     }
    27 }
    View Code

     这样就在SpringBoot项目中实现了字符串类型数据转换成日期类型数据的功能。


     二、项目开发思路思考

    1、首先,可以在项目中定义需要使用到的pojo对象,以此来实现与数据库的对象关系映射,需要注意的对象中的属性最好能与数据库中的字段名称保持一致,如下图所示:

      1 package com.stevlu.bean;
      2 
      3 import com.fasterxml.jackson.annotation.JsonIgnore;
      4 import org.springframework.security.core.GrantedAuthority;
      5 import org.springframework.security.core.authority.SimpleGrantedAuthority;
      6 import org.springframework.security.core.userdetails.UserDetails;
      7 
      8 import java.util.ArrayList;
      9 import java.util.Collection;
     10 import java.util.List;
     11 
     12 /**
     13  * Created by Steven Lu on 2018/10/30.
     14  */
     15 public class Hr implements UserDetails {
     16     private Long id;
     17     private String name;
     18     private String phone;
     19     private String email;
     20     private String address;
     21     private boolean enabled;
     22     private String username;
     23     private String password;
     24     private Long dept;
     25     private List<Role> roles;
     26     private String userface;
     27 
     28     public String getUserface() {
     29         return userface;
     30     }
     31 
     32     public void setUserface(String userface) {
     33         this.userface = userface;
     34     }
     35 
     36     public List<Role> getRoles() {
     37         return roles;
     38     }
     39 
     40     public void setRoles(List<Role> roles) {
     41         this.roles = roles;
     42     }
     43 
     44     public Long getId() {
     45         return id;
     46     }
     47 
     48     public void setId(Long id) {
     49         this.id = id;
     50     }
     51 
     52     public String getName() {
     53         return name;
     54     }
     55 
     56     public void setName(String name) {
     57         this.name = name;
     58     }
     59 
     60     public String getPhone() {
     61         return phone;
     62     }
     63 
     64     public void setPhone(String phone) {
     65         this.phone = phone;
     66     }
     67 
     68     public String getEmail() {
     69         return email;
     70     }
     71 
     72     public void setEmail(String email) {
     73         this.email = email;
     74     }
     75 
     76     public String getAddress() {
     77         return address;
     78     }
     79 
     80     public void setAddress(String address) {
     81         this.address = address;
     82     }
     83 
     84     @Override
     85     public boolean isEnabled() {
     86         return enabled;
     87     }
     88 
     89     public void setEnabled(boolean enabled) {
     90         this.enabled = enabled;
     91     }
     92 
     93     @Override
     94     public String getUsername() {
     95         return username;
     96     }
     97 
     98     @JsonIgnore
     99     @Override
    100     public boolean isAccountNonExpired() {
    101         return true;
    102     }
    103 
    104     @JsonIgnore
    105     @Override
    106     public boolean isAccountNonLocked() {
    107         return true;
    108     }
    109 
    110     @JsonIgnore
    111     @Override
    112     public boolean isCredentialsNonExpired() {
    113         return true;
    114     }
    115 
    116     public void setUsername(String username) {
    117         this.username = username;
    118     }
    119 
    120     @JsonIgnore
    121     @Override
    122     public Collection<? extends GrantedAuthority> getAuthorities() {
    123         List<GrantedAuthority> authorities = new ArrayList<>();
    124         for (Role role : roles) {
    125             authorities.add(new SimpleGrantedAuthority(role.getName()));
    126         }
    127         return authorities;
    128     }
    129 
    130     @Override
    131     public String getPassword() {
    132         return password;
    133     }
    134 
    135     public void setPassword(String password) {
    136         this.password = password;
    137     }
    138 
    139     public Long getDept() {
    140         return dept;
    141     }
    142 
    143     public void setDept(Long dept) {
    144         this.dept = dept;
    145     }
    146 }
    View Code

    Hr.java

    数据库中相应字段

    2、然后思考项目中可能用到的数据库相关操作,增删改查,一对多,多对多的联系等。清楚完需要用的操作后,就可以开始编写Mybatis中mapper接口和mapper.xml文件了,详细的mapper.xml文件定义方法见之前的博客。(使用mapper接口的方法,mybatis配置文件会自动帮我们生成接口的实现类)

      1 <?xml version="1.0" encoding="UTF-8" ?>
      2 <!DOCTYPE mapper
      3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
      4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
      5 <mapper namespace="com.stevlu.mapper.HrMapper">
      6     <resultMap id="BaseResultMap" type="com.stevlu.bean.Hr">
      7         <id column="id" property="id"/>
      8         <result column="name" property="name"/>
      9         <result column="phone" property="phone"/>
     10         <result column="email" property="email"/>
     11         <result column="address" property="address"/>
     12         <result column="enabled" property="enabled"/>
     13         <result column="username" property="username"/>
     14         <result column="password" property="password"/>
     15         <result column="userface" property="userface"/>
     16         <result column="dept" property="dept"/>
     17     </resultMap>
     18     <resultMap id="lazyLoadRoles" type="com.stevlu.bean.Hr" extends="BaseResultMap">
     19         <collection property="roles" ofType="com.stevlu.bean.Role" select="com.stevlu.mapper.HrMapper.getRolesByHrId"
     20                     column="id"></collection>
     21     </resultMap>
     22     <resultMap id="eagerLoadRoles" type="com.stevlu.bean.Hr" extends="BaseResultMap">
     23         <collection property="roles" ofType="com.stevlu.bean.Role">
     24             <id property="id" column="rid"/>
     25             <result property="name" column="rname"/>
     26             <result property="nameZh" column="rnameZh"/>
     27         </collection>
     28     </resultMap>
     29     <select id="loadUserByUsername" resultMap="lazyLoadRoles">
     30         select * from tkt_hr WHERE username=#{username}
     31     </select>
     32     <select id="getRolesByHrId" resultType="com.stevlu.bean.Role">
     33         SELECT r.* FROM tkt_hr_role h, tkt_role r where h.rid=r.id AND h.hrid=#{id}
     34     </select>
     35     <insert id="hrReg">
     36         INSERT INTO tkt_hr set username=#{username},password=#{password}
     37     </insert>
     38     <update id="hrUpdatep" parameterType="com.stevlu.bean.Hr">
     39         update tkt_hr
     40         <set>
     41             <if test="password != null">
     42                 password = #{password,jdbcType=VARCHAR},
     43             </if>
     44         </set>
     45         where username = #{username}
     46     </update>
     47     <select id="getHrsByKeywords" resultMap="eagerLoadRoles">
     48         select h.*, r.id as rid, r.name as rname, r.nameZh as rnameZh from tkt_hr h
     49             left join tkt_hr_role h_r on h.id = h_r.hrid
     50             left join tkt_role r on h_r.rid = r.id
     51         where h.id not in 
     52             (select h_r.hrid from tkt_hr_role h_r, tkt_role r where h_r.rid = r.id and r.nameZh = '系统管理员')
     53         <if test="keywords!='all' and keywords!=''">
     54             and h.username like '%' || #{keywords} ||'%'
     55         </if>
     56         order by h.id
     57     </select>
     58     <select id="getHrById" resultMap="eagerLoadRoles">
     59         select h.*,r.id AS rid,r.name AS rname,r.nameZh AS rnameZh from ((tkt_hr h left join tkt_hr_role
     60         h_r on ((h.id = h_r.hrid))) left join tkt_role r on ((h_r.rid = r.id))) where h.id=#{hrId}
     61     </select>
     62     <update id="updateHr" parameterType="com.stevlu.bean.Hr">
     63         update tkt_hr
     64         <set>
     65             <if test="name != null">
     66                 name = #{name,jdbcType=VARCHAR},
     67             </if>
     68             <if test="phone != null">
     69                 phone = #{phone,jdbcType=CHAR},
     70             </if>
     71             <if test="email != null">
     72                 email = #{email,jdbcType=VARCHAR},
     73             </if>
     74             <if test="address != null">
     75                 address = #{address,jdbcType=VARCHAR},
     76             </if>
     77             <if test="enabled != null">
     78                 enabled = #{enabled,jdbcType=BIT},
     79             </if>
     80             <if test="username != null">
     81                 username = #{username,jdbcType=VARCHAR},
     82             </if>
     83             <if test="password != null">
     84                 password = #{password,jdbcType=VARCHAR},
     85             </if>
     86             <if test="userface != null">
     87                 userface = #{userface,jdbcType=VARCHAR},
     88             </if>
     89             <if test="dept != null">
     90                 dept = #{dept,jdbcType=INTEGER},
     91             </if>
     92         </set>
     93         where id = #{id}
     94     </update>
     95     <update id="updateHrDept" parameterType="com.stevlu.bean.Hr">
     96         update tkt_hr
     97         <set>
     98             <if test="dept != null">
     99                 dept = #{dept,jdbcType=INTEGER},
    100             </if>
    101         </set>
    102         where id = #{id}
    103     </update>
    104     <update id="updateHrPhone" parameterType="com.stevlu.bean.Hr">
    105         update tkt_hr
    106         <set>
    107             <if test="phone != null">
    108                 phone = #{phone,jdbcType=INTEGER},
    109             </if>
    110         </set>
    111         where id = #{id}
    112     </update>
    113     <update id="updateHrEmail" parameterType="com.stevlu.bean.Hr">
    114         update tkt_hr
    115         <set>
    116             <if test="email != null">
    117                 email = #{email,jdbcType=VARCHAR},
    118             </if>
    119         </set>
    120         where id = #{id}
    121     </update>
    122     <update id="updateHrUsername" parameterType="com.stevlu.bean.Hr">
    123         update tkt_hr
    124         <set>
    125             <if test="username != null">
    126                 username = #{username,jdbcType=VARCHAR},
    127             </if>
    128         </set>
    129         where id = #{id}
    130     </update>
    131     <update id="updateHrName" parameterType="com.stevlu.bean.Hr">
    132         update tkt_hr
    133         <set>
    134             <if test="name != null">
    135                 name = #{name,jdbcType=VARCHAR},
    136             </if>
    137         </set>
    138         where id = #{id}
    139     </update>
    140     <delete id="deleteRoleByHrId" parameterType="Long">
    141         DELETE FROM tkt_hr_role where hrid=#{hrId}
    142     </delete>
    143     <insert id="addRolesForHr">
    144         INSERT INTO tkt_hr_role(id,hrid,rid) select SEQ_TKT_HR_ROLE_NEWID.NEXTVAL, tmp.* from (
    145         <foreach collection="rids" separator="union all" item="rid">
    146             select #{hrId}, #{rid} from dual
    147         </foreach>
    148         ) tmp
    149     </insert>
    150     <delete id="deleteHr" parameterType="Long">
    151         DELETE FROM tkt_hr WHERE id=#{hrId}
    152     </delete>
    153     <select id="getAllHr" resultType="com.stevlu.bean.Hr">
    154         select * from tkt_hr
    155         <if test="currentId!=null">
    156             WHERE id !=#{currentId}
    157         </if>
    158     </select>
    159 </mapper>
    View Code
     1 package com.stevlu.mapper;
     2 
     3 import org.apache.ibatis.annotations.Param;
     4 
     5 import com.stevlu.bean.Hr;
     6 import com.stevlu.bean.Role;
     7 
     8 import java.util.List;
     9 
    10 /**
    11  * Created by Steven Lu on 2018/10/30.
    12  */
    13 public interface HrMapper {
    14     Hr loadUserByUsername(String username);
    15 
    16     List<Role> getRolesByHrId(Long id);
    17 
    18     int hrReg(@Param("username") String username, @Param("password") String password);
    19 
    20     int hrUpdatep(@Param("username") String username, @Param("password") String password);
    21     
    22     List<Hr> getHrsByKeywords(@Param("keywords") String keywords);
    23 
    24     int updateHr(Hr hr);
    25 
    26     int updateHrDept(Hr hr);
    27     
    28     int updateHrPhone(Hr hr);
    29     
    30     int updateHrEmail(Hr hr);
    31     
    32     int updateHrUsername(Hr hr);
    33     
    34     int updateHrName(Hr hr);
    35 
    36     int deleteRoleByHrId(Long hrId);
    37 
    38     int addRolesForHr(@Param("hrId") Long hrId, @Param("rids") Long[] rids);
    39 
    40     Hr getHrById(Long hrId);
    41 
    42     int deleteHr(Long hrId);
    43 
    44     List<Hr> getAllHr(@Param("currentId") Long currentId);
    45 }
    View Code

     要想让配置的mapper文件和mapper接口生效,需要在配置类中加上包扫描的注解,当然在全局配置文件中也可以设置。

    package com.stevlu;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    @MapperScan("com.stevlu.mapper")
    public class WebSocketServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(WebSocketServerApplication.class, args);
        }
    }
    View Code

    3、操作数据库的mapper接口定义完成之后,我们就可以在service层调用定义好的mapper接口。(service层可以对传经来的参数进行一些处理)

      1 package com.stevlu.service;
      2 
      3 import org.springframework.beans.factory.annotation.Autowired;
      4 import org.springframework.security.core.userdetails.UserDetails;
      5 import org.springframework.security.core.userdetails.UserDetailsService;
      6 import org.springframework.security.core.userdetails.UsernameNotFoundException;
      7 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
      8 import org.springframework.stereotype.Service;
      9 import org.springframework.transaction.annotation.Transactional;
     10 
     11 import com.stevlu.bean.Hr;
     12 import com.stevlu.common.HrUtils;
     13 import com.stevlu.mapper.HrMapper;
     14 
     15 import java.util.List;
     16 
     17 /**
     18  * Created by Steven Lu on 2018/10/30.
     19  */
     20 @Service
     21 @Transactional
     22 public class HrService implements UserDetailsService {
     23 
     24     @Autowired
     25     HrMapper hrMapper;
     26 
     27     @Override
     28     public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
     29         Hr hr = hrMapper.loadUserByUsername(s);
     30         if (hr == null) {
     31             throw new UsernameNotFoundException("用户名不对");
     32         }
     33         return hr;
     34     }
     35 
     36     public int hrReg(String username, String password) {
     37         // 如果用户名存在,返回错误
     38         if (hrMapper.loadUserByUsername(username) != null) {
     39             return -1;
     40         }
     41         BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
     42         String encode = encoder.encode(password);
     43         return hrMapper.hrReg(username, encode);
     44     }
     45     
     46     public int hrUpdatep(String username, String oldPassword, String newPassword, String encodedPassword) {
     47         // 如果旧密码错误,返回错误
     48         BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
     49         if (encoder.matches(oldPassword, encodedPassword)) {
     50             String encode = encoder.encode(newPassword);
     51             return hrMapper.hrUpdatep(username, encode);
     52         } else {
     53             return -1;
     54         }
     55     }
     56 
     57     public List<Hr> getHrsByKeywords(String keywords) {
     58         return hrMapper.getHrsByKeywords(keywords);
     59     }
     60 
     61     public int updateHr(Hr hr) {
     62         return hrMapper.updateHr(hr);
     63     }
     64 
     65     public int updateHrDept(Hr hr) {
     66         return hrMapper.updateHrDept(hr);
     67     }
     68     
     69     public int updateHrPhone(Hr hr) {
     70         return hrMapper.updateHrPhone(hr);
     71     }
     72     
     73     public int updateHrEmail(Hr hr) {
     74         return hrMapper.updateHrEmail(hr);
     75     }
     76     
     77     public int updateHrUsername(Hr hr) {
     78         return hrMapper.updateHrUsername(hr);
     79     }
     80     
     81     public int updateHrName(Hr hr) {
     82         return hrMapper.updateHrName(hr);
     83     }
     84 
     85     public int updateHrRoles(Long hrId, Long[] rids) {
     86         int i = hrMapper.deleteRoleByHrId(hrId);
     87         return hrMapper.addRolesForHr(hrId, rids);
     88     }
     89 
     90     public Hr getHrById(Long hrId) {
     91         return hrMapper.getHrById(hrId);
     92     }
     93 
     94     public int deleteHr(Long hrId) {
     95         return hrMapper.deleteHr(hrId);
     96     }
     97 
     98     public List<Hr> getAllHrExceptAdmin() {
     99         return hrMapper.getAllHr(HrUtils.getCurrentHr().getId());
    100     }
    101 
    102     public List<Hr> getAllHr() {
    103         return hrMapper.getAllHr(null);
    104     }
    105 }
    View Code

     4、编写controller层,用于实现url请求的映射,由于项目是RESTful风格的,所以要注意url路径设计的技巧,以及HTTP请求的类型是什么,一般为(get查询,post增加,delete删除,put更新)

    示例代码;

      1 package com.stevlu.controller;
      2 
      3 import org.springframework.beans.factory.annotation.Autowired;
      4 import org.springframework.web.bind.annotation.PathVariable;
      5 import org.springframework.web.bind.annotation.RequestMapping;
      6 import org.springframework.web.bind.annotation.RequestMethod;
      7 import org.springframework.web.bind.annotation.RestController;
      8 
      9 import com.stevlu.bean.*;
     10 import com.stevlu.service.*;
     11 
     12 import java.util.HashMap;
     13 import java.util.List;
     14 import java.util.Map;
     15 
     16 /**
     17  * Created by Steven Lu on 2018/11/21.
     18  */
     19 @RestController
     20 @RequestMapping("/system/basic")
     21 public class SystemBasicController {
     22     @Autowired
     23     RoleService roleService;
     24     @Autowired
     25     MenuService menuService;
     26     @Autowired
     27     MenuRoleService menuRoleService;
     28     @Autowired
     29     DepartmentService departmentService;
     30     @Autowired
     31     PositionService positionService;
     32     @Autowired
     33     JobLevelService jobLevelService;
     34     @Autowired
     35     TicketService ticketService;
     36 
     37     @RequestMapping(value = "/role/{rid}", method = RequestMethod.DELETE)
     38     public RespBean deleteRole(@PathVariable Long rid) {
     39         if (roleService.deleteRoleById(rid) == 1) {
     40             return new RespBean("success", "删除成功!");
     41         }
     42         return new RespBean("error", "删除失败!");
     43     }
     44 
     45     @RequestMapping(value = "/addRole", method = RequestMethod.POST)
     46     public RespBean addNewRole(String role, String roleZh) {
     47         if (roleService.addNewRole(role, roleZh) == 1) {
     48             return new RespBean("success", "添加成功!");
     49         }
     50         return new RespBean("error", "添加失败!");
     51     }
     52 
     53     @RequestMapping(value = "/menuTree/{rid}", method = RequestMethod.GET)
     54     public Map<String, Object> menuTree(@PathVariable Long rid) {
     55         Map<String, Object> map = new HashMap<>();
     56         List<Menu> menus = menuService.menuTree();
     57         map.put("menus", menus);
     58         List<Long> selMids = menuService.getMenusByRid(rid);
     59         map.put("mids", selMids);
     60         return map;
     61     }
     62 
     63     @RequestMapping(value = "/updateMenuRole", method = RequestMethod.PUT)
     64     public RespBean updateMenuRole(Long rid, Long[] mids) {
     65         if (menuRoleService.updateMenuRole(rid, mids) == mids.length) {
     66             return new RespBean("success", "更新成功!");
     67         }
     68         return new RespBean("error", "更新失败!");
     69     }
     70 
     71     @RequestMapping("/roles")
     72     public List<Role> allRoles() {
     73         return roleService.roles();
     74     }
     75 
     76     @RequestMapping(value = "/dep", method = RequestMethod.POST)
     77     public Map<String, Object> addDep(Department department) {
     78         Map<String, Object> map = new HashMap<>();
     79         int result = departmentService.addDep(department);
     80         if (result >= 1) {
     81             map.put("status", "success");
     82             map.put("msg", department);
     83             return map;
     84         }
     85         map.put("status", "error");
     86         map.put("msg", "添加失败!");
     87         return map;
     88     }
     89 
     90     @RequestMapping(value = "/dep/{did}", method = RequestMethod.DELETE)
     91     public RespBean deleteDep(@PathVariable Long did) {
     92         int result = departmentService.deleteDep(did);
     93         if (result > 0) {
     94             return new RespBean("success", "删除成功!");
     95         } else if (result == -1) {
     96             return new RespBean("error", "部门正被员工使用,取消关联前无法删除!");
     97         }
     98         return new RespBean("error", "删除失败!");
     99     }
    100 
    101     @RequestMapping(value = "/dep/{pid}", method = RequestMethod.GET)
    102     public List<Department> getDepByPid(@PathVariable Long pid) {
    103         return departmentService.getDepByPid(pid);
    104     }
    105 
    106     @RequestMapping(value = "/deps", method = RequestMethod.GET)
    107     public List<Department> getAllDeps() {
    108         return departmentService.getAllDeps();
    109     }
    110 
    111     @RequestMapping(value = "/position", method = RequestMethod.POST)
    112     public RespBean addPos(Position pos) {
    113         int result = positionService.addPos(pos);
    114         if (result == 1) {
    115             return new RespBean("success", "添加成功!");
    116         } else if (result == -1) {
    117             return new RespBean("error", "职位名重复,添加失败!");
    118         }
    119         return new RespBean("error", "添加失败!");
    120     }
    121 
    122     @RequestMapping(value = "/positions", method = RequestMethod.GET)
    123     public List<Position> getAllPos() {
    124         return positionService.getAllPos();
    125     }
    126 
    127     @RequestMapping("/position/{pids}")
    128     public RespBean deletePosById(@PathVariable String pids) {
    129         if (positionService.deletePosById(pids)) {
    130             return new RespBean("success", "删除成功!");
    131         }
    132         return new RespBean("error", "删除失败!");
    133     }
    134 
    135     @RequestMapping(value = "/position", method = RequestMethod.PUT)
    136     public RespBean updatePosById(Position position) {
    137         if (positionService.updatePosById(position) == 1) {
    138             return new RespBean("success", "修改成功!");
    139         }
    140         return new RespBean("error", "修改失败!");
    141     }
    142 
    143     @RequestMapping(value = "/joblevel", method = RequestMethod.POST)
    144     public RespBean addJobLevel(JobLevel jobLevel) {
    145         int result = jobLevelService.addJobLevel(jobLevel);
    146         if (result == 1) {
    147             return new RespBean("success", "添加成功!");
    148         } else if (result == -1) {
    149             return new RespBean("error", "职称名重复,添加失败!");
    150         }
    151         return new RespBean("error", "添加失败!");
    152     }
    153 
    154     @RequestMapping(value = "/joblevels", method = RequestMethod.GET)
    155     public List<JobLevel> getAllJobLevels() {
    156         return jobLevelService.getAllJobLevels();
    157     }
    158 
    159     @RequestMapping(value = "/joblevel/{ids}", method = RequestMethod.DELETE)
    160     public RespBean deleteJobLevelById(@PathVariable String ids) {
    161         if (jobLevelService.deleteJobLevelById(ids)) {
    162             return new RespBean("success", "删除成功!");
    163         }
    164         return new RespBean("error", "删除失败!");
    165     }
    166 
    167     @RequestMapping(value = "/joblevel", method = RequestMethod.PUT)
    168     public RespBean updateJobLevel(JobLevel jobLevel) {
    169         if (jobLevelService.updateJobLevel(jobLevel) == 1) {
    170             return new RespBean("success", "修改成功!");
    171         }
    172         return new RespBean("error", "修改失败!");
    173     }
    174 }
    View Code


     最后不要忘记在application.properties文件中配置数据库的连接属性,mybatis配置文件所在的路径等相关配置。

    # EMBEDDED SERVER CONFIGURATION (ServerProperties)
    server.port=8082
    server.session-timeout=1800
    server.context-path=
    server.tomcat.max-threads=0
    server.tomcat.uri-encoding=UTF-8
    server.tomcat.basedir=target/tomcat
    
    # HTTP encoding (HttpEncodingProperties)
    spring.http.encoding.charset=UTF-8
    spring.http.encoding.enabled=true 
    spring.http.encoding.force=true
    
    #datasource
    spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/travel?serverTimezone=UTC&characterEncoding=utf-8
    spring.datasource.username=root
    spring.datasource.password=root
    #spring.datasource.url=jdbc:oracle:thin:@//192.168.103.235:1521/taf
    #spring.datasource.username=elite_ynj
    #spring.datasource.password=EYuc1809#
    mybatis.config-location=classpath:/mybatis-config.xml
    View Code

     该工程一个完整的目录结构如下:

  • 相关阅读:
    第三个实验代码
    20165104孟凡斌-第五周作业
    20165104孟凡斌-第四周作业
    20165104孟凡斌-第二次java考试课下作业
    20165104孟凡斌-第三周作业
    20165104孟凡斌-第二次JAVA作业
    20165104孟凡斌-第一次Java考试课下作业
    2018-2019-1 《信息安全系统设计基础》 20165235 实验五 通信协议设计
    2018-2019-1 20165235 实验四 外设驱动程序设计
    20165235 实现pwd功能
  • 原文地址:https://www.cnblogs.com/ustc-anmin/p/10533459.html
Copyright © 2011-2022 走看看