eclipse搭建springboot2.0.2、mybatis、springmvc、log4j、devtools热部署基本搭建

结合上一篇所述,我们先搭建一个基本开发的框架示例

和之前一样创建一个springboot的项目

选中web、MyBatis、JDBC、MySQL选项Finish完成

在resources下创建generator文件夹和application.yml文件并删除原有的application.properties文件(这里我比较习惯用yml)

mybatis自动生成的xml的配置

[html] view plain copy

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE generatorConfiguration  
  3.         PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"  
  4.         "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">  
  5. <generatorConfiguration>  
  6.     <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->  
  7.     <classPathEntry  location="D:\cc_work\.m2\repository\mysql\mysql-connector-java\5.1.21\mysql-connector-java-5.1.21.jar"/>  
  8.     <context id="DB2Tables"  targetRuntime="MyBatis3">  
  9.         <commentGenerator>  
  10.             <property name="suppressDate" value="true"/>  
  11.             <!-- 是否去除自动生成的注释 true:是 : false:否 -->  
  12.             <property name="suppressAllComments" value="true"/>  
  13.         </commentGenerator>  
  14.         <!--数据库链接URL,用户名、密码 -->  
  15.         <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/springboot" userId="root" password="">  
  16.         </jdbcConnection>  
  17.         <javaTypeResolver>  
  18.             <property name="forceBigDecimals" value="false"/>  
  19.         </javaTypeResolver>  
  20.         <!-- 生成模型的包名和位置-->  
  21.         <javaModelGenerator targetPackage="com.springboot.domain.model" targetProject="springboot-test/src/main/java">  
  22.             <property name="enableSubPackages" value="true"/>  
  23.             <property name="trimStrings" value="true"/>  
  24.         </javaModelGenerator>  
  25.         <!-- 生成映射文件的包名和位置-->  
  26.         <sqlMapGenerator targetPackage="mapping" targetProject="springboot-test/src/main/resources">  
  27.             <property name="enableSubPackages" value="true"/>  
  28.         </sqlMapGenerator>  
  29.         <!-- 生成DAO的包名和位置-->  
  30.         <javaClientGenerator type="XMLMAPPER" targetPackage="com.springboot.domain.mapper" targetProject="springboot-test/src/main/java">  
  31.             <property name="enableSubPackages" value="true"/>  
  32.         </javaClientGenerator>  
  33.         <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->  
  34.         <table tableName="wx_user" domainObjectName="WxUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">  
  35.             <property name="useActualColumnNames" value="true" />  
  36.         </table>  
  37.     </context>  
  38. </generatorConfiguration>  

application.yml的配置如下:

 

