springboot整合mybatis

SpringBoot整合Mybatis

1.引入maven依赖包:

   <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.0</version>
        </dependency>

实体层,dao层,mapper文件可通过你想工程生成

2.创建实体user (core.pojo包下)

package core.pojo;

import java.util.Date;

public class User {
    private String num;

    private String name;

    private String password;

    private String partNum;

    private String sex;

    private String phone;

    private String email;

    private Boolean flag;

    private Boolean status;

    private String createby;

    private Date createtime;

    private String updateby;

    private Date updatetime;

    public User(String num, String name, String password, String partNum, String sex, String phone, String email, Boolean flag, Boolean status, String createby, Date createtime, String updateby, Date updatetime) {
        this.num = num;
        this.name = name;
        this.password = password;
        this.partNum = partNum;
        this.sex = sex;
        this.phone = phone;
        this.email = email;
        this.flag = flag;
        this.status = status;
        this.createby = createby;
        this.createtime = createtime;
        this.updateby = updateby;
        this.updatetime = updatetime;
    }

    public User() {
        super();
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num == null ? null : num.trim();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public String getPartNum() {
        return partNum;
    }

    public void setPartNum(String partNum) {
        this.partNum = partNum == null ? null : partNum.trim();
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex == null ? null : sex.trim();
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone == null ? null : phone.trim();
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email == null ? null : email.trim();
    }

    public Boolean getFlag() {
        return flag;
    }

    public void setFlag(Boolean flag) {
        this.flag = flag;
    }

    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

    public String getCreateby() {
        return createby;
    }

    public void setCreateby(String createby) {
        this.createby = createby == null ? null : createby.trim();
    }

    public Date getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }

    public String getUpdateby() {
        return updateby;
    }

    public void setUpdateby(String updateby) {
        this.updateby = updateby == null ? null : updateby.trim();
    }

    public Date getUpdatetime() {
        return updatetime;
    }

    public void setUpdatetime(Date updatetime) {
        this.updatetime = updatetime;
    }

    @Override
    public String toString() {
        return "User{" +
                "num=‘" + num + ‘\‘‘ +
                ", name=‘" + name + ‘\‘‘ +
                ", password=‘" + password + ‘\‘‘ +
                ", partNum=‘" + partNum + ‘\‘‘ +
                ", sex=‘" + sex + ‘\‘‘ +
                ", phone=‘" + phone + ‘\‘‘ +
                ", email=‘" + email + ‘\‘‘ +
                ", flag=" + flag +
                ", status=" + status +
                ", createby=‘" + createby + ‘\‘‘ +
                ", createtime=" + createtime +
                ", updateby=‘" + updateby + ‘\‘‘ +
                ", updatetime=" + updatetime +
                ‘}‘;
    }
}

 

3.创建UserMapper.xml (resources.mapper包下)

