目录结构

com.geyao.mybatis.mapper

BlogMapper类

package com.geyao.mybatis.mapper;import java.util.List;
import java.util.Map;import org.apache.ibatis.annotations.Param;import com.geyao.mybatis.pojo.Blog;public interface BlogMapper {Blog selectBlog(Integer id);Blog selectBlog2(Integer id);List<Blog> selectBlogByTitle(String title);List<Blog> selectBlogByTitle2(String title);List<Blog> selectBlogBySort(String column);List<Blog> selectBlogByPage(int offset,int pagesize);List<Blog> selectBlogByPage1(@Param(value="offset")int offset,@Param(value="pagesize")int pagesize);List<Blog> selectBlogByPage2(Map<String, Object>map);int insertBlog(Blog blog);int insertBlogMysql(Blog blog);int updateBlog(Blog blog);int deleteBlogById(Integer id);List<Blog> selectActiveBlogByTitle(String title);List<Blog> selectActiveBlogByTitleOrStyle(Blog blog);List<Blog> selectBlogByCondition(Blog blog);int updateBlogByCondition(Blog blog);List<Blog> selectBlogByConditionTrim(Blog blog);int updateBlogByConditionTrim(Blog blog);int deleteBlogList(List<Integer> ids);
}

BlogMapper.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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀)-->
<mapper namespace="com.geyao.mybatis.mapper.BlogMapper"><!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型resultType="me.gacl.domain.User"就表示将查询结果封装成一个User类的对象返回User类就是users表所对应的实体类--><cache/><!-- 根据id查询得到一个user对象--><resultMap type="Blog" id="blogResultMap"><id column="id" property="id" jdbcType="INTEGER"></id><result column="authod_id" property="authodId" jdbcType="INTEGER"/></resultMap><!-- sql片段 --><sql id="columnbase">select id,title,authod_id as authodId,state,featured,style</sql><!-- crud --><select id="selectBlog" parameterType="int"   resultType="Blog">select *from Blog where id=#{id}</select> <select id="selectBlog2" parameterType="int"  resultMap="blogResultMap">select *from Blog where id=#{id}</select><select id="selectBlogByTitle" parameterType="String" resultMap="blogResultMap">select * from Blog where title like #{title}</select><select id="selectBlogByTitle2" parameterType="String" resultMap="blogResultMap">select * from Blog where title like '${value}'</select><select id="selectBlogBySort" parameterType="String" resultMap="blogResultMap">select * from Blog order by ${value}</select><select id="selectBlogByPage"  resultMap="blogResultMap">select * from Blog limit #{0},#{1}</select><select id="selectBlogByPage1"  resultMap="blogResultMap">select * from Blog limit #{offset},#{pagesize}</select><select id="selectBlogByPage2"  resultMap="blogResultMap">select * from Blog limit #{offset},#{pagesize}</select><insert id="insertBlog" parameterType="Blog" useGeneratedKeys="true" keyProperty="id">INSERT INTO Blog(title,authod_id,state,featured,style)VALUES(#{title},#{authodId},#{state},#{featured},#{style})</insert><insert id="insertBlogOracle" parameterType="Blog" ><selectKey resultType="java.lang.Integer" order="BEFORE" keyProperty="id">select seq.nextval as id from dual</selectKey>INSERT INTO Blog(title,authod_id,state,featured,style)VALUES(#{title},#{authodId},#{state},#{featured},#{style})</insert><insert id="insertBlogMysql" parameterType="Blog" ><selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id">SELECT LAST_INSERT_ID()</selectKey>INSERT INTO Blog(title,authod_id,state,featured,style)VALUES(#{title},#{authodId},#{state},#{featured},#{style})</insert><update id="updateBlog" parameterType="Blog" >UPDATE BlogSETtitle = #{title},authod_id = #{authodId},state = #{state},featured = #{featured},
style= #{ style}WHERE id = #{id}</update><delete id="deleteBlogById" parameterType="int">delete from blog where id =#{id}</delete><!-- 动态sql --><select id="selectActiveBlogByTitle" parameterType="String" resultMap="blogResultMap">SELECT * FROM blogwhere state ='ACTIVE'<if test="value!=null and value !=''">AND title LIKE '%o%'</if></select><select id="selectActiveBlogByTitleOrStyle" parameterType="Blog" resultMap="blogResultMap">SELECT * FROM blogwhere state ='ACTIVE'<choose><when test="title !=null and title !=''">and lower(title) like lower(#{title})</when><when test="style !=null and style!=''"></when><otherwise>and featured=true;</otherwise></choose></select><select id="selectBlogByCondition" parameterType="Blog" resultMap="blogResultMap">select * from blog<where><if test="state !=null and state !=''">state=#{state}</if><if test="title !=null and title !=''">and lower(title) like lower(#{title})</if><if test=" featured !=null">and featured = #{featured}</if></where></select><update id="updateBlogByCondition" parameterType="Blog" >UPDATE Blog<set><if test="title!=null">title = #{title},</if><if test="authodId!=null">authod_id = #{authodId},</if><if test="state!=null">state = #{state},</if><if test="featured!=null">featured = #{featured},</if>
<if test="style!=null">style= #{ style}</if>
</set>WHERE id = #{id}</update><select id="selectBlogByConditionTrim" parameterType="Blog" resultMap="blogResultMap">select * from blog<trim prefix="where" prefixOverrides="and / or"><if test="state !=null and state !=''">state=#{state}</if><if test="title !=null and title !=''">lower(title) like lower(#{title})</if><if test=" featured !=null">and featured = #{featured}</if></trim></select><update id="updateBlogByConditionTrim" parameterType="Blog" >UPDATE Blog<trim prefix="set" suffixOverrides=","><if test="title!=null">title = #{title},</if><if test="authodId!=null">authod_id = #{authodId},</if><if test="state!=null">state = #{state},</if><if test="featured!=null">featured = #{featured},</if>
<if test="style!=null">style= #{ style}</if>
</trim>WHERE id = #{id}</update><delete id="deleteBlogList" parameterType="list" >delete from Blog where id in<foreach collection="list" item="item" open="(" close= ")" separator=",">#{item}</foreach></delete></mapper>

