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

1,mapperconfig.xml

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

  1. <configuration>
  2. <settings>
  3. <setting name="cacheEnabled" value="true"/>
  4. </settings>
  5. <environments default="test">
  6. <environment id="test">
  7. <transactionManager type="JDBC"></transactionManager>
  8. <dataSource type="POOLED">
  9. <property name="driver" value="com.mysql.jdbc.Driver"/>
  10. <property name="url" value="jdbc:mysql://localhost:3306/bookstore?characterEncoding=UTF-8"/>
  11. <property name="username" value="***"/>
  12. <property name="password" value="****"/>
  13. </dataSource>
  14. </environment>
  15. </environments>
  16. <mappers>
  17. <mapper resource="com/chinasofti/bookstore/dao/TypesDao.xml"/>
  18. <mapper resource="com/chinasofti/bookstore/dao/BookDao.xml"/>
  19. </mappers>
  20. </configuration>

2,entity

这是实体类,没什么好说的。就是javabean掌握好就好。总共book和type两个实体类。

1(book)

  1. package com.chinasofti.bookstore.entity;
  2.  
  3. public class Book {
  4. private int bid;
  5. private String bname;
  6. private String author;
  7. private String descn;
  8. private int price;
  9. private int num;
  10. private Types t;
  11.  
  12. public Book(int bid, String bname, String author, String descn, int price, int num, Types t) {
  13. this.bid = bid;
  14. this.bname = bname;
  15. this.author = author;
  16. this.descn = descn;
  17. this.price = price;
  18. this.num = num;
  19. this.t = t;
  20. }
  21.  
  22. public Book(String bname, String author, String descn, int price, int num, Types t) {
  23. this.bname = bname;
  24. this.author = author;
  25. this.descn = descn;
  26. this.price = price;
  27. this.num = num;
  28. this.t = t;
  29. }
  30.  
  31. public Book() {
  32. }
  33.  
  34. public int getBid() {
  35. return bid;
  36. }
  37.  
  38. public void setBid(int bid) {
  39. this.bid = bid;
  40. }
  41.  
  42. public String getBname() {
  43. return bname;
  44. }
  45.  
  46. public void setBname(String bname) {
  47. this.bname = bname;
  48. }
  49.  
  50. public String getAuthor() {
  51. return author;
  52. }
  53.  
  54. public void setAuthor(String author) {
  55. this.author = author;
  56. }
  57.  
  58. public String getDescn() {
  59. return descn;
  60. }
  61.  
  62. public void setDescn(String descn) {
  63. this.descn = descn;
  64. }
  65.  
  66. public int getPrice() {
  67. return price;
  68. }
  69.  
  70. public void setPrice(int price) {
  71. this.price = price;
  72. }
  73.  
  74. public int getNum() {
  75. return num;
  76. }
  77.  
  78. public void setNum(int num) {
  79. this.num = num;
  80. }
  81.  
  82. public Types getT() {
  83. return t;
  84. }
  85.  
  86. public void setT(Types t) {
  87. this.t = t;
  88. }
  89.  
  90. @Override
  91. public String toString() {
  92. return "Book{" +
  93. "bid=" + bid +
  94. ", bname='" + bname + '\'' +
  95. ", author='" + author + '\'' +
  96. ", descn='" + descn + '\'' +
  97. ", price=" + price +
  98. ", num=" + num +
  99. ", t=" + t +
  100. '}';
  101. }
  102. }

2(type)

  1. package com.chinasofti.bookstore.entity;
  2.  
  3. import java.io.Serializable;
  4.  
  5. public class Types implements Serializable {
  6. private int tid;
  7. private String tname;
  8.  
  9. public Types(int tid, String tname) {
  10. this.tid = tid;
  11. this.tname = tname;
  12. }
  13.  
  14. public Types() {
  15. }
  16.  
  17. public int getTid() {
  18. return tid;
  19. }
  20.  
  21. public void setTid(int tid) {
  22. this.tid = tid;
  23. }
  24.  
  25. public String getTname() {
  26. return tname;
  27. }
  28.  
  29. public void setTname(String tname) {
  30. this.tname = tname;
  31. }
  32.  
  33. @Override
  34. public String toString() {
  35. return "Types{" +
  36. "tid=" + tid +
  37. ", tname='" + tname + '\'' +
  38. '}';
  39. }
  40. }

3,dao

