基于Mybatis的bookstore架构模型
总共分为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架构模型的更多相关文章
- JVM笔记 -- JVM的发展以及基于栈的指令集架构
2011年,JDK7发布,1.7u4中,开始启用新的垃圾回收器G1(但是不是默认). 2017年,发布JDK9,G1成为默认GC,代替CMS.(一般公司使用jdk8的时候,会通过参数,指定GC为G1) ...
- MFC主窗口架构模型
根据主窗口类型,MFC软件工程可以分为一下几种架构模型: 1.SDI(Simple Document Interface)单文档界面,一个主窗口下只编辑一份文档 2.MDI(Multiple Docu ...
- 高扩展的基于NIO的服务器架构
当你考虑写一个扩展性良好的基于Java的服务器时,相信你会毫不犹豫地使用Java的NIO包.为了确保你的服务器能够健壮.稳定地运行,你可能会花大量的时间阅读博客和教程来了解线程同步的NIO selec ...
- Unity3D中的AI架构模型
我们都知道现在AI(由人工制造出来的系统所表现出来的模拟人类的智能活动)非常的火,可以说是家喻户晓.当然,在游戏中,AI也是到处可以找到的,对于AI,我们应该关注的问题是如何让游戏角色能够向人或动物那 ...
- 基于hadoop的BI架构
BI系统,是企业利用数据驱动运营的一个典型系统.BI系统通过发掘企业运行过程中的数据,发现企业的潜在风险.为企业的各项决策提供数据支撑. 传统的BI系统通常构建于关系型数据库之上.随着企业业务量的增大 ...
- 【神经网络篇】--基于数据集cifa10的经典模型实例
一.前述 本文分享一篇基于数据集cifa10的经典模型架构和代码. 二.代码 import tensorflow as tf import numpy as np import math import ...
- 【深度学习篇】--神经网络中的池化层和CNN架构模型
一.前述 本文讲述池化层和经典神经网络中的架构模型. 二.池化Pooling 1.目标 降采样subsample,shrink(浓缩),减少计算负荷,减少内存使用,参数数量减少(也可防止过拟合)减少输 ...
- 基于SOA的银行系统架构
Part-1 [简述] 1.通过引入面向服务架构(SOA),企业服务总线(ESB),适配器(Adapter)及面向构件等技术,尝试打造一个统一业务流程服务平台,实现面向流程的服务集成. 2.传统银行 ...
- Shuttle ESB(三)——架构模型介绍(2)
上一篇文章中,介绍了Shuttle ESB架构模型中的三个重要部分. 今天,我们继续介绍剩余的三个内容:模式和消息路由. 四.模式 Request/Response(请求/响应模式) 对基于Reque ...
随机推荐
- 「JOISC 2014 Day1」历史研究 --- 回滚莫队
题目又臭又长,但其实题意很简单. 给出一个长度为\(N\)的序列与\(Q\)个询问,每个询问都对应原序列中的一个区间.对于每个查询的区间,设数\(X_{i}\)在此区间出现的次数为\(Sum_{X_{ ...
- 1072 开学寄语 (20分)C语言
下图是上海某校的新学期开学寄语:天将降大任于斯人也,必先删其微博,卸其 QQ,封其电脑,夺其手机,收其 ipad,断其 wifi,使其百无聊赖,然后,净面.理发.整衣,然后思过.读书.锻炼.明智.开悟 ...
- 实现antd下拉框动态添加内容(与数据库交互)
antd下拉控件的动态内容添加(与数据库交互) antd这个框架给开发带来了极大的方便,但同时,我认为还有一些不方便的地方:常用的逻辑在文档中没有体现.需要前端开发经验的人才能快速上手,而我刚刚接触这 ...
- nginx 负载均衡的配置
首先搭建好三台nginx,我是用VM搭建的 nginx搭建,https://www.cnblogs.com/liubaoqing/p/10507962.html 这里的三台nginx ,ip分别是 1 ...
- POJ 2456 Aggressive cows (二分)
题目传送门 POJ 2456 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) s ...
- input值
input里面的值为字符串(string)类型,所以用作数的计算的时候需要用Number(mInput.value)进行转换成数值Numbei()类型才可以计算 例如: mInput1.value + ...
- DevOps is Hard、DevSecOps is Even Harder . --- Enterprise Holdings
Enterprise Holdings. 的IT团队超过2000人,在2018年的演讲中介绍了Enterprise Holdings的DevOps是如何转型的.我们通过打造一个不只包涵了pipelin ...
- js中排序方法sort() 和 reverse()
reverse() 作用:反转原数组. 用法: array.reverse(); 图解: sort() 作用:对原数组进行排序.默认将每个数组项 先 转换为字符串 再 进行字符串对比后升序排序. 用法 ...
- python中end=''
end = '' 用于连接下一条的print输出内容 效果图: 代码: # end='' 用于连接下一条输出语句 print('哈哈哈') print('嘻嘻嘻') print('\n\n') pr ...
- RSA 的加密 解密
RSA加密解密类: package me.hao0.trace.order; import java.io.BufferedReader; import java.io.BufferedWriter; ...