[html] view plain copy

  1. server:  
  2.   port: 80  
  3.   
  4. spring:  
  5.   mvc:  
  6.     view:  
  7.       prefix: /WEB-INF/jsp/  
  8.       suffix: .jsp  
  9.   datasource:  
  10.     url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=false  
  11.     username: root  
  12.     password:   
  13.     driver-class-name: com.mysql.jdbc.Driver  
  14.   
  15. mybatis:  
  16.   mapper-locations: classpath:mapping/*.xml  
  17.   type-aliases-package: com.springboot.domain.model  

执行完生成后我们接下来吧service和controller也写完。我们先看一下结构

在src/main/下面创建一个webapp WEB-INF jsp文件夹 和index.jsp文件

接下来我贴一下代码

controller

[java] view plain copy

  1. package com.springboot.controller;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.annotation.Resource;  
  6.   
  7. import org.springframework.stereotype.Controller;  
  8. import org.springframework.web.bind.annotation.RequestMapping;  
  9. import org.springframework.web.bind.annotation.ResponseBody;  
  10.   
  11. import com.springboot.domain.model.WxUser;  
  12. import com.springboot.service.WxUserService;  
  13.   
  14. @Controller  
  15. @RequestMapping("wxuser")  
  16. public class TestController {  
  17.   
  18.     @Resource  
  19.     private WxUserService wxUserService;  
  20.       
  21.     @RequestMapping("index")  
  22.     public String index(){  
  23.           
  24.         return "index";  
  25.     }  
  26.       
  27.     @RequestMapping("getData")  
  28.     @ResponseBody  
  29.     public List<WxUser> getData(){  
  30.           
  31.         return wxUserService.selectWxUserList();  
  32.     }  
  33. }  

service接口就不贴了,直接实现

[java] view plain copy

  1. package com.springboot.service.impl;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.annotation.Resource;  
  6.   
  7. import org.springframework.stereotype.Service;  
  8.   
  9. import com.springboot.domain.mapper.WxUserMapper;  
  10. import com.springboot.domain.model.WxUser;  
  11. import com.springboot.service.WxUserService;  
  12.   
  13. @Service  
  14. public class WxUserServiceImpl implements WxUserService{  
  15.   
  16.     @Resource  
  17.     private WxUserMapper wxUserMapper;  
  18.       
  19.     @Override  
  20.     public List<WxUser> selectWxUserList() {  
  21.         return wxUserMapper.selectWxUserList();  
  22.     }  
  23.   
  24. }  

dao接口

[java] view plain copy

  1. package com.springboot.domain.mapper;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.springboot.domain.model.WxUser;  
  6.   
  7. public interface WxUserMapper {  
  8.     int deleteByPrimaryKey(Integer id);  
  9.   
  10.     int insert(WxUser record);  
  11.   
  12.     int insertSelective(WxUser record);  
  13.   
  14.     WxUser selectByPrimaryKey(Integer id);  
  15.   
  16.     int updateByPrimaryKeySelective(WxUser record);  
  17.   
  18.     int updateByPrimaryKey(WxUser record);  
  19.       
  20.     List<WxUser> selectWxUserList();  
  21. }  

mapping映射文件

[java] view plain copy

  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.springboot.domain.mapper.WxUserMapper" >  
  4.   <resultMap id="BaseResultMap" type="com.springboot.domain.model.WxUser" >  
  5.     <id column="id" property="id" jdbcType="INTEGER" />  
  6.     <result column="nickname" property="nickname" jdbcType="VARCHAR" />  
  7.     <result column="openid" property="openid" jdbcType="VARCHAR" />  
  8.     <result column="subscribe" property="subscribe" jdbcType="VARCHAR" />  
  9.     <result column="sex" property="sex" jdbcType="VARCHAR" />  
  10.     <result column="city" property="city" jdbcType="VARCHAR" />  
  11.     <result column="country" property="country" jdbcType="VARCHAR" />  
  12.     <result column="province" property="province" jdbcType="VARCHAR" />  
  13.     <result column="language" property="language" jdbcType="VARCHAR" />  
  14.     <result column="headimgurl" property="headimgurl" jdbcType="VARCHAR" />  
  15.     <result column="subscribe_time" property="subscribe_time" jdbcType="INTEGER" />  
  16.     <result column="unionid" property="unionid" jdbcType="VARCHAR" />  
  17.     <result column="remark" property="remark" jdbcType="VARCHAR" />  
  18.     <result column="groupid" property="groupid" jdbcType="VARCHAR" />  
  19.     <result column="subscribe_scene" property="subscribe_scene" jdbcType="VARCHAR" />  
  20.     <result column="qr_scene" property="qr_scene" jdbcType="VARCHAR" />  
  21.     <result column="qr_scene_str" property="qr_scene_str" jdbcType="VARCHAR" />  
  22.   </resultMap>  
  23.   <sql id="Base_Column_List" >  
  24.     id, nickname, openid, subscribe, sex, city, country, province, language, headimgurl,   
  25.     subscribe_time, unionid, remark, groupid, subscribe_scene, qr_scene, qr_scene_str  
  26.   </sql>  
  27.   <!-- 这里是自己写的 -->  
  28.   <select id="selectWxUserList" resultMap="BaseResultMap" >  
  29.     select   
  30.     <include refid="Base_Column_List" />  
  31.     from wx_user  
  32.   </select>  
  33.   <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >  
  34.     select   
  35.     <include refid="Base_Column_List" />  
  36.     from wx_user  
  37.     where id = #{id,jdbcType=INTEGER}  
  38.   </select>  
  39.   <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >  
  40.     delete from wx_user  
  41.     where id = #{id,jdbcType=INTEGER}  
  42.   </delete>  
  43.   <insert id="insert" parameterType="com.sprin
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值