总共分为Control,dao,enter,entity,service,util,view这几层。同时还含有一个mapperconfig.xml文件。

1,mapperconfig.xml

这里面用来配置相关数据库的连接和mapper的resource

  <configuration>
<settings>
<setting name="cacheEnabled" value="true"/>
</settings>
<environments default="test">
<environment id="test">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/bookstore?characterEncoding=UTF-8"/>
<property name="username" value="***"/>
<property name="password" value="****"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/chinasofti/bookstore/dao/TypesDao.xml"/>
<mapper resource="com/chinasofti/bookstore/dao/BookDao.xml"/>
</mappers>
</configuration>

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

<?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.chinasofti.bookstore.dao.BookDao">
<resultMap id="book" type="com.chinasofti.bookstore.entity.Book">
<id property="bid" column="BID"/>
<result property="bname" column="BNAME"/>
<result property="descn" column="DESCN"/>
<result property="author" column="author"/>
<result property="price" column="PRICE"/>
<result property="num" column="num"/>
</resultMap>
<resultMap id="booktypes" type="com.chinasofti.bookstore.entity.Book" extends="book">
<association property="t" javaType="com.chinasofti.bookstore.entity.Types">
<id property="tid" column="TID"/>
<result property="tname" column="TNAME"/>
</association>
</resultMap>
<insert id="insert" parameterType="com.chinasofti.bookstore.entity.Book">
insert into book (bid,bname,author,descn,price,num,tid) values (#{bid},#{bname},#{author},#{descn},#{price},#{num},#{t.tid});
</insert>
<delete id="delectById" parameterType="int">
delete from book where bid=#{bid}
</delete>
<select id="selectByName" parameterType="string" resultMap="booktypes">
select *from (select *from book where bname=#{bname}) b ,types t where b.bid=t.tid
</select>
<select id="selectAll" resultMap="booktypes">
select*from book b left join types t on bname=#{bname}and b.bid=t.tid
</select>
<update id="update" parameterType="com.chinasofti.bookstore.entity.Book">
update book <set>
<if test="bname!=null">bname=#{bname},</if>
<if test="author!=null">author=#{author},</if>
<if test="descn!=null">descn=#{descn},</if>
<if test="price!=null">price=#{price},</if>
<if test="num!=null">num=#{num},</if>
</set>
where bid=#{bid}
</update>
</mapper>

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> selectAll();
Book selectByName(String bname);
}

typedao.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.chinasofti.bookstore.dao.TypesDao">
<cache/>
<sql id="ty">tid,tname</sql>
<resultMap id="tp" type="com.chinasofti.bookstore.entity.Types">
<id property="tid" column="TID"/>
<result property="tname" column="TNAME"/>
</resultMap>
<insert id="inserttype" parameterType="string">
insert into types(tname) values (#{tname});
</insert>
<delete id="deleteById" parameterType="int">
delete from types where tid=#{tid}
</delete>
<select id="selectAll" resultMap="tp">
select <include refid="ty"/> from types
</select>
<select id="selectByName" parameterType="string" resultMap="tp">
select <include refid="ty"/> from types where tname=#{tname}
</select>
</mapper>

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> selectAll();
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<Book> findAll(){
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<Types> findAll(){
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<Types> all){
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("请输入类别名称"))
)));
}
}

