Statement方式的增删改查

新建studentMapper.xml文件并写入增删改查

select * from student where stuNo = #{stuNo}
<!--增加-->
<insert id="addStudent" parameterType="org.dong.Student">

– 增加stuNo,stuName,stuAge,grName
INSERT into student(stuNo,stuName,stuAge,graName)
values (#{stuNo},#{stuName},#{stuAge},#{graName})

<!--删除-->
<delete id="delteStudentByStuNo" parameterType="int">

– 删除student表的stuNo
DELETE from student
where stuNo = #{stuNo}

<!--修改-->
<update id="updateStudentByStuNo" parameterType="org.dong.Student">

– 根据stuNo修改名字,年龄,班级名
UPDATE student
SET stuName=#{stuName},stuAge=#{stuAge},graName=#{graName}
WHERE stuNo=#{stuNo}

<!--查询全部学生-->
<select id="queryAllStudents" resultType="org.dong.Student">
    SELECT  * FROM  student
</select>

新建一个测试类

public class TestMyBatis {

   //查询单个学生
public static void queryStudentByStuNo() throws IOException{
    //加载MyBatis配置文件(为了访问数据库)
    //将conf.xml配置文件变成对象
    Reader reader = Resources.getResourceAsReader("mapper/conf.xml");
    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
    //SqlSessionFactory - connection
    SqlSession session = sessionFactory.openSession();
    //personMapper.xml中的namespace和id组合成personMapper.queryPersonById就拿到select语句
    // String statement ="mapper.personMapper.queryPersonById";
    String statement ="mapper.studentMapper.queryStudentByStuNo";
    //personMapper.xml返回值类型为Person所以可以用Person
    //查询学号为2的

// Person person = session.selectOne(statement,2);
Student student = session.selectOne(statement,1);
System.out.println(student);
session.close();

}

//查询全部学生
public static void queryAllStudents() throws IOException {
    //加载MyBatis配置文件(为了访问数据库)
    //将conf.xml配置文件变成对象
    Reader reader = Resources.getResourceAsReader("mapper/conf.xml");
    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
    //SqlSessionFactory - connection
    SqlSession session = sessionFactory.openSession();
    //personMapper.xml中的namespace和id组合成personMapper.queryPersonById就拿到select语句
    // String statement ="mapper.personMapper.queryPersonById";
    String statement ="mapper.studentMapper.queryAllStudents";
    //personMapper.xml返回值类型为Person所以可以用Person
    //查询学号为2的

// Person person = session.selectOne(statement,2);
//查询全部学生用selectList和List
List students = session.selectList(statement);
System.out.println(students);
session.close();
}

//增加学生
public static void addStudent() throws IOException {
    //加载MyBatis配置文件(为了访问数据库)
    //将conf.xml配置文件变成对象
    Reader reader = Resources.getResourceAsReader("mapper/conf.xml");
    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
    //SqlSessionFactory - connection
    SqlSession session = sessionFactory.openSession();
    //personMapper.xml中的namespace和id组合成personMapper.queryPersonById就拿到select语句
    // String statement ="mapper.personMapper.queryPersonById";


    String statement ="mapper.studentMapper.addStudent";
    Student student = new Student(3,"zz",21,"s2");
    //增加的(参数,学生)starement表示指定执行的SQL student:SQL中需要的参数(???)
    int count =session.insert(statement,student);
    //提交事务
    session.commit();
    System.out.println("增加"+count+"个学生");
    session.close();

}

//删除学生
public static void delteStudentByStuNo() throws IOException {
    //加载MyBatis配置文件(为了访问数据库)
    //将conf.xml配置文件变成对象
    Reader reader = Resources.getResourceAsReader("mapper/conf.xml");
    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
    //SqlSessionFactory - connection
    SqlSession session = sessionFactory.openSession();
    //personMapper.xml中的namespace和id组合成personMapper.queryPersonById就拿到select语句
    // String statement ="mapper.personMapper.queryPersonById";


    String statement ="mapper.studentMapper.delteStudentByStuNo";
   //删除3号学生
    int count = session.delete(statement,3);
    //提交事务
    session.commit();
    System.out.println("删除"+count+"个学生");
    session.close();

}

//修改学生
public static void updateStudentByStuNo() throws IOException {
    //加载MyBatis配置文件(为了访问数据库)
    //将conf.xml配置文件变成对象
    Reader reader = Resources.getResourceAsReader("mapper/conf.xml");
    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
    //SqlSessionFactory - connection
    SqlSession session = sessionFactory.openSession();
    //personMapper.xml中的namespace和id组合成personMapper.queryPersonById就拿到select语句
    // String statement ="mapper.personMapper.queryPersonById";


    String statement ="mapper.studentMapper.updateStudentByStuNo";
    //修改的参数
    Student student =new Student();
    //修改哪个人,where stuNo=2
    student.setStuNo(1);
    //修改成什么样子
    student.setStuName("dzz");
    student.setStuAge(18);
    student.setGraName("s8");
    //执行
    int count = session.update(statement,student);

    //提交事务
    session.commit();
    System.out.println("修改"+count+"个学生");
    session.close();

}
//throws表示此main方法可能要抛出异常
public static void main(String[] args) throws IOException{
    queryAllStudents();
   // addStudent();
   // delteStudentByStuNo();
    updateStudentByStuNo();
    queryAllStudents();
}

}

Student.java

public class Student {
private int stuNo;
private String stuName;
private int stuAge;
private String graName;

public Student() {

}

public Student(int stuNo, String stuName, int stuAge, String graName) {
    this.stuNo = stuNo;
    this.stuName = stuName;
    this.stuAge = stuAge;
    this.graName = graName;
}

public int getStuNo() {
    return stuNo;
}

public void setStuNo(int stuNo) {
    this.stuNo = stuNo;
}

public String getStuName() {
    return stuName;
}

public void setStuName(String stuName) {
    this.stuName = stuName;
}

public int getStuAge() {
    return stuAge;
}

public void setStuAge(int stuAge) {
    this.stuAge = stuAge;
}

public String getGraName() {
    return graName;
}

public void setGraName(String graName) {
    this.graName = graName;
}

@Override
public String toString(){
    return this.stuNo+","+this.stuName+","+this.stuAge+","+this.graName;
}

}

还有一个conf.xml映射文件可以看我上上一篇文章进行参考

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值