基于MyBatis的CRUD操作

一、基于XML实现
1.定义SQL映射XML文件
studentMapper.xml:
<mapper namespace="com.mapping.studentMapper">
    <!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getStudent,id属性值必须是唯一的,不能够重复
    使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型
    resultType="com.domain.User"就表示将查询结果封装成一个Student类的对象返回
           Student类就是student表所对应的实体类
    -->
    <!-- 根据id查询得到一个student对象  -->
    <select id="getStudent" parameterType="int"
        resultType="com.domain.Student">
        select * from ksl_student where sid=#{sid}
    </select>
    
    <!-- 创建Student -->
    <insert id="addStudent" parameterType="com.domain.Student">
        insert into ksl_student(sname, sage, ssex) values (#{sname}, #{sage}, #{ssex})
    </insert>
    
    <!-- 删除Student -->
    <delete id="deleteStudent" parameterType="int">
        delete from ksl_student where sid = #{sid}
    </delete>
    
    <!-- 修改Student -->
    <update id="updateStudent" parameterType="com.domain.Student">
        update ksl_student set sname = #{sname}, sage = #{sage}, ssex = #{ssex} where sid = #{sid}
    </update>
    
    <!-- 查看全部Student -->
    <select id="getAllStudents" resultType="com.domain.Student">
        select * from ksl_student
    </select>
</mapper>
2.测试代码举例:
SqlSession sqlSession = MyBatisUtil.getSqlSession(false);
String statement = "com.mapping.studentMapper.addStudent";
Student s = new Student();
s.setSage(34);
s.setSname("liqingzhao");
s.setSsex("girl");
// 执行插入操作(返回值代表受影响的行数)
int res = sqlSession.insert(statement, s);
// 手动提交事务
sqlSession.commit();
sqlSession.close();
System.out.println(res);

二、基于注解实现
1.定义SQL映射的接口
(我们不需要针对接口去编写具体的实现类代码,具体的实现类由MyBatis动态构建出来,我们可以直接拿来使用)
public interface StudentMapperI {

    @Insert("insert into ksl_student(sname, sage, ssex) values (#{sname}, #{sage}, #{ssex})")
    public int add(Student student);
    
    @Delete("delete from ksl_student where sid = #{sid}")
    public int deleteById(int sid);
    
    @Update("update ksl_student set sname = #{sname}, sage = #{sage}, ssex = #{ssex} where sid = #{sid}")
    public int update(Student student);
    
    @Select("select * from ksl_student where sid=#{sid}")
    public Student getBySId(int sid);
    
    @Select("select * from ksl_student")
    public List<Student> getAll();
}
2.在conf.xml中注册这个接口
<mapper class="com.mapping.StudentMapperI"/>
3.测试代码举例:
@Test
    public void testAdd() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        StudentMapperI mapper = sqlSession.getMapper(StudentMapperI.class);
        Student s = new Student();
        s.setSage(34);
        s.setSname("liqingzhao");
        s.setSsex("girl");
        int res = mapper.add(s);
        sqlSession.close();
        System.out.println(res);
    }

三、工具类
public class MyBatisUtil {

    // 获取SqlSessionFactory
    public static SqlSessionFactory getSqlSessionFactory() {
        String resource = "conf.xml";
        InputStream is = MyBatisUtil.class.getClassLoader().getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        return sessionFactory;
    }
    // 获取SqlSession
    public static SqlSession getSqlSession() {
        return getSqlSessionFactory().openSession();
    }
    /*
     * 获取SqlSession
     * @param isAutoCommit
     *         true 表示创建的SqlSession对象在执行完SQL之后会自动提交事务
     *         false 表示创建的SqlSession对象在执行完SQL之后不会自动提交事务,这时就需要我们手动调用sqlSession.commit()提交事务
     */
    public static SqlSession getSqlSession(boolean isAutoCommit) {
        return getSqlSessionFactory().openSession(isAutoCommit);
    }
}

猜你喜欢

转载自www.cnblogs.com/yuanfei1110111/p/10349471.html