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

分享图片

相关文章
相关标签/搜索
每日一句
    每一个你不满意的现在,都有一个你没有努力的曾经。
公众号推荐
   一个历史类的公众号,欢迎关注
一两拨千金