maven第一個簡單項目(CRUD,富文本,多刪除,單元測試,採用多模塊)

Q1:結構截圖java

Q2:Computer-entities(實體層)sql

1.Computerapache

package com.zhangyuwei.Computer.entities;

import java.util.Date;

public class Computer {
    int cId;
    String cName;
    float cPrice;
    Date cBirthday;
    int cState;
    String cDesc;
    int ctId;

    public int getcId() {
        return cId;
    }

    public void setcId(int cId) {
        this.cId = cId;
    }

    public String getcName() {
        return cName;
    }

    public void setcName(String cName) {
        this.cName = cName;
    }

    public float getcPrice() {
        return cPrice;
    }

    public void setcPrice(float cPrice) {
        this.cPrice = cPrice;
    }

    public Date getcBirthday() {
        return cBirthday;
    }

    public void setcBirthday(Date cBirthday) {
        this.cBirthday = cBirthday;
    }

    public int getcState() {
        return cState;
    }

    public void setcState(int cState) {
        this.cState = cState;
    }

    public String getcDesc() {
        return cDesc;
    }

    public void setcDesc(String cDesc) {
        this.cDesc = cDesc;
    }

    public int getCtId() {
        return ctId;
    }

    public void setCtId(int ctId) {
        this.ctId = ctId;
    }

    @Override
    public String toString() {
        return "Computer{" +
                "cId=" + cId +
                ", cName='" + cName + '\'' +
                ", cPrice=" + cPrice +
                ", cBirthday=" + cBirthday +
                ", cState=" + cState +
                ", cDesc='" + cDesc + '\'' +
                ", ctId=" + ctId +
                '}';
    }
}

2.Computer_ComputerTypesession

package com.zhangyuwei.Computer.entities;

public class Computer_ComputerType extends Computer{
    int ctId;
    String ctName;

    public int getCtId() {
        return ctId;
    }

    public void setCtId(int ctId) {
        this.ctId = ctId;
    }

    public String getCtName() {
        return ctName;
    }

    public void setCtName(String ctName) {
        this.ctName = ctName;
    }
    @Override
    public String toString() {
        return "Computer_ComputerType{" +
                "ctId=" + ctId +
                ", ctName='" + ctName + '\'' +
                ", Computer='" +super.cId+'\''+
                ", Computer='" +super.cName+'\''+
                ", Computer='" +super.cPrice+'\''+
                ", Computer='" +super.cBirthday+'\''+
                ", Computer='" +super.cState+'\''+
                ", Computer='" +super.cDesc+'\''+
                '}';
    }
}

3.ComputerTypemybatis

package com.zhangyuwei.Computer.entities;

public class ComputerType {
    int ctId;
    String ctName;

    public int getCtId() {
        return ctId;
    }

    public void setCtId(int ctId) {
        this.ctId = ctId;
    }

    public String getCtName() {
        return ctName;
    }

    public void setCtName(String ctName) {
        this.ctName = ctName;
    }

    @Override
    public String toString() {
        return "ComputerType{" +
                "ctId=" + ctId +
                ", ctName='" + ctName + '\'' +
                '}';
    }
}

Q3:Computer-daoapp

 

 1.computerDaoide

package com.zhangyuwei.Computer.dao;

import com.zhangyuwei.Computer.entities.Computer;
import com.zhangyuwei.Computer.entities.ComputerType;
import com.zhangyuwei.Computer.entities.Computer_ComputerType;
import com.zhangyuwei.mybatis.utils.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;

