idea 创建Springboot 项目使用MayBatis plus

1.配置pom.xml

<!--MybatisPlus依赖-->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.0.1</version>
		</dependency>

2.创建entity实体类 ,使用lombox 简化代码

@TableName()--->指定特定表名


import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

/**
 * 数据库实体,联系人
 */
@Data
@TableName("tuser")
public class TUser {
    /**
     * UID
     */
    private String uid;

    /**
     * 编码
     */
    private String unumber;

    /**
     * 用户名
     */
    private String uname;

    /**
     * 密码
     */
    private String upwd;
}

 

3.创建dao层 mapper类继承BaseMapper<T>

其中@Mapper --->注入到spring中



import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.bean.TUser;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface UserDao  extends BaseMapper<TUser> {

    void insertOne(TUser tuser);

    /**
     * 查询所有
     * @return
     */
    List<TUser> findAll();

}

3.创建Mapper.xml

这些方法都是Dao层 自定义的方法

<?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.example.demo.dao.UserDao">
    <resultMap id="BaseResultMap" type="com.example.demo.bean.TUser">
        <id column="uid" jdbcType="VARCHAR" property="uid" />
        <result column="unumber" jdbcType="VARCHAR" property="unumber" />
        <result column="uname" jdbcType="VARCHAR" property="uname" />
        <result column="upwd" jdbcType="VARCHAR" property="upwd" />
    </resultMap>
    <select id="findAll" resultType="com.example.demo.bean.TUser">
        select * from tuser
    </select>

    <insert id="insertOne"  parameterType="com.example.demo.bean.TUser" >
           INSERT INTO tuser (uid,uname)VALUES(#{uid},#{uname})
       </insert>
</mapper>

4.配置application.properties mybatis plus配置信息

如果没有如下设置,则在dao层自定义的方法不能被注入,只能执行BaseMapper的方法,也就是自己自定义的扩展方法用不了

#设置mapper文件的位置
mybatis-plus.mapper-locations: classpath*:com/example/demo/mapper/*.xml
mybatis-plus.type-aliases-package=cmo.example.demo.bean

5.创建service 层 调用dao层接口方法

package com.example.demo.service;

import com.example.demo.bean.TUser;
import com.example.demo.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Transactional
@Service
public class TUserService {
    @Autowired
    UserDao dao;

   /* public void saveOne(TUser user){
        dao.insert(user);

    }*/

    public void insertOne(TUser user){
        dao.insertOne(user);

    }

    public List<TUser> findAll(){
        List<TUser> all = dao.findAll();
        System.out.println("haha"+all.size());
        return all;

    }

}

6.创建Controler 测试 BaseMapper中的方法和自定义的方法

package com.example.demo.controller;

import com.example.demo.bean.TUser;
import com.example.demo.dao.UserDao;
import com.example.demo.service.TUserService;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//@Api(description = "测试接口")
@RestController
@RequestMapping(value = "/tuser")
public class SimpleTestController {

    @Autowired
    TUserService service;

    @Autowired
    UserDao dao;
   // @ApiOperation(value = "新增")
    @GetMapping("/add")
    @ResponseBody
    public String add() {
        System.out.println("添加一个联系人");
        TUser user = new TUser();
        user.setUid("3");
        user.setUname("张三");
        user.setUnumber("1");
        user.setUpwd("123");
//        service.insertOne(user);
//        System.out.println(1);
        List<TUser> tUsers = dao.selectList(null);
        System.out.println(tUsers.size());
      List<TUser> all = service.findAll();
        System.out.println(all.size());
        return user.getUname();
    }

}

最终运行效果展示

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值