• mybatis学习(19):模糊查询#


    目录结构

     

     

     

    com.geyao.mybatis.mapper

    BlogMapper类

    package com.geyao.mybatis.mapper;
     
    import java.util.List;
     
    import com.geyao.mybatis.pojo.Blog;
     
    public interface BlogMapper {
        Blog selectBlog(Integer id);
        
        Blog selectBlog2(Integer id);
        
        List<Blog> selectBlogByTitle(String title);
    }
    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,
            style
             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>
    </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 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;
        }
        @Override
        public String toString() {
            return "Blog [id=" + id + ", title=" + title + ", authodId=" + authodId + ", state=" + state + ", featured="
                    + featured + ", style=" + style + "]";
        }
        
        
        
        
    }
    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 block
                e.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.List;
     
    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 {
        @Test
        public 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);
    }
    }
    jar包

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

    运行结果


     

  • 相关阅读:
    动物静态HTML网页作业作品 大学生野生动物保护网页设计制作成品 简单DIV CSS布局网站
    window11安装JDK1.8【jdk-8u121】
    免费使用Salesforce Data Cloud!详细操作步骤来啦
    一般人用 Linux 算是找虐吗?
    MySQL 的存储引擎
    安装 Ubuntu 桌面服务与谷歌浏览器
    tomcat配置ssl证书
    Django REST Framework完整教程-RESTful规范-序列化和反序列数据-数据视图
    Vue组件(component)
    Electron-Vue创建项目及踩坑记录
  • 原文地址:https://blog.csdn.net/qq_41632427/article/details/125632959