bookdao.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.chinasofti.bookstore.dao.BookDao">
  6. <resultMap id="book" type="com.chinasofti.bookstore.entity.Book">
  7. <id property="bid" column="BID"/>
  8. <result property="bname" column="BNAME"/>
  9. <result property="descn" column="DESCN"/>
  10. <result property="author" column="author"/>
  11. <result property="price" column="PRICE"/>
  12. <result property="num" column="num"/>
  13. </resultMap>
  14. <resultMap id="booktypes" type="com.chinasofti.bookstore.entity.Book" extends="book">
  15. <association property="t" javaType="com.chinasofti.bookstore.entity.Types">
  16. <id property="tid" column="TID"/>
  17. <result property="tname" column="TNAME"/>
  18. </association>
  19. </resultMap>
  20. <insert id="insert" parameterType="com.chinasofti.bookstore.entity.Book">
  21. insert into book (bid,bname,author,descn,price,num,tid) values (#{bid},#{bname},#{author},#{descn},#{price},#{num},#{t.tid});
  22. </insert>
  23. <delete id="delectById" parameterType="int">
  24. delete from book where bid=#{bid}
  25. </delete>
  26. <select id="selectByName" parameterType="string" resultMap="booktypes">
  27. select *from (select *from book where bname=#{bname}) b ,types t where b.bid=t.tid
  28. </select>
  29. <select id="selectAll" resultMap="booktypes">
  30. select*from book b left join types t on bname=#{bname}and b.bid=t.tid
  31. </select>
  32. <update id="update" parameterType="com.chinasofti.bookstore.entity.Book">
  33. update book <set>
  34. <if test="bname!=null">bname=#{bname},</if>
  35. <if test="author!=null">author=#{author},</if>
  36. <if test="descn!=null">descn=#{descn},</if>
  37. <if test="price!=null">price=#{price},</if>
  38. <if test="num!=null">num=#{num},</if>
  39. </set>
  40. where bid=#{bid}
  41. </update>
  42. </mapper>

bookdao(接口)

  1. package com.chinasofti.bookstore.dao;
  2.  
  3. import com.chinasofti.bookstore.entity.Book;
  4.  
  5. import java.util.List;
  6.  
  7. public interface BookDao {
  8. int insert(Book book);
  9. int delectById(int bid);
  10. int update(Book book);
  11. List<Book> selectAll();
  12. Book selectByName(String bname);
  13. }

typedao.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.chinasofti.bookstore.dao.TypesDao">
  6. <cache/>
  7. <sql id="ty">tid,tname</sql>
  8. <resultMap id="tp" type="com.chinasofti.bookstore.entity.Types">
  9. <id property="tid" column="TID"/>
  10. <result property="tname" column="TNAME"/>
  11. </resultMap>
  12. <insert id="inserttype" parameterType="string">
  13. insert into types(tname) values (#{tname});
  14. </insert>
  15. <delete id="deleteById" parameterType="int">
  16. delete from types where tid=#{tid}
  17. </delete>
  18. <select id="selectAll" resultMap="tp">
  19. select <include refid="ty"/> from types
  20. </select>
  21. <select id="selectByName" parameterType="string" resultMap="tp">
  22. select <include refid="ty"/> from types where tname=#{tname}
  23. </select>
  24. </mapper>

typedao

  1. package com.chinasofti.bookstore.dao;
  2.  
  3. import com.chinasofti.bookstore.entity.Types;
  4.  
  5. import java.util.List;
  6.  
  7. public interface TypesDao {
  8. int inserttype(String tname);
  9. int deleteById(int tid);
  10. List<Types> selectAll();
  11. Types selectByName(String name);
  12. }

4,service

bookservice

  1. package com.chinasofti.bookstore.service;
  2.  
  3. import com.chinasofti.bookstore.dao.BookDao;
  4. import com.chinasofti.bookstore.entity.Book;
  5. import org.apache.ibatis.io.Resources;
  6. import org.apache.ibatis.session.SqlSession;
  7. import org.apache.ibatis.session.SqlSessionFactory;
  8. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  9.  
  10. import java.io.IOException;
  11. import java.util.List;
  12.  
  13. public class BookService {
  14. private SqlSessionFactory factory;
  15. private SqlSession session;
  16. private BookDao dao;
  17. public BookService(){
  18. try {
  19. factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("sqlconfig.xml"));
  20. session = factory.openSession();
  21. dao = session.getMapper(BookDao.class);
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. System.out.println("sqlsession创建失败");
  25. }
  26. }
  27. public String addBook(Book book){
  28. Book b = dao.selectByName(book.getBname());
  29. if (b!=null){
  30. return "该书以存在";
  31. }
  32. String s= dao.insert(book)>0?"添加成功":"添加失败";
  33. session.commit();
  34. return s;
  35. }
  36. public List<Book> findAll(){
  37. return dao.selectAll();
  38. }
  39. public String remove(int bid){
  40. String s=dao.delectById(bid)>0?"删除成功":"删除失败";
  41. session.commit();
  42. return s;
  43. }
  44. public String change(Book book){
  45. String s=dao.update(book)>0?"修改成功":"修改失败";
  46. session.commit();
  47. return s;
  48. }
  49. }

typeservice

  1. package com.chinasofti.bookstore.service;
  2.  
  3. import com.chinasofti.bookstore.dao.TypesDao;
  4. import com.chinasofti.bookstore.entity.Types;
  5. import org.apache.ibatis.io.Resources;
  6. import org.apache.ibatis.session.SqlSession;
  7. import org.apache.ibatis.session.SqlSessionFactory;
  8. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  9.  
  10. import java.io.IOException;
  11. import java.util.List;
  12.  
  13. public class TypesService {
  14. private SqlSessionFactory factory;
  15. private SqlSession session;
  16. private TypesDao dao;
  17. public TypesService(){
  18. try {
  19. factory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("sqlconfig.xml"));
  20. session = factory.openSession();
  21. dao = session.getMapper(TypesDao.class);
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. System.out.println("创建失败");
  25. }
  26. }
  27. public String addType(String tname){
  28. Types t = dao.selectByName(tname);
  29. if (t!=null){
  30. return "该类别已存在,请重新输入";
  31. }
  32. if (dao.inserttype(tname)>0){
  33. session.commit();
  34. return "添加成功";
  35. }else {
  36. return "添加失败";
  37. }
  38. }
  39. public List<Types> findAll(){
  40. return dao.selectAll();
  41. }
  42. public String remove(int tid){
  43. String s=dao.deleteById(tid)>0?"删除成功":"删除失败";
  44. session.commit();
  45. return s;
  46. }
  47. public Types findByName(String tname){
  48. return dao.selectByName(tname);
  49. }
  50. }

5,util

  1. package com.chinasofti.bookstore.util;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class UserInput {
  6. //创造用于接收用户输入整数的方法
  7. public static int getInt(String wang){
  8. System.out.println(wang);
  9. while (true){
  10. Scanner sc=new Scanner(System.in);
  11. try {
  12. return sc.nextInt();
  13. }
  14. catch (Exception e){
  15. System.out.println("对不起,输入格式不正确,请重新输入");
  16. }
  17. }
  18. }
  19. public static double getDouble(String wang){
  20. System.out.println(wang);
  21. while (true){
  22. Scanner sc=new Scanner(System.in);
  23. try {
  24. return sc.nextDouble();
  25. }
  26. catch (Exception e){
  27. System.out.println("对不起,输入格式不正确,请重新输入");
  28. }
  29. }
  30. }
  31. public static String getString(String wang){
  32. System.out.println(wang);
  33. Scanner sc=new Scanner(System.in);
  34. return sc.next();
  35.  
  36. }
  37. }

6,view

  1. package com.chinasofti.bookstore.view;
  2.  
  3. import com.chinasofti.bookstore.entity.Types;
  4.  
  5. import java.util.List;
  6.  
  7. /*输出界面
  8. * 图书管理
  9. 1、添加图书类别
  10. 2、显示图书类别
  11. 3、删除图书类别
  12. 4、添加图书
  13. 5、查询图书
  14. 6、删除图书
  15. 7、修改图书
  16. 8、根据类别查询图书*/
  17. public class View {
  18. public static void welcome(){
  19. System.out.println("---------欢迎来到我的世界--------------");
  20. System.out.println("1、添加图书类别");
  21. System.out.println("2、显示图书类别");
  22. System.out.println("3、删除图书类别");
  23. System.out.println("4、添加图书");
  24. System.out.println("5、查询图书");
  25. System.out.println("6、删除图书");
  26. System.out.println("7、修改图书");
  27. System.out.println("8、根据类别查询图书");
  28. System.out.println("---------------------------------------");
  29. }
  30. public static void showType(List<Types> all){
  31. System.out.println("所有类别如下");
  32. System.out.println("编号\t名称");
  33. for (Types t:all){
  34. System.out.println(t.getTid()+"\t"+t.getTname());
  35. }
  36. }
  37. }

7,control(业务逻辑只写了一个,其余的依葫芦画瓢就好)

  1. package com.chinasofti.bookstore.control;
  2.  
  3. import com.chinasofti.bookstore.entity.Book;
  4. import com.chinasofti.bookstore.entity.Types;
  5. import com.chinasofti.bookstore.service.BookService;
  6. import com.chinasofti.bookstore.service.TypesService;
  7. import com.chinasofti.bookstore.util.UserInput;
  8. import com.chinasofti.bookstore.view.View;
  9.  
  10. import java.util.List;
  11.  
  12. public class Control {
  13. private BookService service;
  14. private TypesService typesService;
  15. public Control() {
  16. this.service = new BookService();
  17. this.typesService=new TypesService();
  18. }
  19.  
  20. public void start(){
  21. //显示主界面
  22. View.welcome();
  23. //接收用户输入的指令
  24. int select = UserInput.getInt("请选择");
  25. if (select<=0){
  26. System.out.println("欢迎下次再来,886");
  27. System.exit(0);
  28. }else if(select==4){
  29. this.addbook();
  30. }
  31. }
  32.  
  33. private void addbook() {
  34. System.out.println("-----------请添加图书--------------");
  35. View.showType(this.typesService.findAll());
  36. System.out.println(this.service.addBook(new Book(
  37. UserInput.getString("请输入书名"),
  38. UserInput.getString("请输入图书的作者"),
  39. UserInput.getString("请输入图书的描述"),
  40. UserInput.getInt("请输入图书的价格"),
  41. UserInput.getInt("请输入图书的数量"),
  42. typesService.findByName(UserInput.getString("请输入类别名称"))
  43. )));
  44. }
  45. }

基于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. 基于 HTML5 WebGL + WebVR 的 3D 虚实现实可视化培训系统

    前言 2019 年 VR, AR, XR, 5G, 工业互联网等名词频繁出现在我们的视野中,信息的分享与虚实的结合已经成为大势所趋,5G 是新一代信息通信技术升级的重要方向,工业互联网是制造业转型升级 ...

  2. javascript-void keyword

    javascript-void keyword 写在前面 ECMA-262定义了ECMAScript所支持的关键字(keyword),关键字不能用作ECMAScript程序的标识符(Indetifie ...

  3. (三)unittest断言方法的介绍

    断言如同在测试用例上,类似于预期结果与实际结果是否一致,如果一致则表示测试通过,Assert断言很好的用于测试结果判断上,更灵活的对预期结果和实际结果进行对比,下面简单的介绍一下unittest的As ...

  4. SpringBoot2 整合 Zookeeper组件,管理架构中服务协调

    本文源码:GitHub·点这里 || GitEE·点这里 一.Zookeeper基础简介 1.概念简介 Zookeeper是一个Apache开源的分布式的应用,为系统架构提供协调服务.从设计模式角度来 ...

  5. AcWing 243. 一个简单的整数问题2 | 树状数组

    传送门 题目描述 给定一个长度为N的数列A,以及M条指令,每条指令可能是以下两种之一: 1.“C l r d”,表示把 A[l],A[l+1],…,A[r] 都加上 d. 2.“Q l r”,表示询问 ...

  6. JPA或Hibernate中使用原生SQL实现分页查询、排序

    发生背景:前端展示的数据需要来自A表和D表拼接,A表和D表根据A表的主键进行关联,D表的非主键字段关联C表的主键,根据条件筛选出符合的数据,并且根据A表的主键关联B表的主键(多主键)的条件,过滤A表中 ...

  7. es lucene搜索及聚合流程源码分析

    本文以TermQuery,GlobalOrdinalsStringTermsAggregator为例,通过代码,分析es,lucene搜索及聚合流程.1:协调节点收到请求后,将search任务发到相关 ...

  8. Go Goosy Disk Docker Port Provisioners(GDP)

    小伙伴们,她们中出了一个叛徒,他是谁?是谁?是谁? 由一则口口相传的故事开始吧: 中午吃饭时间抽空小李跑到同座大楼的小张公司串门,小李是一名docker顾问熟称砖家,这间公司老板想挖小李,他盯了前台不 ...

  9. Ubuntu16安装NVIDIA驱动后重复登录 简单粗暴

    第一步 卸载所有NVIDIA的东西 第二步 开机,应该能进入默认驱动的桌面了,在设置里关闭开机密码,开机自动登录 第三步 安装英伟达驱动

  10. 《C# 爬虫 破境之道》:第一境 爬虫原理 — 第六节:第一境尾声

    在第一境中,我们主要了解了爬虫的一些基本原理,说原理也行,说基础知识也罢,结果就是已经知道一个小爬虫是如何诞生的了~那么现在,请默默回想一下,在第一境中,您都掌握了哪些内容?哪些还比较模糊?如果还有什 ...