栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

第七个MyBatis练习,(动态sql的实现,使用 <if> ,<where>,<foreach>标签,分页,数据库连接信息分离)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

第七个MyBatis练习,(动态sql的实现,使用 <if> ,<where>,<foreach>标签,分页,数据库连接信息分离)

项目结构

1.创建Student实体类

package com.it.domain;

public class Student {
    private int sno;
    private String sname;
    private  String ssex;
    private int sage;
    private String sdept;

    public int getSno() {
        return sno;
    }

    public void setSno(int sno) {
        this.sno = sno;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSsex() {
        return ssex;
    }

    public void setSsex(String ssex) {
        this.ssex = ssex;
    }

    public int getSage() {
        return sage;
    }

    public void setSage(int sage) {
        this.sage = sage;
    }

    public String getSdept() {
        return sdept;
    }

    public void setSdept(String sdept) {
        this.sdept = sdept;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sno=" + sno +
                ", sname='" + sname + ''' +
                ", ssex='" + ssex + ''' +
                ", sage=" + sage +
                ", sdept='" + sdept + ''' +
                '}';
    }
}

2.创建StudentDao接口类

package com.it.dao;

import com.it.domain.Student;

import java.util.List;

public interface StudentDao {
    //动态sql要使用java对象作为参数

    //if标签的使用
List selectStudentIf(Student student);
//where标签的使用
List selectStudentWhere(Student student);
//foreach标签的使用,参数是整形的集合
List selectForeachOne(List idList);
//foreach标签的使用,参数是整形的集合
List selectForeachTwo(List studentList);
//测试分页
List selectAll();

}

3.创建StudentDao接口类的映射文件StudentDao.xml








    
        select sno,sname,sage,ssex,sdept from student
    





    where 1=1
    
        and sname=#{sname}
    
    
        and sage>#{sage}
    



    


    
        SELECT * FROM student where sno in
        
            #{i.sno}
        
    


    






4.创建MyBatis.xml文件






    

    
        
    

    
        
    

    
        
            
            
                
                
                
                
            
        
    
    



        
    

5.创建MyBatisUntils工具类

package com.it.untils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MyBatisUntils {
    private static SqlSessionFactory factory = null;
//    写在静态代码块中,可以避免SqlSessionFactory对象重复创建,该对象创建耗时较长,
//    且创建一次可以重复使用,故套写在静态代码块中,当类加载的时候才会运行一次

    static {
        String config="mybatis.xml";
        InputStream resourceAsStream=null;
        try {
            resourceAsStream = Resources.getResourceAsStream(config);
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            factory = builder.build(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //获取SqlSession的方法
    public static SqlSession getSqlSession(){
        SqlSession sqlSession=null;
        if (factory!=null){
            sqlSession = factory.openSession(true);
        }
        return sqlSession;
    }
}

6.创建测试类

package com.it;



import com.github.pagehelper.PageHelper;
import com.it.dao.StudentDao;
import com.it.domain.Student;
import com.it.untils.MyBatisUntils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;


public class AppTest 
{
@Test
    public void testSelectStudentIf(){
    SqlSession sqlSession = MyBatisUntils.getSqlSession();
    StudentDao mapper = sqlSession.getMapper(StudentDao.class);
    Student student=new Student();
    student.setSname("小白");
    student.setSage(18);
    List studentList = mapper.selectStudentIf(student);
    for (Student stu:studentList){
        System.out.println(stu);
    }
}
@Test
    public void testSelectStudentWhere(){
    SqlSession sqlSession = MyBatisUntils.getSqlSession();
    StudentDao mapper = sqlSession.getMapper(StudentDao.class);
    Student student=new Student();
    student.setSname("小明");
    student.setSage(20);
    mapper.selectStudentWhere(student);
}
@Test
    public  void testSelectForech(){
    SqlSession sqlSession = MyBatisUntils.getSqlSession();
    StudentDao mapper = sqlSession.getMapper(StudentDao.class);
    List list=new ArrayList<>();
    list.add(1);
    list.add(2);
    list.add(3);
    List studentList = mapper.selectForeachOne(list);
    for (Student stu:studentList){
        System.out.println(stu);
    }
}
@Test
    public void testSelectForeach2(){
    SqlSession sqlSession = MyBatisUntils.getSqlSession();
    StudentDao mapper = sqlSession.getMapper(StudentDao.class);
    List list=new ArrayList<>();
    Student s1=new Student();
    s1.setSno(2);
    list.add(s1);
    s1=new Student();
    s1.setSno(5);
    list.add(s1);
    List studentList = mapper.selectForeachTwo(list);
    for (Student student:studentList){
        System.out.println(student);
    }
}

@Test
    public void testAllPageHelper(){
    SqlSession sqlSession = MyBatisUntils.getSqlSession();
    StudentDao mapper = sqlSession.getMapper(StudentDao.class);
    //加入PageHelper的方法,分页,第一个参数为分页起始位置,第二个参数为每个页的数据量的大小
    PageHelper.startPage(0,3);
    List studentList = mapper.selectAll();
    for (Student stu:studentList){
        System.out.println(stu);
    }
}

}

7.依赖




  4.0.0

  com.it
  MyBatis4
  pom
  1.0-SNAPSHOT
  
    MyBatis4Model
    MyBatis5

  

  
    UTF-8
    1.8
    1.8
  

  
    
    
      org.mybatis
      mybatis
      3.5.3
    
    
    
      mysql
      mysql-connector-java
      5.1.37
    

    
      junit
      junit
      4.11
      test
    


    
      com.github.pagehelper
      pagehelper
      5.1.10
    

  

  

    
      
        src/main/java
        
          ***.xml
        
        false
      
      
        src/main/resources
        
          ***.xml
        
        false
      
    

    
      
        
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-jar-plugin
          3.0.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
        
        
          maven-site-plugin
          3.7.1
        
        
          maven-project-info-reports-plugin
          3.0.0
        
      
    
  

数据库内容

结果展示

1.if标签的使用

2.where标签的使用

3.foreach> 循环java中的数组,list集合的。 主要用在sql的in语句中 ,当前传递过来的参数是整形集合

4.foreach> 循环java中的数组,list集合的。 主要用在sql的in语句中 ,当前传递过来的参数是Student对象集合

5.分页,需要使用mevan加载该插件才可以使用

mybatis主配置文件配置插件

 

 6. 把数据库连接信息放到一个单独的文件中。 和mybatis主配置文件分开。
     目的是便于修改,保存,处理多个数据库的信息。

jdbc.properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=123456

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/939062.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号