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

chapter01zhao

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

chapter01zhao

1.IClassMapper

package com.dao;

import com.pojo.IClass;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface IClassMapper {
    @Select("select * from c_class where id = #{id}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "classroom",property = "classroom"),
            @Result(column = "id",property = "studentList",javaType = List.class,
                    many = @Many(select = "com.utils.findStudentByCidTest"))
    })
    IClass selectdClassStuByCid(int id);
}

2.IStudentMapper

package com.dao;

import com.pojo.IStudent;
import org.apache.ibatis.annotations.*;

public interface IStudentMapper {
    
    @Select("select * from s_student where id = #{id}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "age",property = "age")
    })
    public IStudent selectStudentById(int id);

    
    @Update("update s_student set name = #{name},age=#{age} where id=#{id}")
    public int UpdateStudent(IStudent student);

    
    @Insert("insert into s_student(id,name,age) value(#{id},#{name},#{age})")
    public int InsertStudent(IStudent student);

    
    @Delete("delete from s_student where id = #{id}")
    int deleteStudent(int book_id);

    
    @Select("select * from s_student where cid = #{cid}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "age",property = "age")
    })
    int selectByCid(int cid);
}

3.IClass

package com.pojo;

public class IClass {
    private int id;
    private String classroom;

    public int getId() {
        return id;
    }

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

    public String getClassroom() {
        return classroom;
    }

    public void setClassroom(String classroom) {
        this.classroom = classroom;
    }

    @Override
    public String toString() {
        return "IClass{" +
                "id=" + id +
                ", classroom='" + classroom + ''' +
                '}';
    }
}

4.IStudent

package com.pojo;

public class IStudent {
    private int id;
    private String name;
    private int age;
    private int cid;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public int getCid() {
        return cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }

    @Override
    public String toString() {
        return "IStudent{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", age='" + age + ''' +
                ", cid=" + cid +
                '}';
    }
}

5.findIStudentByIdTest

package com.utils;

import com.dao.IStudentMapper;
import com.pojo.IStudent;
import org.apache.ibatis.session.SqlSession;

public class findIStudentByIdTest {
    public static void main(String[] args){
        SqlSession session=MyBatisUtils.getSession();

        IStudentMapper mapper=session.getMapper(IStudentMapper.class);
        IStudent student=mapper.selectStudentById(2);

        System.out.println(student.toString());
        session.close();
    }
}

6.findStudentByCidTest

package com.utils;

 import com.dao.IClassMapper;
import com.dao.IStudentMapper;
import com.pojo.IClass;
import com.pojo.IStudent;
import org.apache.ibatis.session.SqlSession;

import java.util.Scanner;

public class findStudentByCidTest {
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        SqlSession session=MyBatisUtils.getSession();

        IStudentMapper mapper=session.getMapper(IStudentMapper.class);
        IClassMapper classMapper=session.getMapper(IClassMapper.class);

        System.out.println("请输入班号");
        int cid=scanner.nextInt();

        IClass  student=classMapper.selectdClassStuByCid(cid);
        System.out.println(student.toString());
    }
}

7.MybaitsTest

package com.utils;

import com.dao.IStudentMapper;
import com.pojo.IStudent;
import org.apache.ibatis.session.SqlSession;

import java.util.Scanner;

public class MybaitsTest {
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        SqlSession session=MyBatisUtils.getSession();

        IStudentMapper studentMapper=session.getMapper(IStudentMapper.class);

        System.out.println("请输入要选择的功能:01查询学生  02删除学生  03更新学生  04插入学生");
        int user=scanner.nextInt();