基于Mybatis的bookstore架构模型的更多相关文章

  1. JVM笔记 -- JVM的发展以及基于栈的指令集架构

    2011年,JDK7发布,1.7u4中,开始启用新的垃圾回收器G1(但是不是默认). 2017年,发布JDK9,G1成为默认GC,代替CMS.(一般公司使用jdk8的时候,会通过参数,指定GC为G1) ...

  2. MFC主窗口架构模型

    根据主窗口类型,MFC软件工程可以分为一下几种架构模型: 1.SDI(Simple Document Interface)单文档界面,一个主窗口下只编辑一份文档 2.MDI(Multiple Docu ...

  3. 高扩展的基于NIO的服务器架构

    当你考虑写一个扩展性良好的基于Java的服务器时,相信你会毫不犹豫地使用Java的NIO包.为了确保你的服务器能够健壮.稳定地运行,你可能会花大量的时间阅读博客和教程来了解线程同步的NIO selec ...

  4. Unity3D中的AI架构模型

    我们都知道现在AI(由人工制造出来的系统所表现出来的模拟人类的智能活动)非常的火,可以说是家喻户晓.当然,在游戏中,AI也是到处可以找到的,对于AI,我们应该关注的问题是如何让游戏角色能够向人或动物那 ...

  5. 基于hadoop的BI架构

    BI系统,是企业利用数据驱动运营的一个典型系统.BI系统通过发掘企业运行过程中的数据,发现企业的潜在风险.为企业的各项决策提供数据支撑. 传统的BI系统通常构建于关系型数据库之上.随着企业业务量的增大 ...

  6. 【神经网络篇】--基于数据集cifa10的经典模型实例

    一.前述 本文分享一篇基于数据集cifa10的经典模型架构和代码. 二.代码 import tensorflow as tf import numpy as np import math import ...

  7. 【深度学习篇】--神经网络中的池化层和CNN架构模型

    一.前述 本文讲述池化层和经典神经网络中的架构模型. 二.池化Pooling 1.目标 降采样subsample,shrink(浓缩),减少计算负荷,减少内存使用,参数数量减少(也可防止过拟合)减少输 ...

  8. 基于SOA的银行系统架构

    Part-1  [简述] 1.通过引入面向服务架构(SOA),企业服务总线(ESB),适配器(Adapter)及面向构件等技术,尝试打造一个统一业务流程服务平台,实现面向流程的服务集成. 2.传统银行 ...

  9. Shuttle ESB(三)——架构模型介绍(2)

    上一篇文章中,介绍了Shuttle ESB架构模型中的三个重要部分. 今天,我们继续介绍剩余的三个内容:模式和消息路由. 四.模式 Request/Response(请求/响应模式) 对基于Reque ...

随机推荐

  1. 多线程事儿(task)之 一(转载)

    此文转载作为记录,转载地址https://www.cnblogs.com/xiaoXuZhi/p/XYH_tsak_one.html 多线程,一个多么熟悉的词汇,作为一名程序员,我相信无论是从事什么开 ...

  2. Mybatis 学习过程中出现空指针异常的错误【已解决】

    Mybatis 学习过程中出现空指针异常的错误[已解决] 以下是写的小测试的代码 bean层 Player类(篮球队队员) bean层 Team类(篮球队) dao层 TeamDao.xml配置文件 ...

  3. 分享在开发多终端使用比较多的Adb命令

    分享在开发多终端或者涉及PC-Android的传输使用比较多的Adb命令 查看连接的设备 adb devices 列出设备安装的软件包 adb shell pm list packages 使用这个方 ...

  4. 小小知识点(二十)利用MATLAB计算定积分

    一重定积分 1. Z = trapz(X,Y,dim) 梯形数值积分,通过已知参数x,y按dim维使用梯形公式进行积分 %举例说明1 clc clear all % int(sin(x),0,pi) ...

  5. Groovy重载操作符

    重载一时爽,一直重载一直爽. 最近在读<Groovy in action>一本书重新复习了Groovy的一些语法特性,迷恋上这个重载操作符的功能,坚持爽的不要要的.分享一个Demo. 由于 ...

  6. Python 处理Excel内的数据(案例介绍*2)

    (一)案例一介绍 现在有一匹电商产品跟当日销量的数据,如下,总共有上万笔的数据,现在需要统计每个品牌当日的销售量,比如美宝莲今天总共卖出了多少的商品,另外需要统计每个品牌下面的每个子品类当日销售量(品 ...

  7. ssh免密登陆和加密解密

    一 丶实现无密码的远程管理 1.生成公钥 私钥 [root@room9pc14 桌面]# ssh-keygen [root@room9pc14 桌面]# ls /root/.ssh/ 2.上传公钥到虚 ...

  8. 从0开发3D引擎(六):函数式反应式编程及其在引擎中的应用

    目录 上一篇博文 介绍函数式反应式编程 函数式反应式编程学习资料 函数式反应式编程的优点与缺点 优点 缺点 异步处理的其它方法 为什么使用Most库 引擎中相关的函数式反应式编程知识点 参考资料 大家 ...

  9. AcWing 247. 亚特兰蒂斯 | 扫描线

    传送门 题目描述 有几个古希腊书籍中包含了对传说中的亚特兰蒂斯岛的描述. 其中一些甚至包括岛屿部分地图. 但不幸的是,这些地图描述了亚特兰蒂斯的不同区域. 您的朋友Bill必须知道地图的总面积. 你自 ...

  10. 构造分组背包(CF)

    Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of t ...