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

JavaEE实验一:熟悉java EE编程环境

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

JavaEE实验一:熟悉java EE编程环境

实验一:熟悉java EE编程环境

实验内容: 创建学生数据库,建学生信息表(sno,sname,ssex,birthday,major)并输入几行数据。
参考本章案例,查询所有女生的信息。

实验步骤
  • 1、创建maven项目,在pom.xml文件中配置依赖:MyBatis、mysql、Junit依赖
  • 2、创建module
  • 3、在resources下建db.properties、mybatis-config.xml文件。注意要在mybatis-config.xml文件中引入db.properties文件
  • 4、创建学生信息表并输入数据
  • 5、在src.main.java中建包com.pojo,包中创建学生表对应的POJO类,建包com.dao,包中定义接口StudentDAO,接口中定义查询女生的抽象方法。
  • 6、在src.main.resources中建包com.dao,包中建StudentMapper.xml文件,在文件中绑定接口com.dao.StudentDAO,定义查询映射语句。
  • 7、在mybatis-config.xml文件中注册StudentMapper.xml文件
  • 8、在Test.java目录中创建测试类。
  • 10、utils
  • 10、运行测试方法。

1、创建maven项目,在pom.xml文件中配置依赖:MyBatis、mysql、Junit依赖


    4.0.0

    org.example
    IDEA
    pom
    1.0-SNAPSHOT
    
        md1
    

    
        8
        8
    

    
        
            org.mybatis
            mybatis
            3.5.2
        
        
            mysql
            mysql-connector-java
            8.0.11
        
        
            junit
            junit
            4.12
            test
        
    


2、创建module
mysql.driver=com.mysql.cj.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/mybatis?  serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
mysql.username=root
mysql.password=123456
3、在resources下建db.properties、mybatis-config.xml文件。注意要在mybatis-config.xml文件中引入db.properties文件



    
    
        
            
            
                
                
                
                
            
        
    
    
        
    

4、创建学生信息表并输入数据 5、在src.main.java中建包com.pojo,包中创建学生表对应的POJO类,建包com.dao,包中定义接口StudentDAO,接口中定义查询女生的抽象方法。
package com.pojo;
public class Student {
    private  Integer sno;
    private  String sname;
    private  Boolean ssex;
    private  String birthday;
    private  String major;
    public Integer getSno() {
        return sno;
    }
    public void setSno(Integer sno) {
        this.sno = sno;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public Boolean getSsex() {
        return ssex;
    }
    public void setSsex(Boolean ssex) {
        this.ssex = ssex;
    }
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
    public String getMajor() {
        return major;
    }
    public void setMajor(String major) {
        this.major = major;
    }
    @Override
    public String toString() {
        return "StudentDAO{" +
                "sno=" + sno +
                ", sname='" + sname + ''' +
                ", ssex=" + ssex +
                ", birthday='" + birthday + ''' +
                ", major='" + major + ''' +
                '}';
    }
}
6、在src.main.resources中建包com.dao,包中建StudentMapper.xml文件,在文件中绑定接口com.dao.StudentDAO,定义查询映射语句。
package com.dao;
import com.pojo.Student;
import java.util.List;
public interface StudentDAO {
    public List getStudentList();
    public List getStudentListBySex();
7、在mybatis-config.xml文件中注册StudentMapper.xml文件




    
        select * from student
    
    


8、在Test.java目录中创建测试类。
package test;
import com.dao.StudentDAO;
import com.pojo.Student;
import com.utils.MybatisUtils;
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.Reader;
import java.util.List;
public class Test {
    @org.junit.Test
    public void userFindByIdTest() {
        String resources = "mybatis-config.xml";
        Reader reader=null;
        try {
            reader= Resources.getResourceAsReader(resources);
        } catch (IOException e){
         e.printStackTrace();}

        SqlSessionFactory sqlMapper=new
                SqlSessionFactoryBuilder().build(reader);

        SqlSession session=sqlMapper.openSession();
        StudentDAO mapper =session.getMapper(StudentDAO.class);
        List students = mapper.getStudentList();
        for(Student student:students) {
           System.out.println(student);
        }
        List studentsex = mapper.getStudentListBySex();
        for(Student student:studentsex) {
            System.out.println("*false=女,true=男*---性别为女的学生信息:"+student);
        }
        session.close();}
    @org.junit.Test
    public void userFindByIdTest2(){
        SqlSession session = MybatisUtils.getSqlsession();
        StudentDAO mapper=session.getMapper(StudentDAO.class);
        List students = mapper.getStudentList();
        for(Student student:students) {
            System.out.println(student);
        }
        List studentsex = mapper.getStudentListBySex();
        for(Student student:studentsex) {
            System.out.println("*false=女,true=男*---性别为女的学生信息:"+student);
        }
        session.close();
    }
}
10、utils
package com.utils;

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 MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;
    static {
    try
    {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }
    catch(IOException e) {
        e.printStackTrace();
    }
}
    public  static SqlSession getSqlsession(){return  sqlSessionFactory.openSession();}
}
10、运行测试方法。

userFindByIdTest

userFindByIdTest

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

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

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