com.geyao.mybatis.pojo

Blog类

package com.geyao.mybatis.pojo;import java.io.Serializable;public class Blog implements Serializable {/*** */private static final long serialVersionUID = 1L;private Integer id;private String title;private int authodId;private String state;private Boolean featured;private String style;public Blog() {super();//this.title="未命名";//this.authodId=4;//this.state="NOT";//this.featured=false;//this.style="red";    }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 int getAuthodId() {return authodId;}public void setAuthodId(int authodId) {this.authodId = authodId;}public String getState() {return state;}public void setState(String state) {this.state = state;}public Boolean getFeatured() {return featured;}public void setFeatured(Boolean featured) {this.featured = featured;}public String getStyle() {return style;}public void setStyle(String style) {this.style = style;}@Overridepublic String toString() {return "Blog [id=" + id + ", title=" + title + ", authodId=" + authodId + ", state=" + state + ", featured="+ featured + ", style=" + style + "]\n";}}

com.geyao.mybatis.util

MybatisUtil类

package com.geyao.mybatis.util;import java.io.InputStream;
import java.io.Reader;import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class MyBatisUtil {private static SqlSessionFactory sqlSessionFactory =null;static {try {InputStream in = Resources.getResourceAsStream("mybatis-config.xml");sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}private MyBatisUtil() {}public static SqlSession getSqlSession() {return sqlSessionFactory.openSession();}
}

log4j.properties

### \u914D\u7F6E\u6839 ###
log4j.rootLogger = debug,console ,fileAppender,dailyRollingFile,ROLLING_FILE,MAIL,DATABASE### \u8BBE\u7F6E\u8F93\u51FAsql\u7684\u7EA7\u522B\uFF0C\u5176\u4E2Dlogger\u540E\u9762\u7684\u5185\u5BB9\u5168\u90E8\u4E3Ajar\u5305\u4E2D\u6240\u5305\u542B\u7684\u5305\u540D ###
log4j.logger.org.apache=dubug
log4j.logger.java.sql.Connection=dubug
log4j.logger.java.sql.Statement=dubug
log4j.logger.java.sql.PreparedStatement=dubug
log4j.logger.java.sql.ResultSet=dubug### \u914D\u7F6E\u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern =  %d{ABSOLUTE} %5p %c{1}:%L - %m%n

mybatis-config.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><typeAliases><typeAlias type="com.geyao.mybatis.pojo.Blog" alias="Blog"/>
</typeAliases><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/mybatis?serverTimezone=GMT%2B8" /><property name="username" value="root" /><property name="password" value="123" /></dataSource></environment></environments><mappers><!-- 注册userMapper.xml文件, userMapper.xml位于me.gacl.mapping这个包下,所以resource写成me/gacl/mapping/userMapper.xml--><mapper resource="com/geyao/mybatis/mapper/BlogMapper.xml"/></mappers>
</configuration>

单元测试

com.geyao.mybatis.util

testSelectBlog类

package com.geyao.mybatis.mapper;import java.util.HashMap;
import java.util.List;
import java.util.Map;import org.apache.ibatis.session.SqlSession;
import org.junit.Test;import com.geyao.mybatis.pojo.Blog;
import com.geyao.mybatis.util.MyBatisUtil;public class testSelectBlog {@Testpublic void testSelectBlogNoInterface() {SqlSession session =MyBatisUtil.getSqlSession();Blog blog =(Blog)session.selectOne("com.geyao.mybatis.mapper.BlogMapper.selectBlog", 101);session.close();System.out.println(blog);}
@Test
public void testSelectBlog() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog = blogMapper.selectBlog(1);System.out.println(blog);
}@Test
public void testSelectBlog2() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog = blogMapper.selectBlog2(1);System.out.println(blog);
}
@Test
public void testselectBlogByTitle() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);List<Blog> blogList = blogMapper.selectBlogByTitle("%g%");System.out.println(blogList);
}@Test
public void testselectBlogByTitle2() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);List<Blog> blogList = blogMapper.selectBlogByTitle2("%g%");System.out.println(blogList);
}
@Test
public void testselectBlogBySort() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);List<Blog> blogList = blogMapper.selectBlogBySort("title");System.out.println(blogList);
}
@Test
public void testselectBlogByPage() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);List<Blog> blogList = blogMapper.selectBlogByPage(2,2);System.out.println(blogList);
}
@Test
public void testselectBlogByPage1() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);List<Blog> blogList = blogMapper.selectBlogByPage1(2,2);System.out.println(blogList);
}
@Test
public void testselectBlogByPage2() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Map<String, Object> map =new HashMap<String, Object>();map.put("offset", 2);map.put("pagesize", 2);List<Blog> blogList = blogMapper.selectBlogByPage2(map);System.out.println(blogList);
}@Test
public void testInsertBlog() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog=new Blog();int count= blogMapper.insertBlog(blog);session.commit();session.close();System.out.println(blog);System.out.println("插入的"+count+"记录");
}@Test
public void testinsertBlogMysql() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog=new Blog();int count= blogMapper.insertBlogMysql(blog);session.commit();session.close();System.out.println(blog);System.out.println("插入的"+count+"记录");
}@Test
public void testupdateBlog() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog=new Blog();blog.setId(1);blog.setTitle("geyao");blog.setStyle("balck");blog.setState("active");blog.setFeatured(false);blog.setAuthodId(2);int count= blogMapper.updateBlog(blog);session.commit();session.close();System.out.println(blog);System.out.println("修改"+count+"记录");
}
}

