Mybatis框架(三)Mybatis的CRUD操作

在上一篇Mybatis框架(二)Mybatis的入门程序中实现了Mybatis的查找操作,本文章主要实现其余的几种操作,这里不再创建项目,将接着上篇继续操作:

一、我们只需要更该接口StuMapper与Mapper的配置文件中的内容,其余不需要做变动。
编写接口:

     Stu getStuById(int id);   //根据学生的id查找学生信息
     int addStu(Stu stu);  //插入一个学生信息
     int updateStu(Stu stu);  //修改学生的信息
     int deleteStu(int id);   //删除学生的信息

编写对应的mapper中的sql语句:

  <!--根据学生id查找学生的全部信息-->
    <select id="getStuById" resultType="com.wst.pojo.Stu" parameterType="int">
       /*定义sql*/
       select * from stu where id = #{id};
   </select>

     <!--插入信息-->
    <insert id="addStu" parameterType="com.wst.pojo.Stu">
        insert into stu (id,name,age,address) values (#{id},#{name},#{age},#{address});
    </insert>

<!--&lt;!&ndash;修改信息&ndash;&gt;-->
    <update id="updateStu" parameterType="com.wst.pojo.Stu">
        update stu set name = #{name},age=#{age},address=#{address} where id=#{id} ;
    </update>
<!--删除信息-->
    <delete id="deleteStu" parameterType="int">
        delete from stu where id = #{id};
    </delete>

注:在上面的配置中主要涉及到的映射器:
在这里插入图片描述
二、启动,进行测试。
首先我们需要增加测试函数:

 //查询操作
       @Test
      public void selectStuId(){
            SqlSession sqlSession = MyBatisUtils.getSqlSession();
            StuMapper mapper = sqlSession.getMapper(StuMapper.class);
            System.out.println("根据学生的id查找学生的信息:");
            Stu stu=mapper.getStuById(2);
            System.out.println(stu);
            //关闭sqlsession
            sqlSession.close();
        }
        //插入操作
    @Test
    public void insertStu(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        StuMapper mapper = sqlSession.getMapper(StuMapper.class);
        System.out.println("插入学生信息:");
      int m=  mapper.addStu(new Stu(10,"XX",12,"XX"));
      if (m>0) {
            System.out.println("插入成功!");
        }
        sqlSession.commit();
        sqlSession.close();
    }
//更新操作
    @Test
    public void  updateStu(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        StuMapper mapper = sqlSession.getMapper(StuMapper.class);
        System.out.println("更新学生信息:");
        mapper.updateStu(new Stu(1,"XX",10,"XX"));
        sqlSession.commit();
        System.out.println("更新成功!");
        sqlSession.close();
    }
    //删除操作
    @Test
    public void deleteStu(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        StuMapper mapper = sqlSession.getMapper(StuMapper.class);
        System.out.println("删除学生信息:");
       mapper.deleteStu(2);
        sqlSession.commit();
        System.out.println("删除成功!!!");
        sqlSession.close();
    }

注:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MyBatis 是一种持久化框架,可以轻松地实现数据库操作。以下是使用 MyBatis 实现员工信息的 CRUD 操作的示例: 1. 首先,创建一个 Employee 类,该类包含员工的基本信息: ```java public class Employee { private int id; private String name; private String email; private String gender; private Department department; // 省略 getter 和 setter 方法 } ``` 2. 创建一个 Department 类,该类包含部门的基本信息: ```java public class Department { private int id; private String name; // 省略 getter 和 setter 方法 } ``` 3. 创建一个 DAO 接口,该接口定义了员工信息的 CRUD 操作: ```java public interface EmployeeMapper { // 添加员工信息 public int addEmployee(Employee employee); // 根据员工编号删除员工信息 public int deleteEmployeeById(int id); // 更新员工信息 public int updateEmployee(Employee employee); // 根据员工编号查询员工信息 public Employee getEmployeeById(int id); // 查询所有员工信息 public List<Employee> getAllEmployees(); } ``` 4. 创建一个映射文件(employeeMapper.xml),该文件包含了 SQL 语句和参数映射: ```xml <mapper namespace="com.example.EmployeeMapper"> <!-- 添加员工信息 --> <insert id="addEmployee" parameterType="com.example.Employee"> insert into employee(name, email, gender, dept_id) values(#{name}, #{email}, #{gender}, #{department.id}) </insert> <!-- 根据员工编号删除员工信息 --> <delete id="deleteEmployeeById" parameterType="int"> delete from employee where id = #{id} </delete> <!-- 更新员工信息 --> <update id="updateEmployee" parameterType="com.example.Employee"> update employee set name = #{name}, email = #{email}, gender = #{gender}, dept_id = #{department.id} where id = #{id} </update> <!-- 根据员工编号查询员工信息 --> <select id="getEmployeeById" parameterType="int" resultType="com.example.Employee"> select e.id, e.name, e.email, e.gender, d.id as dept_id, d.name as dept_name from employee e left join department d on e.dept_id = d.id where e.id = #{id} </select> <!-- 查询所有员工信息 --> <select id="getAllEmployees" resultType="com.example.Employee"> select e.id, e.name, e.email, e.gender, d.id as dept_id, d.name as dept_name from employee e left join department d on e.dept_id = d.id </select> </mapper> ``` 5. 创建一个 MyBatis 的配置文件,该文件包含了数据库连接信息和映射文件的路径: ```xml <?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"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/example/EmployeeMapper.xml"/> </mappers> </configuration> ``` 6. 最后,在 Java 代码中使用 MyBatis 进行员工信息的 CRUD 操作: ```java // 加载 MyBatis 配置文件 InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 获取 SqlSession 对象 SqlSession sqlSession = sqlSessionFactory.openSession(); // 获取 EmployeeMapper 接口的代理对象 EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class); // 添加员工信息 Employee employee = new Employee(); employee.setName("张"); employee.setEmail("zhangsan@163.com"); employee.setGender("男"); Department department = new Department(); department.setId(1); employee.setDepartment(department); employeeMapper.addEmployee(employee); sqlSession.commit(); // 根据员工编号查询员工信息 Employee employee = employeeMapper.getEmployeeById(1); System.out.println(employee.getName()); // 查询所有员工信息 List<Employee> employeeList = employeeMapper.getAllEmployees(); for (Employee emp : employeeList) { System.out.println(emp.getName()); } // 关闭 SqlSession 对象 sqlSession.close(); ``` 以上就是使用 MyBatis 实现员工信息的 CRUD 操作的整个过程。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小小Java开发者

“是一种鼓励,你懂的”

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

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

打赏作者

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

抵扣说明:

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

余额充值