总共分为Control,dao,enter,entity,service,util,view这几层。同时还含有一个mapperconfig.xml文件。
1,mapperconfig.xml
这里面用来配置相关数据库的连接和mapper的resource
2,entity
这是实体类,没什么好说的。就是javabean掌握好就好。总共book和type两个实体类。
1(book)
package com.chinasofti.bookstore.entity;
public class Book {
private int bid;
private String bname;
private String author;
private String descn;
private int price;
private int num;
private Types t;
public Book(int bid, String bname, String author, String descn, int price, int num, Types t) {
this.bid = bid;
this.bname = bname;
this.author = author;
this.descn = descn;
this.price = price;
this.num = num;
this.t = t;
}
public Book(String bname, String author, String descn, int price, int num, Types t) {
this.bname = bname;
this.author = author;
this.descn = descn;
this.price = price;
this.num = num;
this.t = t;
}
public Book() {
}
public int getBid() {
return bid;
}
public void setBid(int bid) {
this.bid = bid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDescn() {
return descn;
}
public void setDescn(String descn) {
this.descn = descn;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public Types getT() {
return t;
}
public void setT(Types t) {
this.t = t;
}
@Override
public String toString() {
return "Book{" +
"bid=" + bid +
", bname='" + bname + '\\'' +
", author='" + author + '\\'' +
", descn='" + descn + '\\'' +
", price=" + price +
", num=" + num +
", t=" + t +
'}';
}
}
2(type)
package com.chinasofti.bookstore.entity;
import java.io.Serializable;
public class Types implements Serializable {
private int tid;
private String tname;
public Types(int tid, String tname) {
this.tid = tid;
this.tname = tname;
}
public Types() {
}
public int getTid() {
return tid;
}
public void setTid(int tid) {
this.tid = tid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
@Override
public String toString() {
return "Types{" +
"tid=" + tid +
", tname='" + tname + '\\'' +
'}';
}
}
3,dao
bookdao.xml
bookdao(接口)
package com.chinasofti.bookstore.dao;
import com.chinasofti.bookstore.entity.Book;
import java.util.List;
public interface BookDao {
int insert(Book book);
int delectById(int bid);
int update(Book book);
List
Book selectByName(String bname);
}
typedao.xml
typedao
package com.chinasofti.bookstore.dao;
import com.chinasofti.bookstore.entity.Types;
import java.util.List;
public interface TypesDao {
int inserttype(String tname);
int deleteById(int tid);
List
Types selectByName(String name);
}
4,service
bookservice
package com.chinasofti.bookstore.service;
import com.chinasofti.bookstore.dao.BookDao;
import com.chinasofti.bookstore.entity.Book;
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.util.List;
public class BookService {
private SqlSessionFactory factory;
private SqlSession session;
private BookDao dao;
public BookService(){
try {
factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("sqlconfig.xml"));
session = factory.openSession();
dao = session.getMapper(BookDao.class);
} catch (IOException e) {
e.printStackTrace();
System.out.println("sqlsession创建失败");
}
}
public String addBook(Book book){
Book b = dao.selectByName(book.getBname());
if (b!=null){
return "该书以存在";
}
String s= dao.insert(book)>0?"添加成功":"添加失败";
session.commit();
return s;
}
public List
return dao.selectAll();
}
public String remove(int bid){
String s=dao.delectById(bid)>0?"删除成功":"删除失败";
session.commit();
return s;
}
public String change(Book book){
String s=dao.update(book)>0?"修改成功":"修改失败";
session.commit();
return s;
}
}
typeservice
package com.chinasofti.bookstore.service;
import com.chinasofti.bookstore.dao.TypesDao;
import com.chinasofti.bookstore.entity.Types;
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.util.List;
public class TypesService {
private SqlSessionFactory factory;
private SqlSession session;
private TypesDao dao;
public TypesService(){
try {
factory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("sqlconfig.xml"));
session = factory.openSession();
dao = session.getMapper(TypesDao.class);
} catch (IOException e) {
e.printStackTrace();
System.out.println("创建失败");
}
}
public String addType(String tname){
Types t = dao.selectByName(tname);
if (t!=null){
return "该类别已存在,请重新输入";
}
if (dao.inserttype(tname)>0){
session.commit();
return "添加成功";
}else {
return "添加失败";
}
}
public List
return dao.selectAll();
}
public String remove(int tid){
String s=dao.deleteById(tid)>0?"删除成功":"删除失败";
session.commit();
return s;
}
public Types findByName(String tname){
return dao.selectByName(tname);
}
}
5,util
package com.chinasofti.bookstore.util;
import java.util.Scanner;
public class UserInput {
//创造用于接收用户输入整数的方法
public static int getInt(String wang){
System.out.println(wang);
while (true){
Scanner sc=new Scanner(System.in);
try {
return sc.nextInt();
}
catch (Exception e){
System.out.println("对不起,输入格式不正确,请重新输入");
}
}
}
public static double getDouble(String wang){
System.out.println(wang);
while (true){
Scanner sc=new Scanner(System.in);
try {
return sc.nextDouble();
}
catch (Exception e){
System.out.println("对不起,输入格式不正确,请重新输入");
}
}
}
public static String getString(String wang){
System.out.println(wang);
Scanner sc=new Scanner(System.in);
return sc.next();
}
}
6,view
package com.chinasofti.bookstore.view;
import com.chinasofti.bookstore.entity.Types;
import java.util.List;
/*输出界面
* 图书管理
1、添加图书类别
2、显示图书类别
3、删除图书类别
4、添加图书
5、查询图书
6、删除图书
7、修改图书
8、根据类别查询图书*/
public class View {
public static void welcome(){
System.out.println("---------欢迎来到我的世界--------------");
System.out.println("1、添加图书类别");
System.out.println("2、显示图书类别");
System.out.println("3、删除图书类别");
System.out.println("4、添加图书");
System.out.println("5、查询图书");
System.out.println("6、删除图书");
System.out.println("7、修改图书");
System.out.println("8、根据类别查询图书");
System.out.println("---------------------------------------");
}
public static void showType(List
System.out.println("所有类别如下");
System.out.println("编号\t名称");
for (Types t:all){
System.out.println(t.getTid()+"\t"+t.getTname());
}
}
}
7,control(业务逻辑只写了一个,其余的依葫芦画瓢就好)
package com.chinasofti.bookstore.control;
import com.chinasofti.bookstore.entity.Book;
import com.chinasofti.bookstore.entity.Types;
import com.chinasofti.bookstore.service.BookService;
import com.chinasofti.bookstore.service.TypesService;
import com.chinasofti.bookstore.util.UserInput;
import com.chinasofti.bookstore.view.View;
import java.util.List;
public class Control {
private BookService service;
private TypesService typesService;
public Control() {
this.service = new BookService();
this.typesService=new TypesService();
}
public void start(){
//显示主界面
View.welcome();
//接收用户输入的指令
int select = UserInput.getInt("请选择");
if (select<=0){
System.out.println("欢迎下次再来,886");
System.exit(0);
}else if(select==4){
this.addbook();
}
}
private void addbook() {
System.out.println("-----------请添加图书--------------");
View.showType(this.typesService.findAll());
System.out.println(this.service.addBook(new Book(
UserInput.getString("请输入书名"),
UserInput.getString("请输入图书的作者"),
UserInput.getString("请输入图书的描述"),
UserInput.getInt("请输入图书的价格"),
UserInput.getInt("请输入图书的数量"),
typesService.findByName(UserInput.getString("请输入类别名称"))
)));
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章