jar包

链接:https://pan.baidu.com/s/1g6NgzfLc5uK9S4VL-03lHg
提取码:4r2m

运行结果

目录结构

com.geyao.mybatis.mapper

BlogMapper类

package com.geyao.mybatis.mapper;import java.util.List;
import java.util.Map;import org.apache.ibatis.annotations.Param;import com.geyao.mybatis.pojo.Blog;public interface BlogMapper {Blog selectBlog(Integer id);Blog selectBlog2(Integer id);List<Blog> selectBlogByTitle(String title);List<Blog> selectBlogByTitle2(String title);List<Blog> selectBlogBySort(String column);List<Blog> selectBlogByPage(int offset,int pagesize);List<Blog> selectBlogByPage1(@Param(value="offset")int offset,@Param(value="pagesize")int pagesize);List<Blog> selectBlogByPage2(Map<String, Object>map);int insertBlog(Blog blog);int insertBlogMysql(Blog blog);int updateBlog(Blog blog);
}

BlogMapper.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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀)-->
<mapper namespace="com.geyao.mybatis.mapper.BlogMapper"><!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型resultType="me.gacl.domain.User"就表示将查询结果封装成一个User类的对象返回User类就是users表所对应的实体类--><!-- 根据id查询得到一个user对象--><resultMap type="Blog" id="blogResultMap"><id column="id" property="id" jdbcType="INTEGER"></id><result column="authod_id" property="authodId" jdbcType="INTEGER"/></resultMap><select id="selectBlog" parameterType="int"   resultType="Blog">select id,title,authod_id as authodId,state,featured,stylefrom Blog where id=#{id}</select> <select id="selectBlog2" parameterType="int"  resultMap="blogResultMap">select *from Blog where id=#{id}</select><select id="selectBlogByTitle" parameterType="String" resultMap="blogResultMap">select * from Blog where title like #{title}</select><select id="selectBlogByTitle2" parameterType="String" resultMap="blogResultMap">select * from Blog where title like '${value}'</select><select id="selectBlogBySort" parameterType="String" resultMap="blogResultMap">select * from Blog order by ${value}</select><select id="selectBlogByPage"  resultMap="blogResultMap">select * from Blog limit #{0},#{1}</select><select id="selectBlogByPage1"  resultMap="blogResultMap">select * from Blog limit #{offset},#{pagesize}</select><select id="selectBlogByPage2"  resultMap="blogResultMap">select * from Blog limit #{offset},#{pagesize}</select><insert id="insertBlog" parameterType="Blog" useGeneratedKeys="true" keyProperty="id">INSERT INTO Blog(title,authod_id,state,featured,style)VALUES(#{title},#{authodId},#{state},#{featured},#{style})</insert><insert id="insertBlogOracle" parameterType="Blog" ><selectKey resultType="java.lang.Integer" order="BEFORE" keyProperty="id">select seq.nextval as id from dual</selectKey>INSERT INTO Blog(title,authod_id,state,featured,style)VALUES(#{title},#{authodId},#{state},#{featured},#{style})</insert><insert id="insertBlogMysql" parameterType="Blog" ><selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id">SELECT LAST_INSERT_ID()</selectKey>INSERT INTO Blog(title,authod_id,state,featured,style)VALUES(#{title},#{authodId},#{state},#{featured},#{style})</insert><update id="updateBlog" parameterType="Blog" >UPDATE BlogSETtitle = #{title},authod_id = #{authodId},state = #{state},featured = #{featured},
style= #{ style}WHERE id = #{id}</update>
</mapper>

