20221020 Mybatis

mybatis

官方文档

官方文档:maven中文网

加入maven依赖

<dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>

xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/blog?serverTimezone=Asia/Shanghai"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
		<!--使用接口方式-->
<!--        <mapper class="com.etc.dao.NewsMapper"></mapper>-->
		<!--使用xml方式-->
        <mapper resource="com/etc/dao/NewsMapper.xml"/>
    </mappers>
</configuration>

实体类

package com.etc.entity;

public class News {
    private int id;
    private String title;
    private String content;
    private int typeid;

    public News() {
    }

    public News(int id, String title, String content, int typeid) {
        this.id = id;
        this.title = title;
        this.content = content;
        this.typeid = typeid;
    }

    @Override
    public String toString() {
        return "News{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", typeid=" + typeid +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getTypeid() {
        return typeid;
    }

    public void setTypeid(int typeid) {
        this.typeid = typeid;
    }
}

xml方式

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.etc.dao.NewsMapper">
    <select id="getNews" resultType="com.etc.entity.News">
        select * from news
    </select>
    <insert id="addNews" parameterType="com.etc.entity.News">
        insert into news values (null ,#{title},#{content},#{typeid})
    </insert>
    <delete id="deleteNews">
        delete from news where id=#{id}
    </delete>
    <update id="updateNews" parameterType="com.etc.entity.News">
        update news set title = #{title} , content = #{content} , typeid=#{typeid} where id=#{id}
    </update>
    <select id="getNewsPage" resultType="com.etc.entity.News">
        select * from news where title like #{keywords} or content like #{keywords} limit #{page} , #{limit}
    </select>
</mapper>
接口
package com.etc.dao;

import com.etc.entity.News;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface NewsMapper {

    public int addNews(News news);

    public int deleteNews(int id);

    public int updateNews(News news);

    public List<News> getNews();

    public List<News> getNewsPage(@Param(value = "keywords") String keywords ,@Param(value = "page") int page ,@Param(value = "limit") int limit);
}

注解方式

使用 @select(sql语句) 等这样的注解写在接口对应的方法前即可
具体可看注解表:https://mybatis.net.cn/java-api.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值