IDEA-Maven-Mybatis基础框架搭建

搭建项目结构

创建Maven模块

在这里插入图片描述

在这里插入图片描述

搭建mysql数据库结构

1. 创建数据库

在这里插入图片描述

2. 新建数据表并添加字段和约束

在这里插入图片描述

3. 完成后查看数据表

在这里插入图片描述

搭建项目结构

1. 搭建包结构

在这里插入图片描述

2. 在pom.xml中引入项目依赖(坐标)

    会优先去本地仓库进行查找,已有的会进行提示在写的时候,写完记得刷新一下(右上角  没有的会去中央仓库进行下载,联网情况下 

在这里插入图片描述

3. 在resource目录下引入Mybatis.config配置文件和db.properties配置文件

在这里插入图片描述
在这里插入图片描述

driver=com.mysql.cj.jdbc.Driver  // mysql驱动
url=jdbc:mysql://localhost:3306/数据库名
username=用户名
password=密码

// 8.0.30 数据库连接是这样

在这里插入图片描述

    <properties resource="db.properties"></properties>
<!--    连接Mysql数据库-->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>

            </dataSource>
        </environment>
    </environments>

    <mappers>
<!--   SQL Mapper 接口所在包-->
        <package name="com.itheima.mapper"/>
        
    </mappers>
    <settings>
<!--        Mybatis默认日志-->
        <setting name="logImpl" value="stdout_logging"/>
    </settings>

在这里插入图片描述
在这里插入图片描述

搭建学员管理系统框架

1. 编写MybatisUtil工具类

public class MybatisUtil {

    // 保证在程序执行期间只有一个Factory (工厂模式生成session对象)
    private static SqlSessionFactory sqlSessionFactory;

    // 静态代码块来初始化   sqlSessionFactory
    static {

        try {
            InputStream in = Resources.getResourceAsStream("MybatisConfig.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    // 提供一个公告方法供 其它类获取session会话
    public static SqlSession opSession() {
        return sqlSessionFactory.openSession();
    }

    // 提交和关闭事务
    public static void commitAndClose(SqlSession sqlSession) {
        sqlSession.commit();
        sqlSession.close();
    }


}

2.编写 Student 实体类

public class Student {

    private Integer id;
    private String name;
    private String sex;
    private Integer age ;
    private Double height;

    public Student() {
    }

    public Student(Integer id, String name, String sex, Integer age, Double height) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.height = height;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", height=" + height +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Double getHeight() {
        return height;
    }

    public void setHeight(Double height) {
        this.height = height;
    }
}

3.编写Mapper接口类

public interface StuMapper {
    @Options(useGeneratedKeys = true,keyProperty = "id") // 开启主键,Stu的id接收
    @Insert("insert into student values(null,#{name},#{sex},#{age},#{height})")
    public void save(Student stu);

    @Select("select * from student")
    public ArrayList<Student> findAll();

    @Select("select * from student where id = #{id}")
    public Student findStuById(Integer id);

    @Update("update student set name = #{name}, sex = #{sex},age = #{age} , height = #{height} where id = #{id}")
    public void updateStu(Student stu);

    @Delete("delete from student where id =#{id}")
    public void deleteStu(Integer id);

}

4.搭建主方法框架


    public static void main(String[] args) {

        while (true) {

            System.out.println("********************************************************");
            System.out.println("1.增加学员   2.删除学员   3.修改学员   4.查询学员  5.退出 ");
            System.out.println("********************************************************");

            System.out.println("请选择您的选择:(1-5)");
            String op = sc.next();

            switch (op) {
                case "1":
                    addStu();
                    break;
                case "2":
                    deleteStu();
                    break;
                case "3":
                    updateStu();
                    break;
                case "4":
                    findAll();
                    break;
                case "5":
                    System.out.println("谢谢使用本系统,拜拜!");
                    System.exit(0);
                default:
                    System.out.println("您的输入有误!");
                    break;
            }
        }
    }

5.实现学员信息管理(增删改查)

①增加学员
  private static void addStu() {

        System.out.println("请输入姓名:");
        String name = sc.next();
        System.out.println("请输入性别:");
        String sex = sc.next();
        System.out.println("请输入年龄:");
        int age = sc.nextInt();
        System.out.println("请输入身高:");
        double height = sc.nextDouble();
        Student student = new Student();

        student.setName(name);
        student.setSex(sex);
        student.setAge(age);
        student.setHeight(height);

        SqlSession sqlSession = MybatisUtil.opSession();
        StuMapper mapper = sqlSession.getMapper(StuMapper.class);
        mapper.save(student);
        System.out.println("添加成功!主键id: " + student.getId());
        MybatisUtil.commitAndClose(sqlSession);



    }
② 删除学员
    private static void deleteStu() {

        findAll();
        System.out.println("请输入要修改的学员学号:");
        int id = sc.nextInt();
        SqlSession sqlSession = MybatisUtil.opSession();
        StuMapper mapper = sqlSession.getMapper(StuMapper.class);

        Student student = mapper.findStuById(id);

        if (student == null) {
            System.out.println("没有该学员信息!");
            return;
        }
        System.out.println("================================================================");
        System.out.println("----------------------------------------------------------------");
        System.out.println("学号\t\t姓名\t\t性别\t\t年龄\t\t身高");

        System.out.println(student.getId()+"\t\t\t"+student.getName()+"\t\t\t"+student.getSex()+"\t\t"+
                student.getAge()+"\t\t\t"+student.getHeight());

        System.out.println("----------------------------------------------------------------");

        System.out.println("请确认是否删除该学员:(Y/N)");
        String op = sc.next();
        if ("y".equalsIgnoreCase(op)) {
            sqlSession = MybatisUtil.opSession();
            mapper = sqlSession.getMapper(StuMapper.class);
            mapper.deleteStu(id);
            MybatisUtil.commitAndClose(sqlSession);
            System.out.println("删除成功!");

        }
            else {
            System.out.println("取消修改!");
            return;
        }
    }
③修改学员
    private static void updateStu() {

        findAll();
        System.out.println("请输入要修改的学员学号:");
        int id = sc.nextInt();
        SqlSession sqlSession = MybatisUtil.opSession();
        StuMapper mapper = sqlSession.getMapper(StuMapper.class);

        Student student = mapper.findStuById(id);

        if (student == null) {
            System.out.println("没有该学员信息!");
            return;
        }

        //回显数据
        System.out.println("================================================================");
        System.out.println("----------------------------------------------------------------");
        System.out.println("学号\t\t姓名\t\t性别\t\t年龄\t\t身高");

            System.out.println(student.getId()+"\t\t\t"+student.getName()+"\t\t\t"+student.getSex()+"\t\t"+
                    student.getAge()+"\t\t\t"+student.getHeight());

        System.out.println("----------------------------------------------------------------");


        System.out.println("请选择是否修改姓名:(Y/N)");
        String op = sc.next();
        if ("y".equalsIgnoreCase(op)) {
            System.out.println("请输入新姓名:");
            String newName = sc.next();
            student.setName(newName);
        }
        System.out.println("请选择是否修改性别:(Y/N)");
        op = sc.next();
        if ("y".equalsIgnoreCase(op)) {
            System.out.println("请输入新性别:");
            String newNSex = sc.next();
            student.setSex(newNSex);
        }
        System.out.println("请选择是否修改年龄:(Y/N)");
         op = sc.next();
        if ("y".equalsIgnoreCase(op)) {
            System.out.println("请输入新年龄:");
            int newAge = sc.nextInt();
            student.setAge(newAge);
        }
        System.out.println("请选择是否修改身高:(Y/N)");
        op = sc.next();
        if ("y".equalsIgnoreCase(op)) {
            System.out.println("请输入新身高:");
            double newHheight = sc.nextDouble();
            student.setHeight(newHheight);
        }


        sqlSession = MybatisUtil.opSession();
        mapper = sqlSession.getMapper(StuMapper.class);
        mapper.updateStu(student);
        MybatisUtil.commitAndClose(sqlSession);

        System.out.println("修改已完成!");

    }
④ 查询学员
    private static void findAll() {

        SqlSession sqlSession = MybatisUtil.opSession();
        StuMapper mapper = sqlSession.getMapper(StuMapper.class);
        ArrayList<Student> stus = mapper.findAll();

        MybatisUtil.commitAndClose(sqlSession);

        if (stus.size() == 0 || stus == null) {
            System.out.println("暂无数据!");
            return;
        }

        System.out.println("【学员信息】");
        System.out.println("================================================================");
        System.out.println("----------------------------------------------------------------");
        System.out.println("学号\t\t姓名\t\t性别\t\t年龄\t\t身高");

        for (Student student : stus) {
            System.out.println(student.getId()+"\t\t\t"+student.getName()+"\t\t\t"+student.getSex()+"\t\t"+
                    student.getAge()+"\t\t\t"+student.getHeight());
        }
        System.out.println("----------------------------------------------------------------");
    }

运行结果

1. 添加功能测试

在这里插入图片描述

2.查询功能测试

在这里插入图片描述

3.修改功能测试

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4.删除功能测试

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

补充:关于Mapper接口下@SQL五提示为绿色问题

在sql语句中 alt + enter(回车)选择如图所示语句,搜索mysql点击确认就可以了
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用IDEA搭建Maven框架SSM的步骤: 1. 打开IDEA,点击"Create New Project"创建一个新项目。 2. 在左侧面板选择"Maven",然后点击"Next"。 3. 在"GroupId"和"ArtifactId"字段中输入项目的唯一标识符,然后点击"Next"。 4. 在"Project SDK"字段中选择JDK版本,然后点击"Next"。 5. 在"Project Name"字段中输入项目名称,然后点击"Finish"。 6. 等待IDEA自动创建项目结构和配置文件。 7. 打开项目的pom.xml文件,在其中添加所需的依赖项。例如,如果你需要使用SSM框架,可以添加以下依赖项: ```xml <dependencies> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.0.RELEASE</version> </dependency> <!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.0.RELEASE</version> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.4</version> </dependency> <!-- 数据库驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> </dependencies> ``` 8. 在src/main目录下创建Java源代码目录和资源目录,例如src/main/java和src/main/resources。 9. 在src/main/resources目录下创建配置文件,例如Spring配置文件和MyBatis配置文件。 10. 在src/main/java目录下创建Java类,例如Controller、Service和Mapper等。 11. 编写你的业务逻辑代码。 12. 运行你的应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PY_XAT_SFZL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值