目录结构

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);}

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><!-- crud --><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><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>
</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.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.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 = 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);
}
}

jar包

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

运行结果

mybatis学习(34):动态sql-choose相关推荐

  1. mybatis学习(39):动态sql片段

    目录结构 com.geyao.mybatis.mapper BlogMapper类 package com.geyao.mybatis.mapper;import java.util.List; im ...

  2. Mybatis学习之动态Sql

    目录 1. 什么是动态Sql 2. 动态Sql需要学习什么 3. 动态Sql之<if> 4. 动态Sql之<where> 5. 动态Sql之<foreach> 6. ...

  3. mybatis学习(33):动态sql if

    目录结构 com.geyao.mybatis.mapper BlogMapper类 package com.geyao.mybatis.mapper;import java.util.List; im ...

  4. Java - MyBatis中的动态SQL是什么意思?

    分享一个大牛的人工智能教程.零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net 对于一些复杂的查询,我们可能会指定多个查询条件,但是 ...

  5. Mybatis 注解开发 + 动态SQL

    Hello 大家好我是橙子同学,今天分享注解Mybatis注解开发+动态sql 目录 每文一铺垫(今天有小插曲哦) 注解开发 添加 @Insert 删除 @Delete 查询 @Select 修改 @ ...

  6. Mybatis入门之动态sql

    Mybatis入门之动态sql 通过mybatis提供的各种标签方法实现动态拼接sql. 1.if.where.sql.include标签(条件.sql片段) <sql id="sel ...

  7. Mybatis映射文件动态SQL语句-01

    因为在很多业务逻辑复杂的项目中,往往不是简单的sql语句就能查询出来自己想要的数据,所有mybatis引入了动态sql语句, UserMapper.xml <?xml version=" ...

  8. mybatis注解开发动态sql

    mybatis注解开发动态sql 本篇来讲一下如何使用mybatis注解模式中的动态sql 先来讲一下什么是动态sql 在我们实际开发的时候可能会出现很多方法需要一条很相似的sql语句来进行增删改查, ...

  9. 6.Mybatis中的动态Sql和Sql片段(Mybatis的一个核心)

    视频地址:http://edu.51cto.com/sd/be679 动态Sql是Mybatis的核心,就是对我们的sql语句进行灵活的操作,他可以通过表达式,对sql语句进行判断,然后对其进行灵活的 ...

最新文章

  1. centos下将vim配置为强大的源码阅读器
  2. 图的遍历递归和非递归实现
  3. 【转】维护工厂库存的一般设置
  4. 从功能、交互、性能全方位解读,BI工具FineBI4.0.2测评报告
  5. python截取指定字符串_python 正则匹配获取指定多个词的在字符串(句子/段落)索引位置...
  6. 数据结构(动态树):[国家集训队2012]tree(伍一鸣)
  7. socket、listen 等函数的打电话隐喻
  8. python在哪个方向岗位最多_Python就业方向这么多,哪些岗位最有发展?
  9. 交互式编程神器jupyter notebook环境搭建【不需要虚拟环境就能实现python2版本和python3版本自由切换】
  10. Dxg——Bat批处理 开发笔记整理分类合集【所有的相关记录,都整理在此】
  11. linux中执行命令权限不够怎样处理
  12. Java常用到的快捷键
  13. 梁刚:基于云原生技术建设“武汉健康云”云平台架构
  14. iNeRF:用于姿态估计的反向神经辐射场(IROS 2021)
  15. 那些年我不知道为啥的电脑“灵异事件”
  16. cccc2016决赛9
  17. 马尔科夫链细致平衡条件
  18. Window下,C++ 操作 Mysql、Url、utf-8文件 编码问题(读取和写入)
  19. JS两个日期之间计算时间差
  20. [Matlab]FIR滤波器设计:(基本窗函数FIR滤波器设计)

热门文章

  1. 58-混沌感悟.(2015.2.11)
  2. 爬取IMDBTOP250
  3. 14天学会安卓开发(附PDF文档和全部示例代码)
  4. mysql 设置事物自动提交_mysql事务自动提交的问题
  5. 两点定标法_一种两点校正红外热像仪的非均匀性的模块及方法
  6. php-fpm 内存 facebook,【百家号】脸书百科,安装php-fpm-5.4.16-42.遇到的小问题 Web程序 - 贪吃蛇学院-专业IT技术平台...
  7. 蓝叠 正在检查服务器最新,Windows update一直停留在正在检查更新
  8. php print div,JavaScrip实现PHP print_r的数功能(三种方法)
  9. 利用MySQL语句批量替换指定wordpress文章中的图片路径
  10. Linux C 预处理详解