目录
MyBatis框架的三种方式用于连接数据库的增伤改查这里采用三种方式.在学会MyBatis基础上进行练习.方案一:普通的文件配置 在配置文件中编写sql语句。方案二:利用接口加上 文件配置操练Sql语句方案三:利用MyBatis的注解的方式操作数据库.这里采用了Map集合的方式.有兴趣的可以利用实体类的方式操作数据库的增删改查.在这里是复习MyBatis框架的基础操作。
第一种方案利用MaBatis中接口+普通的文件配置方式。
运行结果
第二种方案利用MaBatis中普通的文件配置方式。
编辑
第三种方案利用MaBatis中注解的方式。
第一步:这里采用了一个框架MyBatis.用途是从数据库中的数据增伤改查的信息访问到控制台。
第二步:这里采用Spring框架创建对象。
第三步:利用SpringMvc框架作为控制层进行判断与页面的跳转.
MyBatis框架的三种方式用于连接数据库的增伤改查
这里采用三种方式.在学会MyBatis基础上进行练习.
方案一:普通的文件配置 在配置文件中编写sql语句。
方案二:利用接口加上 文件配置操练Sql语句
方案三:利用MyBatis的注解的方式操作数据库.
这里采用了Map集合的方式.有兴趣的可以利用实体类的方式操作
数据库的增删改查.
在这里是复习MyBatis框架的基础操作。
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for book -- ---------------------------- DROp TABLE IF EXISTS `book`; CREATE TABLE `book` ( `id` int NOT NULL AUTO_INCREMENT COMMENT '书籍id', `bookNumber` int NULL DEFAULT NULL COMMENT '书籍编号', `bookName` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '书籍名称', `bookAuthor` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '书籍的作者', `bookPrice` decimal(10, 2) NULL DEFAULT NULL COMMENT '书籍的价格', `bookCount` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '书籍的数量', `bookEdic` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '书籍出版社', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1001028 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of book -- ---------------------------- INSERT INTO `book` VALUES (1, 1002001, '《愿这世间美好与你缓缓相扣!》', '易小冉姐', 35.00, '42', '中国出版社'); INSERT INTO `book` VALUES (3, 1002003, '《自律的人生更自由》', '小脑子', 100.00, '145', '无故选出版社'); INSERT INTO `book` VALUES (4, 1002004, '《管理好情绪》', '融者', 37.00, '78', '古林文历史出版社'); INSERT INTO `book` VALUES (5, 1002005, '《世界上最好的24课》', '哈哪儿', 49.00, '56', '中华工商和出版社'); INSERT INTO `book` VALUES (6, 1002006, '《学习级简法》', '北大博士', 22.00, '56', '北京联合出版公司'); INSERT INTO `book` VALUES (7, 1002007, '《认知与觉醒》', '周立功', 34.00, '78', '北京联合出版公司'); INSERT INTO `book` VALUES (8, 1002008, '《心静》', '马云', 54.00, '76', '青岛出版社'); INSERT INTO `book` VALUES (9, 1002009, '《活成自己喜欢的样子》', '华子', 1009.00, '706', '吉林出版社'); INSERT INTO `book` VALUES (10, 1002010, '《谢谢你今天不行》', '胡父子', 100.00, '706', '中国大学有限公司'); INSERT INTO `book` VALUES (11, 10020011, '《人生》', '路遥', 109.00, '706', '吉林出版社'); INSERT INTO `book` VALUES (12, 1002012, '《自立》', '胡身子', 102.00, '706', '中国大学有限公司'); INSERT INTO `book` VALUES (13, 10020013, '《把生活过成你想要的样子》', '资理财', 109.00, '706', '江西出版社'); INSERT INTO `book` VALUES (14, 1002014, '《人生》', '患者', 102.00, '736', '上海出版社'); INSERT INTO `book` VALUES (15, 10020015, '《三国演义》', '漏光中', 109.00, '706', '江西出版社'); INSERT INTO `book` VALUES (16, 1002016, '《西游记》', '师爷', 102.00, '543', '上饶出版社'); INSERT INTO `book` VALUES (17, 10020017, '《你想要的那种人生》', '', 105.00, '406', '上饶出版社'); INSERT INTO `book` VALUES (18, 1002018, '《自由使你快乐》', '自由使者', 102.00, '567', '南京出版社'); INSERT INTO `book` VALUES (19, 10020019, '《梦话红楼》', '梦静的兰花', 129.00, '746', '江西出版社'); INSERT INTO `book` VALUES (20, 1002020, '《飞过软件》', '师爷', 112.00, '533', '上哈出版社'); INSERT INTO `book` VALUES (21, 10020017, '《你想要的那种人生》', '', 105.00, '406', '上饶出版社'); INSERT INTO `book` VALUES (22, 1002018, '《自由使你快乐》', '自由使者', 102.00, '567', '南京出版社'); INSERT INTO `book` VALUES (23, 1001023, '《你我从的人生一闪而过了吧!》', '王小二', 23.00, '46', '上饶出版社'); INSERT INTO `book` VALUES (24, 1002020, '《飞过软件》', '师爷', 112.00, '533', '上哈出版社'); INSERT INTO `book` VALUES (25, 10020017, '《你想要的那种人生》', '', 105.00, '406', '上饶出版社'); INSERT INTO `book` VALUES (26, 1002018, '《自由使你快乐》', '自由使者', 102.00, '567', '南京出版社'); INSERT INTO `book` VALUES (27, 10020019, '《梦话红楼》', '梦静的兰花', 129.00, '746', '江西出版社'); INSERT INTO `book` VALUES (28, 1002020, '《飞过软件》', '师爷', 112.00, '533', '上哈出版社'); INSERT INTO `book` VALUES (29, 1001021, '《MySql从入门到高级》', '胡先生', 345.00, '234', '九江出版社'); INSERT INTO `book` VALUES (1001031, 1001023, '《你我从的人生一闪而过了吧!》', '王小二', 23.00, '46', '上饶出版社'); SET FOREIGN_KEY_CHECKS = 1;
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for student -- ---------------------------- DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `id` int NOT NULL AUTO_INCREMENT COMMENT '学生的id', `studentNumber` varchar(23) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '学生的编号', `studentName` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '学生的姓名', `studentSex` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '学生的性别', `studnentAge` int NOT NULL COMMENT '学生的年龄', `studentAddress` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '学生的家庭地址', `studentClass` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '学生所在的班级', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1002017 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of student -- ---------------------------- INSERT INTO `student` VALUES (1, '202205161002001', '梦雨泽', '男', 23, '江西', '22级软件三班'); INSERT INTO `student` VALUES (2, '20220516100200', '梦天右', '女', 22, '南昌', '22级软件一班'); INSERT INTO `student` VALUES (3, '2022051610020016', '你是我的唯一', '男', 23, '江西', '22级软件三班'); INSERT INTO `student` VALUES (4, '2022051610020016', '你我是我的唯一', '男', 13, '江西九江', '22级软件三班'); INSERT INTO `student` VALUES (5, '202205161002', '我修改了五个字段', '女', 24, '江西南昌', '22级软件三班'); INSERT INTO `student` VALUES (6, '202205161002006', '胡浩然', '男', 24, '江西南昌', '22级软件三班'); INSERT INTO `student` VALUES (7, '202205161002006', '马尾草书', '女', 23, '北京市', '22级软件三班'); INSERT INTO `student` VALUES (8, '202205161002008', '胡博士', '女', 43, '南昌', '21级软件三班'); INSERT INTO `student` VALUES (9, '202205161002009', '胡有为', '女', 22, '湖南', '12级软件三班'); INSERT INTO `student` VALUES (10, '202205161002010', '胡二强', '男', 33, '上海市', '23级软件三班'); INSERT INTO `student` VALUES (11, '2022051610020011', '胡柏钊', '女', 23, '南昌', '21级软件三班'); INSERT INTO `student` VALUES (12, '2022051610020012', '胡自强', '女', 22, '湖南', '12级软件三班'); INSERT INTO `student` VALUES (13, '202205161002013', '胡二宝', '男', 33, '上海市', '23级软件三班'); INSERT INTO `student` VALUES (14, '2022051610020014', 'mybatis', '女', 23, '南昌', '22级电商'); INSERT INTO `student` VALUES (1002015, '2022051610020015', '哈喽', '男', 34, '九江', '22级电商1班'); INSERT INTO `student` VALUES (1002016, '2022051610020015', 'mybatis2', '男', 23, '南昌', '20级软件三班'); INSERT INTO `student` VALUES (1002017, '2022051610020016', 'ke', '男', 23, '湖口', '20级软件一班'); SET FOREIGN_KEY_CHECKS = 1;
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for teacher -- ---------------------------- DROP TABLE IF EXISTS `teacher`; CREATE TABLE `teacher` ( `id` int NOT NULL AUTO_INCREMENT, `teacherNumber` varchar(23) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL, `teacherName` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL, `teacherAge` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL, `teacherSex` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL, `teacherId` varchar(18) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '身份证', `teacherClass` varchar(20) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL, `teacherPhone` varchar(11) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL, `teacherAddress` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1003011 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of teacher -- ---------------------------- INSERT INTO `teacher` VALUES (1, 'Bill_20220516001', '锁芯', '34', '男', '360429200110070654', '22级软件三班', '18967853421', '南昌'); INSERT INTO `teacher` VALUES (2, 'Bill_20230304001', '风花雪夜', '23', '女', '36042920001009098', '22级软件二班', '19856743278', '江西'); INSERT INTO `teacher` VALUES (3, 'Bill_20200304001', '月梧桐絮', '34', '男', '37856720080706002', '22级软件二班', '17856783456', '上海'); INSERT INTO `teacher` VALUES (4, 'Bill_20200304002', '王一飞', '45', '女', '37856720080706003', '22级软件三班', '17856783456', '上海'); INSERT INTO `teacher` VALUES (5, 'Bill_20200304003', '细末', '35', '男', '37856721080706003', '23级软件三班', '17556783456', '上海'); INSERT INTO `teacher` VALUES (6, 'Bill_20200304004', '淡色', '25', '男', '37854720080706003', '22级软件一班', '17856783456', '杭州'); INSERT INTO `teacher` VALUES (7, 'Bill_20200304005', '只想一辈子', '67', '男', '36056720080706003', '22级软件三班', '17856483456', '南昌'); INSERT INTO `teacher` VALUES (8, 'Bill_20200304008', '花里三书籍', '34', '男', '37856720080706005', '22级软件二班', '17856783456', '上海'); INSERT INTO `teacher` VALUES (9, 'Bill_20200304009', '风华九代字逍遥', '38', '男', '37856721080406003', '21级软件三班', '17556783456', '南昌'); INSERT INTO `teacher` VALUES (10, 'Bill_20200304010', '淡出一切', '24', '女', '37884720080706003', '22级软件一班', '17856783456', '杭州'); INSERT INTO `teacher` VALUES (11, 'Bill_20200304011', '只想一辈子哈', '67', '男', '36056720080706003', '22级软件三班', '17859483456', '南昌'); SET FOREIGN_KEY_CHECKS = 1;
第一种方案利用MaBatis中接口+普通的文件配置方式。
//数据库的连接池 driver=com.mysql.cj.jdbc.Driver //数据库的地址 url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8 //数据库的姓名 user=root //数据库的密码 password=123456
package Com.Student.Dao;
import java.util.List;
import java.util.Map;
public interface IStudentDao {
//1
int getStudentCount();
//2
String getStudentName(int id);
//3
List
package Com.Student.Test;
import Com.Student.Dao.IStudentDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import javax.naming.InitialContext;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class StudentTest1 {
public SqlSession ss = null;
private void init() {
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
ss = new SqlSessionFactoryBuilder().build(is).openSession();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void getStudentCount() {
init();
IStudentDao dao = ss.getMapper(IStudentDao.class);
int count = dao.getStudentCount();
System.out.println("查序学生的数量为" + count + "人");
}
@Test
public void getStudentName() {
init();
IStudentDao dao = ss.getMapper(IStudentDao.class);
String studentName = dao.getStudentName(2);
System.out.println("查询到的数值为" + studentName);
}
@Test
public void getStudentAll() {
init();
IStudentDao dao = ss.getMapper(IStudentDao.class);
List> students = dao.getStudentAll();
System.out.println("查询所有的学生信息");
students.forEach(map -> System.out.println(map));
}
@Test
public void getStudentAll1() {
init();
IStudentDao dao = ss.getMapper(IStudentDao.class);
List> students = dao.getStudentAll1();
System.out.println("查询所有的学生信息studentNumber学生编号");
students.forEach(map -> System.out.println(map));
}
@Test
public void getStudentAll2() {
init();
IStudentDao dao = ss.getMapper(IStudentDao.class);
List> students = dao.getStudentAll2();
System.out.println("查询所有的学生信息studentNumber学生编号");
students.forEach(map -> System.out.println(map));
}
@Test
public void getStudentAll3() {
init();
IStudentDao dao = ss.getMapper(IStudentDao.class);
List> students = dao.getStudentAll3();
System.out.println("查询所有的学生信息的四个字段");
students.forEach(map -> System.out.println(map));
}
@Test
public void getStudentAll4() {
init();
IStudentDao dao = ss.getMapper(IStudentDao.class);
List> students = dao.getStudentAll4();
System.out.println("查询所有的学生信息的六个字段");
students.forEach(map -> System.out.println(map));
}
@Test
public void getStudentOrder() {
init();
IStudentDao dao = ss.getMapper(IStudentDao.class);
List> students = dao.getStudentOrder();
students.forEach(map -> System.out.println(map));
}
//增加一条数据
//studentNumber studentName studentSex studnentAge studentAddress studentClass
@Test
public void AddStudent() {
init();
Map map = new HashMap<>();
map.put("studentNumber", "2022051610020015");
map.put("studentName", "mybatis2");
map.put("studentSex", "男");
map.put("studnentAge", 23);
map.put("studentAddress", "南昌");
map.put("studentClass", "20级软件三班");
IStudentDao dao = ss.getMapper(IStudentDao.class);
int i = dao.AddStudent(map);
ss.commit();
if (i > 0) {
System.out.println("用户成功增加了一条数据");
} else {
System.out.println("用户增加的一条记录失败请查看代码哪里出来问题");
}
}
@Test
public void UpdateStudentOne() {
init();
Map map = new HashMap<>();
IStudentDao dao = ss.getMapper(IStudentDao.class);
//studentNumber=#{studentNumber}
map.put("id", 2);
map.put("studentNumber", "20220516100200");
int i = dao.UpdateStudentOne(map);
ss.commit();
if (i > 0) {
System.out.println("恭喜你成功修改了一个字段成功的修改了一条数据");
}
}
@Test
public void UpdateStudent() {
init();
Map map = new HashMap<>();
IStudentDao dao = ss.getMapper(IStudentDao.class);
//studentNumber=#{studentNumber},studentName=#{studentName}
map.put("id", 3);
map.put("studentNumber", "2022051610020016");
map.put("studentName", "你是我的唯一");
int i = dao.UpdateStudent(map);
ss.commit();
if (i > 0) {
System.out.println("恭喜你成功修改了两个字段成功的修改了一条数据");
}
}
@Test
public void UpdateStudent1() {
init();
Map map = new HashMap<>();
IStudentDao dao = ss.getMapper(IStudentDao.class);
//studentNumber=#{studentNumber},studentName=#{studentName} studentSex=#{studentSex}
map.put("id", 4);
map.put("studentNumber", "2022051610020016");
map.put("studentName", "你我是我的唯一");
map.put("studentSex", "男");
int i = dao.UpdateStudent1(map);
ss.commit();
if (i > 0) {
System.out.println("恭喜你成功修改了三个字段成功的修改了一条数据");
}
}
@Test
public void UpdateStudent2() {
init();
Map map = new HashMap<>();
IStudentDao dao = ss.getMapper(IStudentDao.class);
//studentNumber=#{studentNumber},studentName=#{studentName} studentSex=#{studentSex}studnentAge studentAddress
map.put("id", 5);
map.put("studentNumber", "202205161002");
map.put("studentName", "我修改了五个字段");
map.put("studentSex", "女");
map.put("studnentAge", 24);
map.put("studentAddress", "江西南昌");
int i = dao.UpdateStudent2(map);
ss.commit();
if (i > 0) {
System.out.println("恭喜你成功修改了五个字段成功的修改了一条数据");
}
}
@Test
public void UpdateStudent3() {
init();
Map map = new HashMap<>();
IStudentDao dao = ss.getMapper(IStudentDao.class);
//studentNumber=#{studentNumber},studentName=#{studentName} studentSex=#{studentSex}studnentAge studentAddress
map.put("id", 5);
map.put("studentNumber", "202205161002");
map.put("studentName", "我修改了五个字段");
map.put("studentSex", "女");
map.put("studnentAge", 24);
map.put("studentAddress", "江西南昌");
map.put("studentClass", "23级软件二班");
int i = dao.UpdateStudent3(map);
ss.commit();
if (i > 0) {
System.out.println("恭喜你成功修改了六个字段成功的修改了一条数据");
}
}
@Test
//选择器删除语句、
public void deleteStudent() {
init();
Map map = new HashMap<>();
map.put("id1", 0);
map.put("id2", 1);
IStudentDao dao = ss.getMapper(IStudentDao.class);
int i = dao.deleteStudent(map);
ss.commit();
if (i > 0) {
System.out.println("恭喜你成功删除了0-1的数据内容");
} else {
System.out.println("删除失败");
}
}
}
运行结果
第二种方案利用MaBatis中普通的文件配置方式。
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
user=root
password=123456
select count(*) from book
select * from book
select bookNumber,bookAuthor,bookCount from book
select bookName from book where bookAuthor=#{bookAuthor} and bookPrice=#{bookPrice}
select bookName from book where bookAuthor=#{bookAuthor} and bookPrice=#{bookPrice}
select bookName from book where id=#{id}
select bookNumber,bookName,bookAuthor,bookPrice,bookEdic from book where id=#{id}
INSERT INTO `test`.`book`(`bookNumber`, `bookName`, `bookAuthor`, `bookPrice`, `bookCount`, `bookEdic`)
VALUES(#{bookNumber},#{bookName},#{bookAuthor},#{bookPrice},#{bookCount},#{bookEdic});
UPDATE `test`.`book` SET
bookNumber=#{bookNumber},
bookName=#{bookName},
bookAuthor=#{bookAuthor},
bookPrice=#{bookPrice},
bookCount=#{bookCount},
bookEdic=#{bookEdic}
WHERe id=#{id}
delete from book where bookName=#{bookName}
package Com.Book.Test;
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 org.junit.Test;
import org.w3c.dom.ls.LSOutput;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class OneTest {
SqlSession ss = null;
public void init() {
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(is);
ss = factory.openSession();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void getBookNamber() {
init();
int i = ss.selectOne("Com.Book.Dao.Mapper.getBookNamber");
System.out.println("从数据库中查询到表中的数据数量为《" + i + "》本");
}
@Test
public void getBookAll() {
init();
//返回的类型为list中调用map集合
List> books = ss.selectList("Com.Book.Dao.Mapper.getBookAll");
//返回的类型为List中调用Map集合
books.forEach(map -> System.out.println(map));
}
@Test
public void getBookAll1() {
init();
//返回的类型为list中调用map集合
List> books = ss.selectList("Com.Book.Dao.Mapper.getBookAll1");
//返回的类型为List中调用Map集合
System.out.println("查询指定的字段内容");
books.forEach(map -> System.out.println(map));
}
@Test
public void getBookName1() {
init();
Map map = new HashMap<>();
//
map.put("bookAuthor", "华子");
map.put("bookPrice", " 1009.00");
String bookName = ss.selectOne("Com.Book.Dao.Mapper.getBookName1", map);
System.out.println("查到的书籍名称为:" + bookName);
}
@Test
public void getBookName2() {
init();
Map map = new HashMap<>();
map.put("bookAuthor", "北大博士");
map.put("bookPrice", " 22.00");
String bookName1 = ss.selectOne("Com.Book.Dao.Mapper.getBookName2", map);
System.out.println("查到的书籍名称为:" + bookName1);
}
@Test
public void getBookId() {
init();
String bookName = ss.selectOne("Com.Book.Dao.Mapper.getBookId", 2);
System.out.println("测试数据一从数据库中查到的书籍名称为" + bookName);
}
@Test
public void getBookId1() {
init();
String bookName = ss.selectOne("Com.Book.Dao.Mapper.getBookId", 3);
System.out.println("测试数据二数据库中查到的书籍名称为" + bookName);
}
@Test
public void getBookId2() {
init();
String bookName = ss.selectOne("Com.Book.Dao.Mapper.getBookId", 9);
System.out.println("测试数据三数据库中查到的书籍名称为" + bookName);
}
@Test
public void getBookIdA() {
init();
Map map = ss.selectOne("Com.Book.Dao.Mapper.getBookIdA", 20);
System.out.println("测试数据一数据库中查到的书籍名称为" + map);
}
@Test
public void getBookIdA1() {
init();
Map map = ss.selectOne("Com.Book.Dao.Mapper.getBookIdA", 2);
System.out.println("测试数据一数据库中查到的书籍名称为" + map);
}
//增加图书的信息内容
@Test
public void getBookInsert() {
//调用init方法
init();
//我要转入过来的是一个字段一个值用Map集合的方式存放
Map map = new HashMap<>();
//map.put要增加的字段名
map.put("bookNumber", "1001022");
map.put("bookName", "《你从我的人生一闪而过了吧!》");
map.put("bookAuthor", "王大爷");
map.put("bookPrice", "234");
map.put("bookCount", 456);
map.put("bookEdic", "海南出版社");
int i = ss.insert("Com.Book.Dao.Mapper.getBookInsert", map);
ss.commit();
if (i > 0) {
System.out.println("恭喜用户成功增加一条用户的数据信息");
}
}
@Test
public void getBookUpdate() {
init();
Map map = new HashMap<>();
map.put("id", 23);
map.put("bookNumber", "1001023");
map.put("bookName", "《你我从的人生一闪而过了吧!》");
map.put("bookAuthor", "王小二");
map.put("bookPrice", "23");
map.put("bookCount", 46);
map.put("bookEdic", "上饶出版社");
int i = ss.insert("Com.Book.Dao.Mapper.getBookUpdate", map);
ss.commit();
if (i > 0) {
System.out.println("恭喜用户成功修改一条用户的数据信息变化值为一条记录");
}
}
//删除语句
@Test
public void getBookDelete() {
init();
//《你从我的人生一闪而过了吧!》
int i = ss.delete("Com.Book.Dao.Mapper.getBookDelete", "《你从我的人生一闪而过了吧!》");
ss.commit();
if (i > 0) {
System.out.println("恭喜用户成功删除了0d到1区间的数据信息变化值为一条记录");
}
}
}
第三种方案利用MaBatis中注解的方式。
package Com.Book.Dao;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
import java.util.Map;
public interface ITeacherDao {
//查询老师的数量
@Select(" select count(*) from teacher")
int getTeacher();
//查询编号为10的老师的姓名
@Select("select teacherName from teacher where id=#{id}")
String getteacherNameId(@Param("id") int id);
//查询所有的书籍信息
@Select(" select * from teacher")
List> getTeacherAll();
//查询教师名称为 风花雪夜 和 身份证号为 36042920001009098 teacherName teacherId 转入的两个参数
@Select(" select teacherNumber,teacherName,teacherId from teacher where teacherName=#{teacherName } and teacherAge=#{teacherAge}")
Map getTeacherinfo(@Param("teacherName") String teacherName, @Param("teacherAge") int teacherAge);
//查询指定名称的所有属性,除了名称外的 teacherNumber teacherAge teacherSex teacherId teacherClass teacherPhone
@Select("select teacherNumber,teacherAge ,teacherSex ,teacherId,teacherClass, teacherPhone from teacher where teacherName=#{teacherName}")
Map getTeacher2(@Param("teacherName") String teacherName);
//限制查询的语句
@Select("select * from teacher limit #{start},#{count}")
List> getTeacherLimit(@Param("start") int start, @Param("count") int count);
//增加一条老师的数据信息内容如何增加呢?先看数据库时如何增加的呢?
@Insert("INSERT INTO `test`.`teacher`" +
"(`teacherNumber`, `teacherName`, `teacherAge`, `teacherSex`, `teacherId`, `teacherClass`," +
" `teacherPhone`, `teacherAddress`)" +
"value (#{teacherNumber},#{teacherName},#{teacherAge},#{teacherSex},#{teacherId},#{teacherClass}," +
"#{teacherPhone},#{teacherAddress})")
int addTeacher(Map map);
}
package Com.Book.Test;
import Com.Book.Dao.ITeacherDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TwoTest {
public SqlSession ss = null;
public ITeacherDao dao = null;
public void init() {
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
ss = new SqlSessionFactoryBuilder().build(is).openSession();
dao = ss.getMapper(ITeacherDao.class);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void getTeacherNum() {
init();
int count = dao.getTeacher();
System.out.println("查询到老师的数量为" + count + "位");
}
@Test
public void getteacherNameId() {
init();
String teacherName=dao.getteacherNameId(6);
System.out.println("查询到老师的名称为"+teacherName);
}
@Test
public void getTeacherAll(){
init();
List> teachers=dao.getTeacherAll();
teachers.forEach(map -> System.out.println(map));
}
@Test
public void getTeacherinfo(){
init();
Map map=dao.getTeacherinfo("王一飞",45);
System.out.println(map);
}
@Test
public void getTeacher2(){
init();
Map map=dao.getTeacher2("只想一辈子");
System.out.println(map);
}
@Test
public void getTeacherLimit(){
init();
List> maps=dao.getTeacherLimit(1,4);
System.out.println("测试数据一");
maps.forEach(map -> System.out.println(map));
}
@Test
public void getTeacherLimit1(){
init();
List> maps=dao.getTeacherLimit(6,10);
System.out.println("测试数据二");
maps.forEach(map -> System.out.println(map));
}
@Test
public void addTeacher() {
init();
Map map = new HashMap<>();
map.put("teacherNumber", "BiLL_2022034013");
map.put("teacherName", "项目");
map.put("teacherAge", 23);
map.put("teacherSex", "女");
map.put("teacherId", "345789200310980987");
map.put("teacherClass", "24级软件三班");
map.put("teacherPhone", "12345678976");
map.put("teacherAddress", "上饶市区中心");
ss.commit();
int i=dao.addTeacher(map);
if(i>0){
System.out.println("恭喜用户成功增加一条用户的数据信息");
}
}
}



