MyBatis实现基本CRUD以及进行单元测试

整体项目结构

bean代码

package bean;

public class Apply {
    private long id;
    private String sname;
    private String qq;
    private long enterTime;
    private String type;
    private String school;
    private long number;
    private String repLink;
    private String goal;

    @Override
    public String toString() {
        return "Apply{" +
                "id=" + id +
                ", sname='" + sname + '\'' +
                ", qq='" + qq + '\'' +
                ", enterTime=" + enterTime +
                ", type='" + type + '\'' +
                ", school='" + school + '\'' +
                ", number=" + number +
                ", repLink='" + repLink + '\'' +
                ", goal='" + goal + '\'' +
                '}';
    }

    public Apply() {
    }

    public Apply(int id, String sname, String qq, long enterTime, String type, String school, long number, String repLink, String goal) {
        this.id = id;
        this.sname = sname;
        this.qq = qq;
        this.enterTime = enterTime;
        this.type = type;
        this.school = school;
        this.number = number;
        this.repLink = repLink;
        this.goal = goal;
    }

    public Apply(String sname, String qq, long enterTime, String type, String school, long number, String repLink, String goal) {
        this.sname = sname;
        this.qq = qq;
        this.enterTime = enterTime;
        this.type = type;
        this.school = school;
        this.number = number;
        this.repLink = repLink;
        this.goal = goal;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getSname() {
        return sname;
    }

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

    public String getQq() {
        return qq;
    }

    public void setQq(String qq) {
        this.qq = qq;
    }

    public long getEnterTime() {
        return enterTime;
    }

    public void setEnterTime(long enterTime) {
        this.enterTime = enterTime;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }

    public long getNumber() {
        return number;
    }

    public void setNumber(long number) {
        this.number = number;
    }

    public String getRepLink() {
        return repLink;
    }

    public void setRepLink(String repLink) {
        this.repLink = repLink;
    }

    public String getGoal() {
        return goal;
    }

    public void setGoal(String goal) {
        this.goal = goal;
    }

}

bean映射文件 apply.xml






    
    
    
    
    
    
    
    

    
        insert into applytable(id,sname, qq, entertime, `type`, school,`number`,replink,goal)
        values (
                null,
                #{sname},
                #{qq},
                #{enterTime},
                #{type},
                #{school},
                #{number},
                #{repLink},
                #{goal}
            )
    
    
        update applytable set sname = #{sname}, qq = #{qq},
                entertime = #{enterTime}, `type` = #{type}, school = #{school},`number` = #{number}, replink = #{repLink}, goal = #{goal}
                where id = #{id}
    
    
    
    
        delete from applytable where id = #{id}
    

数据库属性文件 config.properties

jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/xiuzhenyuan?characterEncoding=UTF-8
jdbc.username = root
jdbc.password = admin

MyBatis 配置文件 mybatis-config.xml





    
    
        
        
    

    
    

    
        
        
        
        
        
    

    
    
        
            
            
            
            
                
                
                
                
            
        
    


    
    
        
        
        

        
        
        

    


单元测试代码 ApplyTest.java

package com.jms;

import bean.Apply;
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 org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class ApplyTest {
    public SqlSessionFactory getSqlSessionFactory() throws IOException {
        // mybatis配置文件,这个地方的root地址为:resources,路径要对。
        String resource = "mybatis-config.xml";
        // 得到配置文件流
        InputStream inputStream = Resources.getResourceAsStream(resource);
        // 创建会话工厂,传入mybatis的配置文件信息
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        return sqlSessionFactory;
    }

    // 根据id查询用户信息,得到一条记录结果
    @Test
    public void getApply() throws IOException {

        // 通过工厂得到SqlSession
        SqlSession sqlSession = this.getSqlSessionFactory().openSession();

        // 通过SqlSession操作数据库
        // 第一个参数:映射文件中statement的id,等于=namespace+"."+statement的id
        // 第二个参数:指定和映射文件中所匹配的parameterType类型的参数
        // sqlSession.selectOne结果 是与映射文件中所匹配的resultType类型的对象

        // selectOne查询出一条记录(这种很麻烦的!!!往后看看)
        //这里的参数test.findUserById,test为命名空间,要与user.xml中的对应起来,
        //同理,findUserById也要在user.xml中存在,不然都会报错
        Apply apply = sqlSession.selectOne("test.getApply", 2);
        // 释放资源
        sqlSession.close();
    }

    @Test
    public void addApply() throws IOException {
        SqlSession sqlSession = this.getSqlSessionFactory().openSession();
        sqlSession.insert("test.addApply", new Apply("Alice", "43523", 3452341, "Html", "SCHOOL", 4, "hgw3204", "DAY DAY UP"));
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void updateApply() throws IOException {
        SqlSession sqlSession = this.getSqlSessionFactory().openSession();
        sqlSession.update("test.updateApply", new Apply(2, "Alice", "43523", 3452341, "Html", "SCHOOL", 4, "hgw3204", "DAY DAY UP"));
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void getAll() throws IOException {
        SqlSession sqlSession = this.getSqlSessionFactory().openSession();
        final List selectList = sqlSession.selectList("test.getAllApply");
        for (Apply apply :
                selectList) {
            System.out.println(apply);
        }
        sqlSession.close();
    }

    @Test
    public void getTotal() throws IOException {
        SqlSession sqlSession = this.getSqlSessionFactory().openSession();
        final Integer total = (Integer) sqlSession.selectOne("test.getTotalApply");
        System.out.println(total);
    }
    
    @Test
    public void delete() throws IOException {
        SqlSession sqlSession = this.getSqlSessionFactory().openSession();
        sqlSession.delete("test.deleteApply", 20);
        sqlSession.commit();
        sqlSession.close();
    }
}


单元测试结果

你可能感兴趣的