第二个mybatis练习,使用MyBatisUntils工具类

项目结构

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 {
    List<Student> selectStudents();
    int insertStudents(Student student);
}

3.创建接口映射文件StudentDao.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="com.it.dao.StudentDao">
    <select id="selectStudents" resultType="com.it.domain.Student">
select * from student
    </select>
    <insert id="insertStudents" >
        insert into student values (#{sno},#{sname},#{ssex},#{sage},#{sdept})
    </insert>
</mapper>



4.创建MyBatis.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>
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

    <environments default="mydev">
        <environment id="mydev">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/it/dao/StudentDao.xml"/>
    </mappers>
</configuration>

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 {
    //    SqlSessionFactory设置静态属性,是因为SqlSessionFactory : 重量级对象, 
//    程序创建一个对象耗时比较长,使用资源比较多,在整个项目中,有一个就够用了。
    private static SqlSessionFactory factory = null;
//    静态代码块,在加载该类时运行,且运行一次,避免了多次调用该方法多次运行,节约了时间和空间
    static {
//   测试了一下,虽然我的MyBatis配置文件名称是MyBatis.xml,但是输入mybatis.xml也可以成功运行
//   这个项目是我新建的,不存在调用该工程其他模块的mybatis.xml配置文件的情况,这可以说明这里应该是不区分大小写的
        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.创建StudentDao接口实现类,放在impl包中命名为StudentDaoImpl

package com.it.dao.impl;

import com.it.dao.StudentDao;
import com.it.domain.Student;
import com.it.untils.MyBatisUntils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class StudentDaoImpl implements StudentDao {
    @Override
    public List<Student> selectStudents() {
        SqlSession sqlSession = MyBatisUntils.getSqlSession();
        String sql="com.it.dao.StudentDao.selectStudents";
        List<Student> list = sqlSession.selectList(sql);
        sqlSession.close();
        return list;
    }

    @Override
    public int insertStudents(Student student) {
        SqlSession sqlSession = MyBatisUntils.getSqlSession();
        String sql="com.it.dao.StudentDao.insertStudents";
        int i = sqlSession.insert(sql,student);
        sqlSession.close();
        return i;
    }
}

7.创建测试类

package com.it;

import static org.junit.Assert.assertTrue;

import com.it.dao.StudentDao;
import com.it.dao.impl.StudentDaoImpl;
import com.it.domain.Student;
import org.junit.Test;

import java.util.List;

/**
 * Unit test for simple App.
 */
public class AppTest 
{
@Test
    public  void selectStudents(){
    StudentDao studentDao=new StudentDaoImpl();
    List<Student> studentList = studentDao.selectStudents();
    for (Student stu:studentList){
        System.out.println(stu);
    }
}
@Test
public void testInsertStudent(){
  StudentDao studentDao=new StudentDaoImpl();
  Student student=new Student();
  student.setSno(7);
  student.setSname("小燕");
  student.setSsex("女");
  student.setSage(21);
  student.setSdept("信电");
    int i = studentDao.insertStudents(student);
    System.out.println(i);
}
}

测试结果

数据库中的数据

 程序查找到的语句

增加数据项

 查看数据库是否发生变化

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

做一道光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值