Java笔记之Mybatis(二):增删改查
0.前言
(1).以下操作基于Mybatis(一):简单入门已存在的数据;
(2).mybatisConfig.xml中已开启驼峰命名规则映射;
1.修改Student.xml
<?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="com.mybatis.demo.pojo"> <!-- 查询所有的Student信息 --> <select id="getAllStudent" resultType="Student"> select student_id, student_name, student_age from t_student </select> <!-- 根据ID查询Student的信息 --> <select id="getStudentById" parameterType="int" resultType="Student"> select * from t_student where student_id=#{value} </select> <!-- 新增Student信息 --> <insert id="addStudent" parameterType="Student"> insert into t_student values( null, #{studentName}, #{studentAge} ) </insert> <!-- 根据ID删除指定的Student信息 --> <delete id="deleteStudentById" parameterType="int"> delete from t_student where student_id=#{value} </delete> <!-- 根据ID更新Student信息 --> <update id="updateStudentById" parameterType="Student"> update t_student set student_name=#{studentName}, student_age=#{studentAge} where student_id=#{studentId} </update> </mapper>
2.修改MybatisTest类
package com.mybatis.demo.test; import java.io.IOException; import java.io.InputStream; import java.util.List; 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 com.mybatis.demo.pojo.Student; public class MybatisTest { public static void main(String[] args) throws IOException { //加载mybatis的配置文件 InputStream inputStream = Resources.getResourceAsStream("mybatisConfig.xml"); //获取SqlSessionFactory对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //使用SqlSessionFactory对象创建SqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(); System.out.println("======新增操作开始======"); Student student = new Student(); student.setStudentName("小明"); student.setStudentAge(10); addStudent(sqlSession, student); getAllStudent(sqlSession); System.out.println("======新增操作结束======"); System.out.println("======修改操作开始======"); updateStudentById(sqlSession, 1); getAllStudent(sqlSession); System.out.println("======修改操作结束======"); System.out.println("======删除操作开始======"); deleteStudentById(sqlSession, 1); getAllStudent(sqlSession); System.out.println("======删除操作结束======"); System.out.println("======查询操作开始======"); getStudentById(sqlSession, 2); System.out.println("======查询操作结束======"); } /** * 根据学生ID获取学生信息 * @param sqlSession * @param studentId */ private static void getStudentById(SqlSession sqlSession,int studentId){ Student student = sqlSession.selectOne("getStudentById",studentId); if(student!=null){ System.out.println(student.getStudentId()+"\t"+student.getStudentName()+"\t"+student.getStudentAge()); }else{ System.out.println("未查询到指定ID的学生信息"); } } /** * 查询所有的学生信息 * @param sqlSession */ private static void getAllStudent(SqlSession sqlSession){ //查询结果为多条,使用selectList方法,如果结果为一条,使用selectOne方法 List<Student> students = sqlSession.selectList("getAllStudent"); //遍历打印学生信息 for (Student student : students) { System.out.println(student.getStudentId()+"\t"+ student.getStudentName()+"\t"+student.getStudentAge()); } } /** * 新增学生信息 * @param sqlSession * @param student */ private static void addStudent(SqlSession sqlSession,Student student){ //返回值为操作影响的行数 int rows = sqlSession.insert("addStudent", student); //提交事务 sqlSession.commit(); System.out.println("受影响行数为:"+rows); } /** * 根据ID删除学生信息 * @param sqlSession * @param studentId */ private static void deleteStudentById(SqlSession sqlSession,int studentId){ int rows = sqlSession.delete("deleteStudentById", studentId); sqlSession.commit(); System.out.println("受影响的行数:"+rows); } /** * 根据ID修改学生信息 * @param sqlSession * @param studentId */ private static void updateStudentById(SqlSession sqlSession,int studentId){ Student student = sqlSession.selectOne("getStudentById", studentId); if(student!=null){ student.setStudentName(student.getStudentName()+"001"); int rows = sqlSession.update("updateStudentById", student); sqlSession.commit(); System.out.println("受影响的行数为:"+rows); }else{ System.out.println("未查询到指定ID的学生信息,请确认"); } } }
3.运行MybatisTest类,结果如下:
======新增操作开始====== 受影响行数为:1 1 张三 23 2 李四 24 3 王五 25 4 赵六 26 5 韩七 27 6 小明 10 ======新增操作结束====== ======修改操作开始====== 受影响的行数为:1 1 张三001 23 2 李四 24 3 王五 25 4 赵六 26 5 韩七 27 6 小明 10 ======修改操作结束====== ======删除操作开始====== 受影响的行数:1 2 李四 24 3 王五 25 4 赵六 26 5 韩七 27 6 小明 10 ======删除操作结束====== ======查询操作开始====== 2 李四 24 ======查询操作结束======