MyBatis的简单应用

基本信息

	MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java对象)映射成数据库中的记录。

增删改查

架包
在这里插入图片描述
配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--    声明数据库连接配置-->
    <environments default="development">
<!--        一套开发者环境-->
        <environment id="development">
<!--            默认JDBC事物管理-->
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/student"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="net/zr/mapper/StudentMapper.xml"/> 存放sql语句的文件的位置
    </mappers>
</configuration>

实体类层就按照自己的数据库完成

mapper层

<?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="StudentMapper">
    <insert id="addUser" parameterType="net.zr.bean.Student">
--         insert into user (username,password,sex,age)values ('屠弋辉','123456','女','12')
        insert into user (username,password,sex,age)values (#{username},#{password},#{sex},#{age})


    </insert>
    <delete id="delUser" parameterType="int" >
        delete from user where id=#{id}
    </delete>
    <update id="upUser" parameterType="net.zr.bean.Student">
        update user set username=#{username},password=#{password},sex=#{sex},age=#{age} where id=#{id}
    </update>
    <select id="fingByid" parameterType="int" resultType="net.zr.bean.Student">
        select * from user where id=#{id}
    </select>
    <select id="fingAll" resultType="net.zr.bean.Student" >
        select * from user
    </select>



</mapper>

测试层

package net.zr.test;

import net.zr.bean.Student;
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.io.InputStream;
import java.util.List;
import java.util.Scanner;

public class TestStudent {

    public static void main(String[] args) throws IOException {
        Scanner scanner=new Scanner(System.in);
        while (true) {
            showMenu();
            System.out.println("请输入你需要的功能");
            int n = scanner.nextInt();
            if (n == 6) {
                System.out.println("已退出");
                break;
            }
            switch (n) {
                case 1:
                    addStu();
                    break;
                case 2:
                    delStu();
                    break;
                case 3:
                    findById();
                    break;
                case 4:
                    fingAll();
                    break;
                case 5:
                    updateStu();
                    break;
                default:
                    System.out.println("输入错误");
                    break;
            }
        }

    }

    private static void updateStu() throws IOException {
        Scanner scanner=new Scanner(System.in);
        //1.先获取会话工厂构造器
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        //指定连接配置文件
        InputStream is= Resources.getResourceAsStream("net/zr/SqlMapConfig.xml");
        //2.根据构造器,构造会话工厂
        SqlSessionFactory factory=builder.build(is);

        //3.开启连接会话
        SqlSession session = factory.openSession();
        System.out.println("请输入姓名");
        String username =scanner.next();
        System.out.println("请输入密码");
        String password=scanner.next();
        System.out.println("请输入性别");
        String sex= scanner.next();
        System.out.println("请输入年龄");
        int age=scanner.nextInt();
        System.out.println("请输入你需要修改的ID号");
        int id=scanner.nextInt();
        Student student = new Student(username,password,sex,age,id);
        int i = session.update("StudentMapper.upUser",student);
        System.out.println(i);
        //手动提交
        session.commit();
        session.close();

    }

    private static void fingAll() throws IOException {
        Scanner scanner=new Scanner(System.in);
        //1.先获取会话工厂构造器
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        //指定连接配置文件
        InputStream is= Resources.getResourceAsStream("net/zr/SqlMapConfig.xml");
        //2.根据构造器,构造会话工厂
        SqlSessionFactory factory=builder.build(is);

        //3.开启连接会话
        SqlSession session = factory.openSession();
        List<Student> i = session.selectList("StudentMapper.fingAll");
        for (Student stu:i){
            System.out.println(
                    stu
            );
        }
        //手动提交
        session.commit();
        session.close();

    }



    private static void findById() throws IOException {
        Scanner scanner=new Scanner(System.in);
        //1.先获取会话工厂构造器
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        //指定连接配置文件
        InputStream is= Resources.getResourceAsStream("net/zr/SqlMapConfig.xml");
        //2.根据构造器,构造会话工厂
        SqlSessionFactory factory=builder.build(is);

        //3.开启连接会话
        SqlSession session = factory.openSession();
        System.out.println("请输入需要查找的ID号");
        int id=scanner.nextInt();
        Student i = session.selectOne("StudentMapper.fingByid",id);
        System.out.println("ID"+i.getId()+"用户名"+i.getUsername()+"密码"+i.getPassword()+"性别"+i.getSex()+"年龄"+i.getAge());
        //手动提交
        session.commit();
        session.close();
    }

    private static void delStu() throws IOException {
        Scanner scanner=new Scanner(System.in);
        //1.先获取会话工厂构造器
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        //指定连接配置文件
        InputStream is= Resources.getResourceAsStream("net/zr/SqlMapConfig.xml");
        //2.根据构造器,构造会话工厂
        SqlSessionFactory factory=builder.build(is);

        //3.开启连接会话
        SqlSession session = factory.openSession();
        System.out.println("请输入需要删除的ID号");
        int id=scanner.nextInt();
        int i = session.delete("StudentMapper.delUser",id);
        System.out.println(i);
        //手动提交
        session.commit();
        session.close();

    }

    private static void addStu() throws IOException {
        Scanner scanner=new Scanner(System.in);
        //1.先获取会话工厂构造器
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        //指定连接配置文件
        InputStream is= Resources.getResourceAsStream("net/zr/SqlMapConfig.xml");
        //2.根据构造器,构造会话工厂
        SqlSessionFactory factory=builder.build(is);

        //3.开启连接会话
        SqlSession session = factory.openSession();
        System.out.println("请输入姓名");
        String username =scanner.next();
        System.out.println("请输入密码");
        String password=scanner.next();
        System.out.println("请输入性别");
        String sex= scanner.next();
        System.out.println("请输入年龄");
        int age=scanner.nextInt();

        Student student = new Student(username,password,sex,age);
        int i = session.insert("StudentMapper.addUser",student);
        System.out.println(i);
        //手动提交
        session.commit();
        session.close();
    }

    private static void showMenu() {
        System.out.println("欢迎使用屠吉吉管理的系统");
        System.out.println("1,添加所有");
        System.out.println("2,删除用户");
        System.out.println("3,根据id查询用户");
        System.out.println("4,查询所有用户");
        System.out.println("5,修改用户");
        System.out.println("6,退出");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值