<?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="core.dao.UserDao" >
  <resultMap id="BaseResultMap" type="User" >
    <constructor >
      <idArg column="num" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="name" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="password" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="part_num" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="sex" jdbcType="CHAR" javaType="java.lang.String" />
      <arg column="phone" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="email" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="flag" jdbcType="BIT" javaType="java.lang.Boolean" />
      <arg column="status" jdbcType="BIT" javaType="java.lang.Boolean" />
      <arg column="createby" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="createtime" jdbcType="TIMESTAMP" javaType="java.util.Date" />
      <arg column="updateby" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="updatetime" jdbcType="TIMESTAMP" javaType="java.util.Date" />
    </constructor>
  </resultMap>
  <sql id="Base_Column_List" >
    num, name, password, part_num, sex, phone, email, flag, status, createby, createtime, 
    updateby, updatetime
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from user
    where num = #{num,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
    delete from user
    where num = #{num,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="User" >
    insert into user (num, name, password, 
      part_num, sex, phone, email, 
      flag, status, createby, createtime, 
      updateby, updatetime)
    values (#{num,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
      #{partNum,jdbcType=VARCHAR}, #{sex,jdbcType=CHAR}, #{phone,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, 
      #{flag,jdbcType=BIT}, #{status,jdbcType=BIT}, #{createby,jdbcType=VARCHAR}, #{createtime,jdbcType=TIMESTAMP}, 
      #{updateby,jdbcType=VARCHAR}, #{updatetime,jdbcType=TIMESTAMP})
  </insert>
  <insert id="insertSelective" parameterType="User" >
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="num != null" >
        num,
      </if>
      <if test="name != null" >
        name,
      </if>
      <if test="password != null" >
        password,
      </if>
      <if test="partNum != null" >
        part_num,
      </if>
      <if test="sex != null" >
        sex,
      </if>
      <if test="phone != null" >
        phone,
      </if>
      <if test="email != null" >
        email,
      </if>
      <if test="flag != null" >
        flag,
      </if>
      <if test="status != null" >
        status,
      </if>
      <if test="createby != null" >
        createby,
      </if>
      <if test="createtime != null" >
        createtime,
      </if>
      <if test="updateby != null" >
        updateby,
      </if>
      <if test="updatetime != null" >
        updatetime,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="num != null" >
        #{num,jdbcType=VARCHAR},
      </if>
      <if test="name != null" >
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="partNum != null" >
        #{partNum,jdbcType=VARCHAR},
      </if>
      <if test="sex != null" >
        #{sex,jdbcType=CHAR},
      </if>
      <if test="phone != null" >
        #{phone,jdbcType=VARCHAR},
      </if>
      <if test="email != null" >
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="flag != null" >
        #{flag,jdbcType=BIT},
      </if>
      <if test="status != null" >
        #{status,jdbcType=BIT},
      </if>
      <if test="createby != null" >
        #{createby,jdbcType=VARCHAR},
      </if>
      <if test="createtime != null" >
        #{createtime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateby != null" >
        #{updateby,jdbcType=VARCHAR},
      </if>
      <if test="updatetime != null" >
        #{updatetime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="User" >
    update user
    <set >
      <if test="name != null" >
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="partNum != null" >
        part_num = #{partNum,jdbcType=VARCHAR},
      </if>
      <if test="sex != null" >
        sex = #{sex,jdbcType=CHAR},
      </if>
      <if test="phone != null" >
        phone = #{phone,jdbcType=VARCHAR},
      </if>
      <if test="email != null" >
        email = #{email,jdbcType=VARCHAR},
      </if>
      <if test="flag != null" >
        flag = #{flag,jdbcType=BIT},
      </if>
      <if test="status != null" >
        status = #{status,jdbcType=BIT},
      </if>
      <if test="createby != null" >
        createby = #{createby,jdbcType=VARCHAR},
      </if>
      <if test="createtime != null" >
        createtime = #{createtime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateby != null" >
        updateby = #{updateby,jdbcType=VARCHAR},
      </if>
      <if test="updatetime != null" >
        updatetime = #{updatetime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where num = #{num,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="User" >
    update user
    set name = #{name,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      part_num = #{partNum,jdbcType=VARCHAR},
      sex = #{sex,jdbcType=CHAR},
      phone = #{phone,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR},
      flag = #{flag,jdbcType=BIT},
      status = #{status,jdbcType=BIT},
      createby = #{createby,jdbcType=VARCHAR},
      createtime = #{createtime,jdbcType=TIMESTAMP},
      updateby = #{updateby,jdbcType=VARCHAR},
      updatetime = #{updatetime,jdbcType=TIMESTAMP}
    where num = #{num,jdbcType=VARCHAR}
  </update>
</mapper>

4.创建dao层接口 (core.dao包下)

package core.dao; import core.pojo.User; public interface UserDao { int deleteByPrimaryKey(String num); int insert(User record); int insertSelective(User record); User selectByPrimaryKey(String num); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record); }

5.创建service接口( core.service包下):

package core.service; import core.pojo.User; public interface UserService { User selectByPrimaryKey(String num); }

6.实现service接口(core.service.impl包下):

package core.service.impl; import core.dao.UserDao; import core.pojo.User; import core.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @Transactional public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public User selectByPrimaryKey(String num) { return userDao.selectByPrimaryKey(num); } }

7.创建controller层代码(core.controller包下):

package core.controller;
import core.pojo.User;
import core.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/getUser")
    public @ResponseBody User GetUser(){
        System.out.println("================");
        User user=userService.selectByPrimaryKey("1001");
        return user;
    }

    @RequestMapping("/test")
    public void test(HttpServletRequest req){
        System.out.println("==================");
        System.out.println(req.getParameter("name").toString());
        System.out.println("---------------------");
    }
}

8.配置application.yml (resources包下):

server:
  port: 8082

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/yh01_boot_mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

