结合上一篇所述,我们先搭建一个基本开发的框架示例
和之前一样创建一个springboot的项目
选中web、MyBatis、JDBC、MySQL选项Finish完成
在resources下创建generator文件夹和application.yml文件并删除原有的application.properties文件(这里我比较习惯用yml)
mybatis自动生成的xml的配置
[html] view plain copy
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE generatorConfiguration
- PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
- "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
- <generatorConfiguration>
- <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
- <classPathEntry location="D:\cc_work\.m2\repository\mysql\mysql-connector-java\5.1.21\mysql-connector-java-5.1.21.jar"/>
- <context id="DB2Tables" targetRuntime="MyBatis3">
- <commentGenerator>
- <property name="suppressDate" value="true"/>
- <!-- 是否去除自动生成的注释 true:是 : false:否 -->
- <property name="suppressAllComments" value="true"/>
- </commentGenerator>
- <!--数据库链接URL,用户名、密码 -->
- <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/springboot" userId="root" password="">
- </jdbcConnection>
- <javaTypeResolver>
- <property name="forceBigDecimals" value="false"/>
- </javaTypeResolver>
- <!-- 生成模型的包名和位置-->
- <javaModelGenerator targetPackage="com.springboot.domain.model" targetProject="springboot-test/src/main/java">
- <property name="enableSubPackages" value="true"/>
- <property name="trimStrings" value="true"/>
- </javaModelGenerator>
- <!-- 生成映射文件的包名和位置-->
- <sqlMapGenerator targetPackage="mapping" targetProject="springboot-test/src/main/resources">
- <property name="enableSubPackages" value="true"/>
- </sqlMapGenerator>
- <!-- 生成DAO的包名和位置-->
- <javaClientGenerator type="XMLMAPPER" targetPackage="com.springboot.domain.mapper" targetProject="springboot-test/src/main/java">
- <property name="enableSubPackages" value="true"/>
- </javaClientGenerator>
- <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
- <table tableName="wx_user" domainObjectName="WxUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
- <property name="useActualColumnNames" value="true" />
- </table>
- </context>
- </generatorConfiguration>
application.yml的配置如下:
[html] view plain copy
- server:
- port: 80
- spring:
- mvc:
- view:
- prefix: /WEB-INF/jsp/
- suffix: .jsp
- datasource:
- url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=false
- username: root
- password:
- driver-class-name: com.mysql.jdbc.Driver
- mybatis:
- mapper-locations: classpath:mapping/*.xml
- type-aliases-package: com.springboot.domain.model
执行完生成后我们接下来吧service和controller也写完。我们先看一下结构
在src/main/下面创建一个webapp WEB-INF jsp文件夹 和index.jsp文件
接下来我贴一下代码
controller
[java] view plain copy
- package com.springboot.controller;
- import java.util.List;
- import javax.annotation.Resource;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- import com.springboot.domain.model.WxUser;
- import com.springboot.service.WxUserService;
- @Controller
- @RequestMapping("wxuser")
- public class TestController {
- @Resource
- private WxUserService wxUserService;
- @RequestMapping("index")
- public String index(){
- return "index";
- }
- @RequestMapping("getData")
- @ResponseBody
- public List<WxUser> getData(){
- return wxUserService.selectWxUserList();
- }
- }
service接口就不贴了,直接实现
[java] view plain copy
- package com.springboot.service.impl;
- import java.util.List;
- import javax.annotation.Resource;
- import org.springframework.stereotype.Service;
- import com.springboot.domain.mapper.WxUserMapper;
- import com.springboot.domain.model.WxUser;
- import com.springboot.service.WxUserService;
- @Service
- public class WxUserServiceImpl implements WxUserService{
- @Resource
- private WxUserMapper wxUserMapper;
- @Override
- public List<WxUser> selectWxUserList() {
- return wxUserMapper.selectWxUserList();
- }
- }
dao接口
[java] view plain copy
- package com.springboot.domain.mapper;
- import java.util.List;
- import com.springboot.domain.model.WxUser;
- public interface WxUserMapper {
- int deleteByPrimaryKey(Integer id);
- int insert(WxUser record);
- int insertSelective(WxUser record);
- WxUser selectByPrimaryKey(Integer id);
- int updateByPrimaryKeySelective(WxUser record);
- int updateByPrimaryKey(WxUser record);
- List<WxUser> selectWxUserList();
- }
mapping映射文件
[java] view plain copy
- <?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="com.springboot.domain.mapper.WxUserMapper" >
- <resultMap id="BaseResultMap" type="com.springboot.domain.model.WxUser" >
- <id column="id" property="id" jdbcType="INTEGER" />
- <result column="nickname" property="nickname" jdbcType="VARCHAR" />
- <result column="openid" property="openid" jdbcType="VARCHAR" />
- <result column="subscribe" property="subscribe" jdbcType="VARCHAR" />
- <result column="sex" property="sex" jdbcType="VARCHAR" />
- <result column="city" property="city" jdbcType="VARCHAR" />
- <result column="country" property="country" jdbcType="VARCHAR" />
- <result column="province" property="province" jdbcType="VARCHAR" />
- <result column="language" property="language" jdbcType="VARCHAR" />
- <result column="headimgurl" property="headimgurl" jdbcType="VARCHAR" />
- <result column="subscribe_time" property="subscribe_time" jdbcType="INTEGER" />
- <result column="unionid" property="unionid" jdbcType="VARCHAR" />
- <result column="remark" property="remark" jdbcType="VARCHAR" />
- <result column="groupid" property="groupid" jdbcType="VARCHAR" />
- <result column="subscribe_scene" property="subscribe_scene" jdbcType="VARCHAR" />
- <result column="qr_scene" property="qr_scene" jdbcType="VARCHAR" />
- <result column="qr_scene_str" property="qr_scene_str" jdbcType="VARCHAR" />
- </resultMap>
- <sql id="Base_Column_List" >
- id, nickname, openid, subscribe, sex, city, country, province, language, headimgurl,
- subscribe_time, unionid, remark, groupid, subscribe_scene, qr_scene, qr_scene_str
- </sql>
- <!-- 这里是自己写的 -->
- <select id="selectWxUserList" resultMap="BaseResultMap" >
- select
- <include refid="Base_Column_List" />
- from wx_user
- </select>
- <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
- select
- <include refid="Base_Column_List" />
- from wx_user
- where id = #{id,jdbcType=INTEGER}
- </select>
- <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
- delete from wx_user
- where id = #{id,jdbcType=INTEGER}
- </delete>
- <insert id=