maven第一个简单项目(CRUD,富文本,多删除,单元测试,采用多模块)
Q1:结构截图
Q2:Computer-entities(实体层)
1.Computer
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_ComputerType
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.ComputerType
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-dao
1.computerDao
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.SqlsessionFactoryUtil
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(); } } }
zywds