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