首页 > 经验记录 > Mybatis注解配置

Mybatis注解配置

 
俩pojo类

package top.yibobo.pojo;
import java.sql.Date;
public class Article {
    private Integer id;
    private String title;
    private String content;
    private Date publishTime;
    private Author author;
    public Integer getId() {
        return id;
    }
    public void setId(Integer 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 Date getPublishTime() {
        return publishTime;
    }
    public void setPublishTime(Date publishTime) {
        this.publishTime = publishTime;
    }
    public Author getAuthor() {
        return author;
    }
    public void setAuthor(Author author) {
        this.author = author;
    }
    @Override
    public String toString() {
        return "Article{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", publishTime=" + publishTime +
                ", author=" + author +
                '}';
    }
}

 
 

package top.yibobo.pojo;
import java.sql.Date;
public class Author {
    private int id;
    private String name;
    private String sex;
    private Date birthday;
    private String address;
    private String phone;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    @Override
    public String toString() {
        return "Author{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", birthday=" + birthday +
                ", address='" + address + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}

 
 
俩pojo的接口
这个文章类写了两个查所有方法,其中一个是纯注解配置,还有一个是XML配置
 

package top.yibobo.dao;
import org.apache.ibatis.annotations.*;
import top.yibobo.pojo.Article;
import java.util.List;
public interface ArticleMapper {
    @Insert("insert into article values(null,#{title}," +
            "#{content},#{publishTime},#{author.id})")
    int addArticle(Article article);
    @Update("update article set title=#{title} where id=#{id}")
    int updateArticle(Article article);
    @Delete("DELETE FROM article WHERE id=#{arid}")
    int deleteArticle(@Param("arid") int arid);
    @Select("SELECT * FROM article WHERE id=#{arid}")
    Article findById(@Param("arid") int arid);
    @Results(value = {@Result(id = true,property = "id",column = "id"),//子查询嵌套
                        @Result(property = "title",column = "title"),
                        @Result (property = "content",column = "content"),
                        @Result(property = "publishTime",column = "publish_time"),
                        @Result(property = "author",column = "au_id",
                                javaType = top.yibobo.pojo.Author.class,
                                one = @One(select = "top.yibobo.dao.AuthorMapper.findById"))})
    @Select("SELECT * FROM article")
    List<Article> findAll();
    @ResultMap(value = "top.yibobo.dao.ArticleMapper.arList")
    @Select("SELECT ar.id arid,title,content,publish_time,au.id auid,name " +
            "FROM article ar LEFT JOIN author au ON ar.id=au.id")//表连接
    List<Article> findAll2();
}

 

package top.yibobo.dao;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import top.yibobo.pojo.Author;
public interface AuthorMapper {
    @Select("SELECT * FROM author WHERE id=#{auid}")
    Author findById(@Param("auid") int auid);
}

 
 
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">
<!--ArticleMapper接口的映射文件-->
<mapper namespace="top.yibobo.dao.ArticleMapper">
    <resultMap id="arList" type="top.yibobo.pojo.Article">
        <id property = "id" column = "arid"/>
        <result property = "title" column = "title"/>
        <result property = "content" column = "content"/>
        <result property = "publishTime" column = "publish_time"/>
        <association property="author" javaType="top.yibobo.pojo.Author">
            <id property="id" column="auid" />
            <result property="name" column="name"/>
        </association>
    </resultMap>
</mapper>

 
 

           


CAPTCHAis initialing...
EA PLAYER &

历史记录 [ 注意:部分数据仅限于当前浏览器 ]清空

      00:00/00:00