2021-07-30初识baomidou mybatis-plus

1.导入jar包

<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.3.1</version>
        </dependency>

2.applicationContext.xml

<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="typeAliasesPackage" value="com.pro.domain"/>
    </bean>

3.XxDao接口

public interface XxDao extends BaseMapper<Xx> {
//    public void insertXx(Xx xx);
//
//    public List<Xx> selectXx();
//
//    public Xx selectXxById(Integer id);
//
//    public void updateXx(Xx xx);

}

注释掉原来的代码,继承

XxDao.xml

<?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.pro.dao.XxDao">
    <!--  <insert id="insertXx" parameterType="xx" useGeneratedKeys="true" keyColumn="id" keyProperty="id">-->
    <!--      insert into xx(content) values (#{content})-->
    <!--  </insert>-->

    <!--    <select id="selectXx" resultType="xx">-->
    <!--        select*from xx-->
    <!--    </select>-->
    <!--    <select id="selectXxById" parameterType="int" resultType="xx">-->
    <!--        select * from xx where id=#{id}-->
    <!--    </select>-->
    <!--    <update id="updateXx" parameterType="com.pro.domain.Xx">-->
    <!--        update xx set content=#{content} where id=#{id}-->
    <!--    </update>-->
</mapper>

4.实体类

@TableName("xx")
public class Xx {
    //表的id是自动增长的
    @TableId(type = IdType.AUTO)
    @TableField("id")
    private Integer id;

    @TableField("content")
    private String content;

数据库和实体类的属性一一对应


5.删掉service和controller

6.测试类增删改查

package com.pro.dao;

import com.pro.domain.Xx;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;



@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class XxDaoTest {
    @Autowired
    private XxDao xxDao;

    @Test
    public void testInsert() {
        Xx xx = new Xx("wdq");
        xxDao.insert(xx);
    }
    @Test
    public void testRemove(){
        xxDao.deleteById(6);
    }
    @Test
    public void testModify(){
        Xx xx = xxDao.selectById(8);
        xx.setContent("hahhahahhah");
        xxDao.updateById(xx);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值