com.geyao.mybatis.pojo

Blog类

package com.geyao.mybatis.pojo;public class Blog {private Integer id;private String title;private int authodId;private String state;private Boolean featured;private String style;public Blog() {super();this.title="未命名";this.authodId=4;this.state="NOT";this.featured=false;this.style="red";  }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 int getAuthodId() {return authodId;}public void setAuthodId(int authodId) {this.authodId = authodId;}public String getState() {return state;}public void setState(String state) {this.state = state;}public Boolean getFeatured() {return featured;}public void setFeatured(Boolean featured) {this.featured = featured;}public String getStyle() {return style;}public void setStyle(String style) {this.style = style;}@Overridepublic String toString() {return "Blog [id=" + id + ", title=" + title + ", authodId=" + authodId + ", state=" + state + ", featured="+ featured + ", style=" + style + "]\n";}}

com.geyao.mybatis.util

MybatisUtil类

package com.geyao.mybatis.util;import java.io.InputStream;
import java.io.Reader;import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class MyBatisUtil {private static SqlSessionFactory sqlSessionFactory =null;static {try {InputStream in = Resources.getResourceAsStream("mybatis-config.xml");sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}private MyBatisUtil() {}public static SqlSession getSqlSession() {return sqlSessionFactory.openSession();}
}

log4j.properties

### \u914D\u7F6E\u6839 ###
log4j.rootLogger = debug,console ,fileAppender,dailyRollingFile,ROLLING_FILE,MAIL,DATABASE### \u8BBE\u7F6E\u8F93\u51FAsql\u7684\u7EA7\u522B\uFF0C\u5176\u4E2Dlogger\u540E\u9762\u7684\u5185\u5BB9\u5168\u90E8\u4E3Ajar\u5305\u4E2D\u6240\u5305\u542B\u7684\u5305\u540D ###
log4j.logger.org.apache=dubug
log4j.logger.java.sql.Connection=dubug
log4j.logger.java.sql.Statement=dubug
log4j.logger.java.sql.PreparedStatement=dubug
log4j.logger.java.sql.ResultSet=dubug### \u914D\u7F6E\u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern =  %d{ABSOLUTE} %5p %c{1}:%L - %m%n

mybatis-config.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><typeAliases><typeAlias type="com.geyao.mybatis.pojo.Blog" alias="Blog"/>
</typeAliases><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/mybatis?serverTimezone=GMT%2B8" /><property name="username" value="root" /><property name="password" value="123" /></dataSource></environment></environments><mappers><!-- 注册userMapper.xml文件, userMapper.xml位于me.gacl.mapping这个包下,所以resource写成me/gacl/mapping/userMapper.xml--><mapper resource="com/geyao/mybatis/mapper/BlogMapper.xml"/></mappers>
</configuration>

单元测试

com.geyao.mybatis.util

testSelectBlog类

package com.geyao.mybatis.mapper;import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import org.apache.ibatis.session.SqlSession;
import org.junit.Test;import com.geyao.mybatis.pojo.Blog;
import com.geyao.mybatis.util.MyBatisUtil;public class testSelectBlog {@Testpublic void testSelectBlogNoInterface() {SqlSession session =MyBatisUtil.getSqlSession();Blog blog =(Blog)session.selectOne("com.geyao.mybatis.mapper.BlogMapper.selectBlog", 1);session.close();System.out.println(blog);}
@Test
public void testSelectBlog() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog = blogMapper.selectBlog(1);System.out.println(blog);
}@Test
public void testSelectBlog2() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog = blogMapper.selectBlog2(1);System.out.println(blog);
}
@Test
public void testselectBlogByTitle() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);List<Blog> blogList = blogMapper.selectBlogByTitle("%g%");System.out.println(blogList);
}@Test
public void testselectBlogByTitle2() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);List<Blog> blogList = blogMapper.selectBlogByTitle2("%g%");System.out.println(blogList);
}
@Test
public void testselectBlogBySort() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);List<Blog> blogList = blogMapper.selectBlogBySort("title");System.out.println(blogList);
}
@Test
public void testselectBlogByPage() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);List<Blog> blogList = blogMapper.selectBlogByPage(2,2);System.out.println(blogList);
}
@Test
public void testselectBlogByPage1() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);List<Blog> blogList = blogMapper.selectBlogByPage1(2,2);System.out.println(blogList);
}
@Test
public void testselectBlogByPage2() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Map<String, Object> map =new HashMap<String, Object>();map.put("offset", 2);map.put("pagesize", 2);List<Blog> blogList = blogMapper.selectBlogByPage2(map);System.out.println(blogList);
}@Test
public void testInsertBlog() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog=new Blog();int count= blogMapper.insertBlog(blog);session.commit();session.close();System.out.println(blog);System.out.println("插入的"+count+"记录");
}@Test
public void testinsertBlogMysql() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog=new Blog();int count= blogMapper.insertBlogMysql(blog);session.commit();session.close();System.out.println(blog);System.out.println("插入的"+count+"记录");
}@Test
public void testupdateBlog() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog = blogMapper.selectBlog(1);//Blog blog=new Blog();blog.setId(1);blog.setTitle("geyao");blog.setAuthodId(3);//blog.setStyle("balck");//blog.setState("active");//blog.setFeatured(false);//blog.setAuthodId(2);int count= blogMapper.updateBlog(blog);session.commit();session.close();System.out.println(blog);System.out.println("修改"+count+"记录");
}
@Test
public void testdeleteBlogById() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);int count= blogMapper.deleteBlogById(3);session.commit();session.close();System.out.println("删除的"+count+"记录");
}
@Test
public void testselectActiveBlogByTitle() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);List<Blog> blogList=blogMapper.selectActiveBlogByTitle("o");session.close();System.out.println(blogList);
}
@Test
public void testselectActiveBlogByTitleOrStyle() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog = new Blog();blog.setTitle("%o%");blog.setStyle("black");List<Blog> blogList=blogMapper.selectActiveBlogByTitleOrStyle(blog);session.close();System.out.println(blogList);
}
@Test
public void testselectBlogByCondition() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog = new Blog();blog.setTitle("%o%");List<Blog> blogList=blogMapper.selectBlogByCondition(blog);session.close();System.out.println(blogList);
}
@Test
public void tesupdateBlogByCondition() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog = blogMapper.selectBlog(1);//Blog blog=new Blog();blog.setId(1);blog.setTitle("geyaonice");blog.setAuthodId(3);//blog.setStyle("balck");//blog.setState("active");//blog.setFeatured(false);//blog.setAuthodId(2);int count= blogMapper.updateBlogByCondition(blog);session.commit();session.close();System.out.println(blog);System.out.println("修改"+count+"记录");
}
@Test
public void testselectBlogByConditionTrim() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog = new Blog();blog.setTitle("%o%");List<Blog> blogList=blogMapper.selectBlogByConditionTrim(blog);session.close();System.out.println(blogList);
}
@Test
public void tesupdateBlogByConditionTrim() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog = blogMapper.selectBlog(1);//Blog blog=new Blog();blog.setId(1);blog.setTitle("geyaonice");blog.setAuthodId(3);//blog.setStyle("balck");//blog.setState("active");//blog.setFeatured(false);//blog.setAuthodId(2);int count= blogMapper.updateBlogByConditionTrim(blog);session.commit();session.close();System.out.println(blog);System.out.println("修改"+count+"记录");
}
@Test
public void testdeleteBlogList() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);List<Integer> ids=Arrays.asList(2,3,4);int count= blogMapper.deleteBlogList(ids);session.commit();session.close();System.out.println("删除"+count+"记录");
}
//--------一级缓存
@Test
public void testSelectBlogCacheLevelOne1() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog1=blogMapper.selectBlog(1);System.out.println("结果已查询");Blog blog2=blogMapper.selectBlog(2);System.out.println("结果已查询");session.close();System.out.println("session关闭");}
@Test
public void testSelectBlogCacheLevelOne2() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog1=blogMapper.selectBlog(1);System.out.println("结果已查询");blog1.setFeatured(true);blogMapper.updateBlog(blog1);System.out.println("刷星缓存");Blog blog2=blogMapper.selectBlog(2);System.out.println("重新执行查询");session.close();System.out.println("session关闭");}
//二级缓存------
@Test
public void testSelectBlogCacheLevelTwo1() {SqlSession session1 =MyBatisUtil.getSqlSession();BlogMapper blogMapper1 =session1.getMapper(BlogMapper.class);Blog blog1=blogMapper1.selectBlog(1);System.out.println("结果已查询");session1.close();SqlSession session2 =MyBatisUtil.getSqlSession();BlogMapper blogMapper2 =session2.getMapper(BlogMapper.class);Blog blog2=blogMapper2.selectBlog(1);System.out.println("结果已查询");session2.close();System.out.println("session关闭");}
}

