springboot+mybatis整合

springboot+mybatis整合

添加依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.0</version>
</dependency>

application.yml 配置文件中增加

mybatis:
    typeAliasesPackage: com.andy.framdemo.entity
    mapperLocations: classpath:mappers/*.xml

创建表t_user

create table t_user(
    id INT NOT NULL AUTO_INCREMENT,
    user_name VARCHAR(100) NOT NULL,
    password VARCHAR(100) NOT NULL,
    phone VARCHAR(20) NOT NULL,
    PRIMARY KEY ( tutorial_id )
);

userMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<mapper namespace="com.andy.framdemo.mapper.UserMapper" >
    <resultMap id="BaseResultMap" type="com.andy.framdemo.entity.User" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="user_name" property="userName" jdbcType="VARCHAR" />
        <result column="password" property="password" jdbcType="VARCHAR" />
        <result column="phone" property="phone" jdbcType="VARCHAR" />
    </resultMap>
    <sql id="Base_Column_List" >
        id, user_name, password, phone
    </sql>
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
        select
        <include refid="Base_Column_List" />
        from t_user
        where id = #{id,jdbcType=INTEGER}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
        delete from t_user
        where id = #{id,jdbcType=INTEGER}
    </delete>
    <insert id="insert" parameterType="com.andy.framdemo.entity.User" >
        insert into t_user (id, user_name, password,
        phone)
        values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
        #{phone,jdbcType=VARCHAR})
    </insert>
    <insert id="insertSelective" parameterType="com.andy.framdemo.entity.User" >
        insert into t_user
        <trim prefix="(" suffix=")" suffixOverrides="," >
            <if test="id != null" >
                id,
            </if>
            <if test="userName != null" >
                user_name,
            </if>
            <if test="password != null" >
                password,
            </if>
            <if test="phone != null" >
                phone,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
            <if test="id != null" >
                #{id,jdbcType=INTEGER},
            </if>
            <if test="userName != null" >
                #{userName,jdbcType=VARCHAR},
            </if>
            <if test="password != null" >
                #{password,jdbcType=VARCHAR},
            </if>
            <if test="phone != null" >
                #{phone,jdbcType=INTEGER},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.andy.framdemo.entity.User" >
        update t_user
        <set >
            <if test="userName != null" >
                user_name = #{userName,jdbcType=VARCHAR},
            </if>
            <if test="password != null" >
                password = #{password,jdbcType=VARCHAR},
            </if>
            <if test="phone != null" >
                phone = #{phone,jdbcType=INTEGER},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.andy.framdemo.entity.User" >
        update t_user
        set user_name = #{userName,jdbcType=VARCHAR},
        password = #{password,jdbcType=VARCHAR},
        phone = #{phone,jdbcType=VARCHAR}
        where id = #{id,jdbcType=INTEGER}
    </update>
</mapper>

创建user实体

import lombok.Data;

/**
* Created by Andy.W.Zhang on 2019/3/6.
*/
@Data
public class User {
    private Integer id;
    private String userName;
    private String password;
    private String phone;
}

UserMapper.java

import com.andy.framdemo.entity.User;

/**
* Created by Andy.W.Zhang on 2019/3/6.
*/
public interface UserMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}

application中增加mapper扫描目录

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

@SpringBootApplication
@MapperScan("com.andy.framdemo.mapper")
public class FramdemoApplication {

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

}

UserService.java

import com.andy.framdemo.entity.User;
import com.andy.framdemo.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* Created by Andy.W.Zhang on 2019/3/6.
*/
@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    public int addUser(User user){
        user.setUserName(user.getUserName());
        user.setPassword(user.getPassword());
        user.setPhone(user.getPhone());
        return userMapper.insert(user);
    }
}

UserController.java

import com.andy.framdemo.entity.User;
import com.andy.framdemo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* Created by Andy.W.Zhang on 2019/3/6.
*/
@Api(description = "用户接口")
@Controller
@RequestMapping(value = "/api/v1/users")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "新增用户" ,  notes="新增用户")
    @RequestMapping(value = "",method = RequestMethod.POST)
    @ResponseBody
    public int adduser(@RequestBody User user){
        return userService.addUser(user);
    }
}

结语

以上配置完成后就可以调用api实现crud。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值