public class computerDao implements IcomputerDao {
    /*查詢全部的電腦*/
    public List<Computer> selectAllComputer(){
        //打開一個會話
        SqlSession session=SqlSessionFactoryUtil.openSession(true);
        IcomputerDao it=session.getMapper(IcomputerDao.class);
        List<Computer> entity=it.selectAllComputer();
        return entity;
    }
    /*添加電腦*/
    public int insertComputer(Computer entity){
        //打開一個會話
        SqlSession session=SqlSessionFactoryUtil.openSession(false);
        IcomputerDao it=session.getMapper(IcomputerDao.class);
        int rows=it.insertComputer(entity);
        session.commit();
        return rows;
    }
    /*刪除電腦*/
    public int deleteComputer(int cId){
        //打開一個會話
        SqlSession session=SqlSessionFactoryUtil.openSession(false);
        IcomputerDao it=session.getMapper(IcomputerDao.class);
        int rows=it.deleteComputer(cId);
        session.commit();
        return rows;
    }
    /*修改電腦*/
    public int updateComputer(Computer entity){
        //打開一個會話
        SqlSession session=SqlSessionFactoryUtil.openSession(false);
        IcomputerDao it=session.getMapper(IcomputerDao.class);
        int rows=it.updateComputer(entity);
        session.commit();
        return rows;
    }
    /*查詢全部的電腦類型*/
    public List<ComputerType> selectAllComputerType(){
        //打開一個會話
        SqlSession session=SqlSessionFactoryUtil.openSession(true);
        IcomputerDao it=session.getMapper(IcomputerDao.class);
        List<ComputerType> entity=it.selectAllComputerType();
        return entity;
    }
    /*查詢全部的電腦類型if*/
    public List<Computer_ComputerType> selectComputerWithComputerTypeif(Map<String,Object> map){
        //打開一個會話
        SqlSession session=SqlSessionFactoryUtil.openSession(true);
        IcomputerDao it=session.getMapper(IcomputerDao.class);
        List<Computer_ComputerType> entity=it.selectComputerWithComputerTypeif(map);
        return entity;
    }
    /*查詢出全部的電腦與電腦類型*/
    public List<Computer_ComputerType> selectAllComputerTypeWithComputer(){
        //打開一個會話
        SqlSession session=SqlSessionFactoryUtil.openSession(true);
        IcomputerDao it=session.getMapper(IcomputerDao.class);
        List<Computer_ComputerType> entity=it.selectAllComputerTypeWithComputer();
        return entity;
    }
    /*根據類型id查詢出電腦與電腦類型*/
    public List<Computer_ComputerType> selectComputerTypeWithComputerByctId(int ctId){
        //打開一個會話
        SqlSession session=SqlSessionFactoryUtil.openSession(true);
        IcomputerDao it=session.getMapper(IcomputerDao.class);
        List<Computer_ComputerType> entity=it.selectComputerTypeWithComputerByctId(ctId);
        return entity;
    }
    /*查出個數*/
    public int selectComputerWithComputerTypeCount(){
        //打開一個會話
        SqlSession session=SqlSessionFactoryUtil.openSession(true);
        IcomputerDao it=session.getMapper(IcomputerDao.class);
        int rows=it.selectComputerWithComputerTypeCount();
        return rows;
    }
    /*分頁*/
    public List<Computer_ComputerType> selectComputerWithComputerTypePage(int arg0,int arg1){
        //打開一個會話
        SqlSession session=SqlSessionFactoryUtil.openSession(true);
        IcomputerDao it=session.getMapper(IcomputerDao.class);
        List<Computer_ComputerType> entity=it.selectComputerWithComputerTypePage((arg0-1)*arg1,arg1);
        return entity;
    }
    /*刪除多項*/
    public int deleteComputerWithComputerTypeSome(List<Integer> entity){
        //打開一個會話
        SqlSession session=SqlSessionFactoryUtil.openSession(false);
        IcomputerDao it=session.getMapper(IcomputerDao.class);
        int rows=it.deleteComputerWithComputerTypeSome(entity);
        session.commit();
        return rows;
    }
}

2.IcomputerDao工具

package com.zhangyuwei.Computer.dao;

import com.zhangyuwei.Computer.entities.Computer;
import com.zhangyuwei.Computer.entities.ComputerType;
import com.zhangyuwei.Computer.entities.Computer_ComputerType;

import java.util.List;
import java.util.Map;

public interface IcomputerDao {
    /*查詢全部的電腦*/
    List<Computer> selectAllComputer();
    /*添加電腦*/
    int insertComputer(Computer entity);
    /*刪除電腦*/
    int deleteComputer(int cId);
    /*修改電腦*/
    int updateComputer(Computer entity);
    /*查詢全部的電腦類型*/
    List<ComputerType> selectAllComputerType();
    /*查詢出全部的電腦與電腦類型*/
    List<Computer_ComputerType> selectAllComputerTypeWithComputer();
    /*根據類型id查詢出電腦與電腦類型*/
    List<Computer_ComputerType> selectComputerTypeWithComputerByctId(int ctId);
    /*查出個數*/
    int selectComputerWithComputerTypeCount();
    /*分頁*/
    List<Computer_ComputerType> selectComputerWithComputerTypePage(int arg0,int arg1);
    /*多條件查詢*/
    List<Computer_ComputerType> selectComputerWithComputerTypeif(Map<String,Object> map);
    /*多項刪除*/
    int deleteComputerWithComputerTypeSome(List<Integer> entity);
}

3.SqlsessionFactoryUtilui

package com.zhangyuwei.mybatis.utils;

import com.zhangyuwei.Computer.dao.computerDao;
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;

/**
 * MyBatis 會話工具類
 * */
public class SqlSessionFactoryUtil {
    /**
     * 得到會話工廠
     *
     * */
    public static SqlSessionFactory getFactory(){
        InputStream inputStream = null;
        SqlSessionFactory sqlSessionFactory=null;

        try{
            //加載conf.xml配置文件,轉換成輸入流
            inputStream = computerDao.class.getClassLoader().getResourceAsStream("mybatisConf.xml");

            //根據配置文件的輸入流構造一個SQL會話工廠
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        }
        finally {
            if(inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sqlSessionFactory;
    }

    /**
     * 得到sql會話,是否自動提交
     * */
    public static SqlSession openSession(boolean isAutoCommit){
        return getFactory().openSession(isAutoCommit);
    }

    /**
     * 關閉會話
     * */
    public static void closeSession(SqlSession session){
        if(session!=null){
            session.close();
        }
    }
}
相關文章
相關標籤/搜索