jar包

链接:https://pan.baidu.com/s/1g6NgzfLc5uK9S4VL-03lHg
提取码:4r2m

运行结果

mybatis学习(45):开启二级缓存相关推荐

  1. mybatis开启二级缓存和懒加载,类型别名,类都简称

    SqlMapConfig.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE conf ...

  2. Springboot 集成 mybatis 开启二级缓存(redis)

    首先来了解下mybatis 缓存,mybatis缓存分为一级缓存和二级缓存.一级缓存是默认开启的,无需其他配置操作,二级缓存则需要手动设置开启. 一级缓存原理: Mybatis的一级缓存是指同一个Sq ...

  3. MyBatis 缓存详解-什么时候开启二级缓存?

    一级缓存默认是打开的,二级缓存需要配置才可以开启.那么我们必须思考一个问题,在什么情况下才有必要去开启二级缓存? 1.因为所有的增删改都会刷新二级缓存,导致二级缓存失效,所以适合在查询为主的应用中使用 ...

  4. 深入浅出 MyBatis 的一级、二级缓存机制

    一.MyBatis 缓存 缓存就是内存中的数据,常常来自对数据库查询结果的保存.使用缓存,我们可以避免频繁与数据库进行交互,从而提高响应速度. MyBatis 也提供了对缓存的支持,分为一级缓存和二级 ...

  5. MybatisPlus开启二级缓存

    文章目录 前言 开启二级缓存 前言 本篇博客是讲解如何开启MyBbatisPlus的二级缓存教程,若文章中出现相关问题,请指出! 所有博客文件目录索引:博客目录索引(持续更新) 开启二级缓存 1.ma ...

  6. mybatis注解开发使用二级缓存

    在SqlMapConfig中开启二级缓存支持 <!--配置开启二级缓存--><settings><setting name="cacheEnabled" ...

  7. tkmybatis开启二级缓存

    1.MyBatis配置文件开启二级缓存功能 <settings> <settingname="cacheEnabled"value="true" ...

  8. Mybatis的一、二级缓存的原理与使用、禁止指定方法的二级缓存与刷新缓存、Mybatis整合Ehcache、二级缓存的使用场景与局限性-day03

    目录 第一节 Mybatis的缓存 1.1 Mybatis的缓存理解 1.2 一级缓存 原理 使用与测试 1.3 二级缓存 原理 使用与测试 禁用指定方法的二级缓存 刷新缓存 总结 1.4 整合ehc ...

  9. hibernate开启二级缓存

    一.在hibernate.cfg.xml中加入: <!-- 开启二级缓存 --> <property name="hibernate.cache.use_query_cac ...