        switch (user){
            case 01:
                System.out.println("请输入要查询学生的id");
                int bookId=scanner.nextInt();
                IStudent select=studentMapper.selectStudentById(bookId);
                System.out.println(select.toString());
                break;
            case 02:
                System.out.println("请输入要删除学生的id");
                int userBookId=scanner.nextInt();
                int delete=studentMapper.deleteStudent(userBookId);
                if (delete>0){
                    System.out.println("删除成功");
                }else {
                    System.out.println("删除失败");
                }
                break;
            case 03:
                System.out.println("请输入更新的学生的id");
                int student_id=scanner.nextInt();
                System.out.println("请输入更新学生的名字");
                String name=scanner.next() ;
                System.out.println("请输入更新学生的年龄");
                int age=scanner.nextInt();

                IStudent student=new IStudent();
                student.setId(student_id);
                student.setName(name);
                student.setAge(age);

                int update=studentMapper.UpdateStudent(student);
                if (update>0){
                    System.out.println("更新成功");
                }else {
                    System.out.println("更新失败");
                }
                break;
            case 04:
                System.out.println("请输入插入的学生的id");
                int studentid=scanner.nextInt();
                System.out.println("请输入插入学生的名字");
                String studentname=scanner.next();
                System.out.println("请输入插入学生的年龄");
                int studentage=scanner.nextInt();

                IStudent student1=new IStudent();
                student1.setId(studentid);
                student1.setName(studentname);
                student1.setAge(studentage);

                int insert=studentMapper.InsertStudent(student1);
                if (insert>0){
                    System.out.println("插入成功");
                }else {
                    System.out.println("插入失败");
                }
                break;
        }
        session.commit();
        session.close();
    }
}

8.UpdateStudentTest

package com.utils;

import com.dao.IStudentMapper;
import com.pojo.IStudent;
import org.apache.ibatis.session.SqlSession;

import java.util.Scanner;

public class UpdateStudentTest {
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        SqlSession session=MyBatisUtils.getSession();
        IStudentMapper mapper=session.getMapper(IStudentMapper.class);

        IStudent stu=new IStudent();

        System.out.println("请输入更新信息的ID");
        int id=scanner.nextInt();
        System.out.println("请输入更新学生的名字");
        String name=scanner.next();
        System.out.println("请输入更新学生的年龄");
        int age=scanner.nextInt();

        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);

        int update=mapper.UpdateStudent(stu);
        if (update>0){
            System.out.println("更新成功");
        }else {
            System.out.println("更新失败");
        }
        session.commit();
        session.close();
    }
}

9. UpdateStudentTest

package com.utils;

import com.dao.IStudentMapper;
import com.pojo.IStudent;
import org.apache.ibatis.session.SqlSession;

import java.util.Scanner;

public class UpdateStudentTest {
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        SqlSession session=MyBatisUtils.getSession();
        IStudentMapper mapper=session.getMapper(IStudentMapper.class);

        IStudent stu=new IStudent();

        System.out.println("请输入更新信息的ID");
        int id=scanner.nextInt();
        System.out.println("请输入更新学生的名字");
        String name=scanner.next();
        System.out.println("请输入更新学生的年龄");
        int age=scanner.nextInt();

        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);

        int update=mapper.UpdateStudent(stu);
        if (update>0){
            System.out.println("更新成功");
        }else {
            System.out.println("更新失败");
        }
        session.commit();
        session.close();
    }
}

10.db.properties

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/test
jdbc.user=root
jdbc.password=123456

11.mybatis-config.xml




    
        
        
    


                    
        
                
                        
                
                
                
                
            
        
    

    
        
        
        
    

12.pom.xml



    4.0.0

    war

    org.example
    Mybaits-private
    1.0-SNAPSHOT

    
        8
        8
    
    
        
            org.mybatis
            mybatis
            3.5.2
        
        
            mysql
            mysql-connector-java
            5.1.47
        

        
            junit
            junit
            4.12
            compile
        
    

    
        Mybiats-private
        
            
                
                    maven-clean-plugin
                    3.1.0
                
                
                
                    maven-resources-plugin
                    3.0.2
                
                
                    maven-compiler-plugin
                    3.8.0
                
                
                    maven-surefire-plugin
                    2.22.1
                
                
                    maven-war-plugin
                    3.2.2
                
                
                    maven-install-plugin
                    2.5.2
                
                
                    maven-deploy-plugin
                    2.8.2
                
            
        
    

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/930436.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号