mybatis:
  type-aliases-package: core.pojo
  mapper-locations: classpath:mapper/*.xml
  config-location:
#showSql
logging:
  level:
    com:
      example:
        mapper : debug

9.修改启动类(core包下),扫描dao层

package core;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("core.dao")
public class SpringMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringMybatisApplication.class, args);
    }

}

10.启动测试:http://localhost:8082/user/getUser

分享图片

相关文章
相关标签/搜索
神算子心水高手论坛精选一肖 格尔木市| 大庆市| 海盐县| 台东县| 双峰县| 新沂市| 通山县| 读书| 高安市| 故城县| 沿河| 沁源县| 镇平县| 澳门| 黔南| 辽中县| 广丰县| 武隆县| 宜兴市| 田东县| 龙川县| 桦南县| 依兰县| 永平县| 如皋市| 汉中市| 定远县| 屯留县| 丹巴县| 南和县| 卢湾区| 新蔡县| 孟津县| 临桂县| 南阳市| 夏邑县| 永安市| 离岛区| 新蔡县| 呼和浩特市| 友谊县| 电白县| 海口市| 博乐市| 巴林右旗| 县级市| 怀仁县| 延川县| 廊坊市| 富平县| 卢氏县| 明水县| 山丹县| 随州市| 长治县| 远安县| 永德县| 江都市| 岑溪市| 思南县| 满城县| 建德市| 安溪县| 高要市| 武定县| 甘德县| 五华县| 濉溪县| 东源县| 富蕴县| 蚌埠市| 曲靖市| 巴彦淖尔市| 开江县| 宁南县| 政和县| 定安县| 新津县| 泰和县| 通许县| 顺昌县| 太谷县| 赞皇县| 仁化县| 巴彦淖尔市| 大名县| 昭平县| 承德市| 西贡区| 米易县| 永清县| 台东县| 锡林郭勒盟| 明水县| 襄城县| 绵竹市| 兴山县| 临汾市| 渭源县| 衡南县| 葫芦岛市| 红安县| 寻甸| 抚松县| 清河县| 莱西市| 和龙市| 韶山市| 西峡县| 平邑县| 吐鲁番市| 兴和县| 铜鼓县| 新河县| 新建县| 会宁县| 镇原县| 鄢陵县| 遂平县| 永泰县| 南通市| 杭锦后旗| 安乡县| 林州市| 葫芦岛市| 饶河县| 云林县| 军事| 安乡县| 惠来县| 绥棱县| 仙居县| 江山市| 梁平县| 垦利县| 海安县| 谢通门县| 文昌市| 南城县| 米泉市| 遂川县| 东台市| 青神县| 太原市| 东安县| 久治县| 河东区| 邯郸县| 依安县| 南昌市| 镇原县| 永福县| 全南县| 苏州市| 阜城县| 勃利县| 延寿县| 尚义县| 五原县| 凉城县| 东安县| 平顶山市| 桂阳县| 垦利县| 安仁县| 阿拉善右旗| 达拉特旗| 邹平县| 塔河县| 宁蒗| 巴林右旗| 张家川| 丁青县| 周宁县| 江川县| 依兰县| 犍为县| 赫章县| 玉溪市| 都昌县| 东乌珠穆沁旗| 太保市| 勐海县| 建德市| 砚山县| 张家口市| 瑞安市| 海淀区| 彭山县| 滦南县| 丰县| 临猗县| 竹北市| 聂拉木县| 理塘县| 驻马店市| 伊吾县| 措勤县| 吉水县| 天水市| 澄迈县| 寿宁县| 定襄县| 毕节市| 望都县| 杭州市| 大邑县| 竹山县| 福泉市| 息烽县| 玉环县| 保亭| 和平县| 墨江| 和林格尔县| 河东区| 梧州市| 大兴区| 盐津县| 洛阳市| 玉林市| 榕江县| 泽库县| 宣武区| 江达县| 平武县| 响水县| 景宁| 体育| 莫力| 秦安县| 六枝特区| 若羌县| 嘉鱼县| 凌云县| 佛冈县| 皮山县| 伊吾县| 龙陵县| 芦山县| 察雅县| 开封市| 普陀区| 张家口市| 宜宾县| 固阳县| 揭东县| 刚察县| 射阳县| 新邵县| 仁寿县| 西安市| 巧家县| 巴南区| 东明县| 南昌市| 新河县| 滦南县| 兰州市| 太康县| 东港市| 陇川县| 福清市| 八宿县| 长武县| 门源| 噶尔县| 兴仁县| 志丹县| 甘德县| 琼结县| 罗田县| 吉首市| 虎林市| 云南省| 黎川县| 日喀则市| 栾城县| 翁源县| 宁陕县| 高尔夫| 辉县市| 永安市| 博兴县| 东山县| 绍兴县| 太谷县| 万荣县| 通辽市| 溧阳市| 景泰县| 铅山县| 出国| 同仁县| 大同县| 松阳县| 揭东县| 寻甸| 阳西县| 阿坝县| 信宜市| 闽侯县| 文安县| 肇州县| 吉木萨尔县| 平凉市| 东宁县| 库车县| 肇源县| 宣汉县| 清水县| 历史| 扶绥县| 长顺县| 彰化县| 广水市| 桃源县| 定安县| 松潘县| 达州市| 宜城市| 铁岭市| 天气| 哈尔滨市| 乌鲁木齐县| 崇左市| 镇原县| 循化| 吉隆县| 延安市| 明水县| 宝清县| 云和县| 张北县| 新和县| 平舆县| 米脂县| 卢湾区| 志丹县| 阿拉善左旗| 神木县| 泾源县| 积石山| 收藏| 开远市| 晋宁县| 兴海县| 弥渡县| 墨竹工卡县| 岢岚县| 和田市| 沾益县| 马鞍山市| 壤塘县| 普兰店市| 田阳县| 东乡族自治县| 开平市| 江西省| 邹平县| 兴仁县| 和硕县| 兴义市| 兴安县| 商河县| 论坛| 抚州市| 山西省| 昂仁县| 漯河市| 和硕县| 南康市| 隆安县| 玉林市| 阿克| 高台县| 罗山县| 兴山县| 沈丘县| 炎陵县| 武鸣县| 南京市| 崇文区| 常宁市| 泰和县| 西藏| 会宁县| 韶关市| 安宁市| 星座| 名山县| 青海省| 青铜峡市| 都江堰市| 胶州市| 绩溪县| 噶尔县| 项城市| 正镶白旗| 黔江区| 朔州市| 九江市| 宜君县| 墨竹工卡县| 温州市| 衡阳县| 永善县| 渭南市| 古浪县| 安宁市| 普兰县| 偃师市| 利津县| 肇东市| 绥江县| 土默特左旗| 牟定县| 台安县| 宜阳县| 全州县| 阜新| 尼木县| 营山县| 天津市| 娱乐| 濮阳市| 苏尼特左旗| 贵溪市| 抚松县| 阳城县| 英德市| 东宁县| 五指山市| 霍邱县| 汨罗市| 盐边县| 西乌| 彰武县| 垣曲县| 吉木萨尔县| 江陵县| 松江区| 泽州县| 藁城市| 庆安县| 饶平县| 翼城县| 太白县| 锡林浩特市| 揭西县| 双牌县| 乌拉特后旗| 潼南县| 志丹县| 镇雄县| 兴文县| 桂阳县| 临安市| 正镶白旗| 拉萨市| 灵川县| 开平市| 铜陵市| 宁蒗| 嘉兴市| 日喀则市| 休宁县| 永福县| 岐山县| 遂昌县| 监利县| 清水县| 安平县| 喀喇| 武汉市| 京山县| 赤城县| 新巴尔虎右旗| 迁西县| 肥城市| 武威市| 潜江市| 南木林县| 平陆县| 靖江市| 南岸区| 双桥区| 揭西县| 郴州市| 新郑市| 津市市| 齐河县| 铅山县| 五大连池市| 珲春市| 唐海县| 东兴市| 阿拉善左旗| 龙里县| 延川县| 宜兰市| 阜平县| 平定县| 含山县| 建平县| 铁力市| 孝义市| 基隆市| 林周县| 湖南省| http://m.gz1980lockc.fun http://wap.yqo2j8rl9v.fun http://wap.gz1980willc.fun http://www.yqo3j5rl9v.fun http://m.jvz0j1r2o.fun http://www.bo2020characters.fun http://m.yqo1j4rl2v.fun http://www.gz1980flyc.fun http://wap.gz1980prograzc.fun http://wap.bo2020knows.fun http://m.yqo8j4rl1v.fun http://www.gz1980endc.fun http://wap.gz1980zoonc.fun http://wap.bo2020creazs.fun http://m.yqo9j1rl9v.fun