最新文章

  1. iPhone销量低迷,或导致苹果放弃自动驾驶项目?
  2. 一个用python做的完整项目_我从一个小项目学习Python编程的全过程(二)
  3. C语言入门题-7-1 最大和最小 (10分)
  4. sicily 1068. Euro Efficiency
  5. payara 创建 集群_Apache Payara:让我们加密
  6. LeetCode Range Sum Query Immutable
  7. 03JavaScript程序设计修炼之道-2019-06-20_20-31-49
  8. 深度学习之 soft-NMS
  9. 语义分割和实例分割_一文读懂语义分割与实例分割
  10. (Oracle、SqlServer、Access)数据库开发代码生成工具SharpCode2.0
  11. [codeup 2143] 迷瘴
  12. 预测模型(数学建模)
  13. 计算机一级插入页眉,计算机一级考试,设置页眉为“汉字的交换码”
  14. matlab的默认复数开方
  15. Element组件--Upload文件/图片上传
  16. 通信工程专业就业怎么样?难不难学?
  17. 程序人生 | 春风得意马蹄疾,一日看尽长安花
  18. 《增强现实:融合现实与虚拟世界》
  19. CODE RO RW ZI
  20. 科技周刊第五期:科学技术在发展中的作用

热门文章

  1. java servlet+oracle 新手可看
  2. 你会给别人提反馈吗?
  3. Page_Load的问题
  4. 编程软件python是什么意思_程序员Python编程必备5大工具,你用过几个?
  5. 你知道“拉黑”、“关注”、“点赞”、“转发”、“分享到朋友圈”等英语咋说吗?
  6. Linux常用错误码--errno-base.h
  7. 飞凌开发板 cramfs 镜像文件修改
  8. Linux下检测网络状态是否正常
  9. 汇编常用命令、指令一览
  10. ant设置国际化设置为中文