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

目录结构

 

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表所对应的实体类--><!-- 根据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 <include refid="columnbase"></include>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;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.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", 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);
}
@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+"记录");
}
}

jar包

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

运行结果

 

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/423766.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

中国古代十三美男

一、潘安    潘岳&#xff0c;就是人所周知的潘安&#xff0c;西晋时河南人氏&#xff0c;表字安仁&#xff0c;小字檀奴。其人“姿容既好&#xff0c;神情亦佳”。潘岳年轻时&#xff0c;坐车到洛阳城外游玩&#xff0c;当时不少妙龄姑娘见了他&#xff0c;都会怦然心动给他…

mybatis学习(40):逆向工程的创建

目录 首先导入我们的jar包 链接&#xff1a;https://pan.baidu.com/s/1Ent3kAwOagOZLT0XxDLEeA 提取码&#xff1a;zqpu 建立一个com.geyao.generator的包 generator的java类 package com.geyao.generator; import java.io.File; import java.util.*;import org.mybatis.ge…

SpringBoot中Tomcat配置(学习SpringBoot实战)

1、Tomcat配置 Spring Boot默认内嵌的Tomcat为Servlet容器&#xff0c;所以本节只讲对Tomcat配置&#xff0c;其实本节的配置对Tomcat、Jetty和Undertow都是通用的。 1.1 配置Tomcat 关于Tomcat的所有属性都在org.springframework.boot.autoconfigure.web.ServerProperties配…

axios的数据请求方式及跨域

express 的三大功能&#xff1a;静态资源、路由、模板引擎 app.use(express.static(www));  只要是创建这个静态的目录&#xff0c;这个 www 的静态目录里面的文件就可以被访问 数据的请求方式 axios get 的 请求方式    axios.get(url地址).then(function(success){  //…

mybatis学习(41):使用逆向工程

新建一个项目&#xff0c;将逆向工程的生成的拷贝进来 配置文件 log4j.properties ### \u914D\u7F6E\u6839 ### log4j.rootLogger debug,console ,fileAppender,dailyRollingFile,ROLLING_FILE,MAIL,DATABASE### \u8BBE\u7F6E\u8F93\u51FAsql\u7684\u7EA7\u522B\uFF0C\u5176…

[Alg] 二叉树的非递归遍历

1. 非递归遍历二叉树算法 (使用stack) 以非递归方式对二叉树进行遍历的算法需要借助一个栈来存放访问过得节点。 (1) 前序遍历 从整棵树的根节点开始&#xff0c;对于任意节点V&#xff0c;访问节点V并将节点V入栈&#xff0c;并判断节点V的左子节点L是否为空。若L不为空&#…

[c++]访MSN浮出窗口的示例

【声明】严格来讲&#xff0c;这篇文章不属于我的原创。我在这里参考了codeproject上的国外作者的模仿MSN浮出窗口的C#代码。换句话说&#xff0c;可以认为我把C#代码翻译成了C代码。另外&#xff0c;为了简化代码&#xff0c;CloseButton我没有采用自己绘制&#xff0c;而是用…

mybatis学习(42):mybatis的一级缓存

目录结构 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…

防止ASP.NET按钮多次提交的办法

方法一<asp:Button ID"btnSumbit" runat"server" UseSubmitBehavior"false" OnClientClick"this.valueSumbit;this.disabledtrue; " Text"Sumbit" OnClick"btnSumbit_Click" /> 方法二1<html xmlns"…

mybatis学习(43):一级缓存被刷新情况

目录结构 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…

python文件读写小结

读文件 打开一个文件用open()方法(open()返回一个文件对象&#xff0c;它是可迭代的)&#xff1a; >>> f open(test.txt, r) r表示是文本文件&#xff0c;rb是二进制文件。&#xff08;这个mode参数默认值就是r&#xff09; 如果文件不存在&#xff0c;open()函数就会…

mybatis学习(44):二级缓存1

目录结构 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…

HDU6089 恐怖分子(变形线段树)

题目描述 n*m的平面内有K个不安全点&#xff0c;Q个询问位置在(x,y)的人能走到多少个点&#xff1f;从(x,y)走到(x,y)是合法的&#xff0c;当且仅当(x,y)和(x,y)之间的矩形中不包含不安全点。 题解 问题相当于平面中有若干障碍点&#xff0c;询问以某一个点为四个角之一的不包含…

mybatis学习(45):开启二级缓存

目录结构 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…

【读书笔记】重要的东西

以上摘自一本叫做《大道至简》的小册子&#xff0c;有关介绍可以参考下面的链接 http://www.china-pub.com/34356转载于:https://www.cnblogs.com/chenxizhang/archive/2008/08/11/1264917.html

七夕秀恩爱新姿势!这波操作我给十分!

一、前言 像每一滴酒回不了最初的葡萄&#xff0c;我回不到年少。爱情亦是如此&#xff0c;这就是写一篇小程序的初衷&#xff0c;用来记录我和她最美的恋爱。什么是最美恋爱&#xff1f;就是繁忙之余的一封书信&#xff0c;一起奋斗的目标&#xff0c;精彩的瞬间&#xff0c;旅…

mybatis学习(46):二级缓存被刷新的情况

目录结构 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…

python 主函数 输入

python 主函数 输入 看一段主函数调用函数的小程序&#xff1a; 1 import sys2 def Fuc():3 print hello4 5 if __name__ __main__:6 if len(sys.argv) ! 3:7 print Usage: python input_name output_name8 exit(1)9 f_input sys.argv[1] 10…

mybatis学习(47):嵌套查询--一对一

数据库 目录结构 映入jar包和junit单元测试 com.geyao.mybatis.mapper AuthorMapper类 package com.geyao.mybatis.mapper;import com.geyao.mybatis.pojo.Author; public interface AuthorMapper {Author selectAuthorById(Integer id); } BlogMapper类 package com.gey…

element-ui select

1. 组合 label <!DOCTYPE html> <html><head><meta charset"UTF-8"><!-- import CSS --><link rel"stylesheet" href"https://unpkg.com/element-ui/lib/theme-chalk/index.css"> </head><body>…