栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MyBatis实操第一课5月17号.在学完了MaBatis框架后。

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MyBatis实操第一课5月17号.在学完了MaBatis框架后。

目录

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




    
    
    
    
    
        
            
            
            
            
                
                
                
                
            
        
    

    
    
        
    






    
    
        select studentName from student where id=#{id}
    

    
    
        select studentNumber from student
    
    
    
    
        select studentNumber,studentName,studentSex,studnentAge from student
    

    
    
        select *from student order by studentNumber desc
    

    
    
    
        insert into student(`studentNumber`, `studentName`, `studentSex`, `studnentAge`, `studentAddress`,
        `studentClass`)
        values (#{studentNumber},#{studentName},#{studentSex},#{studnentAge},#{studentAddress},#{studentClass})
    


    
    
        
        update student set studentNumber=#{studentNumber} where id=#{id}
    


    
    
        
        update student set studentNumber=#{studentNumber},studentName=#{studentName} where id=#{id}
    


    
    
        
        update student set studentNumber=#{studentNumber},studentName=#{studentName},
        studentSex=#{studentSex} where id=#{id}
    


    
    
        
        update student set studentNumber=#{studentNumber},studentName=#{studentName},
        studentSex=#{studentSex},studnentAge=#{studnentAge},studentAddress=#{studentAddress} where id=#{id}
    

    
    
    
        
        update student set studentNumber=#{studentNumber},studentName=#{studentName},
        studentSex=#{studentSex},studnentAge=#{studnentAge},studentAddress=#{studentAddress},studentClass=#{studentClass}
        where id=#{id}
    


    
    
        
        delete from book where id between #{id1} and #{id2}
    




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> getStudentAll();

    //4
    List> getStudentAll1();

    //5
    List> getStudentAll2();

    //6
    List> getStudentAll3();

    //7
    List> getStudentAll4();

    //8
    List> getStudentOrder();

    //9
    //
    int AddStudent(Map Map);

    //10
    int UpdateStudentOne(Map Map);

    //11
    int UpdateStudent(Map Map);

    //12
    int UpdateStudent1(Map Map);

    //13
    int UpdateStudent2(Map Map);

    //14
    int UpdateStudent3(Map Map);

    //15
    int deleteStudent(Map Map);


}
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 * from book
    


    
        select bookName from book where bookAuthor=#{bookAuthor} and bookPrice=#{bookPrice}
    


    
    
        select bookName from book where id=#{id}