MyBatis入门级(增删改查)
生活随笔
收集整理的這篇文章主要介紹了
MyBatis入门级(增删改查)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.接口 StudentMapper
package com.tty.mybatis.dao;import com.tty.mybatis.pojo.Student; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select;import java.util.Map;public interface StudentMapper {// 還可以這樣寫節省時間@Select("select `id`,`name`,`student_sex` from student where `id` = #{id}")//查詢 id 查詢學生public Student retrieveStudentById(Integer id);//增加public void addStudent(Student student);//更新public void updateStudentName(Map<String,Object> map);// 普通寫法 :public void updateStudentName(@Param("id") Integer id, @Param("newName") String newName);// 刪除public void deleteStudent(Integer id); }2.學生映射配置文件 StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- student SQL 映射文件 --><mapper namespace="com.tty.mybatis.dao.StudentMapper"> <!-- 命名空間,寫 StudentMapper的路徑 --><!-- 查詢 --><select id="retrieveStudentById" resultType="student"> <!-- 這里的id寫之前的命名的 , resultType是封裝結果類型寫 import 后面的 -->select `id`,`name`,`student_sex` from student where `id` = #{id}</select><!-- 插入 --><insert id="addStudent" >INSERT INTO `student` VALUES (#{id},#{name},#{studentSex})</insert><!-- 更新 --><update id="updateStudentName">UPDATE `student` SET `name`=#{newName} WHERE `id`=#{id}</update><!-- 刪除 --><delete id="deleteStudent">DELETE FROM `student` WHERE `id`=#{id}</delete> </mapper>3.測試類 TestStudent
import com.tty.mybatis.dao.StudentMapper; import com.tty.mybatis.pojo.Student; 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 org.apache.ibatis.session.defaults.DefaultSqlSessionFactory; import org.junit.Test;import javax.annotation.Resource; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map;public class TestStudent {@Testpublic void test() {SqlSession sqlSession=getSqlSession();try {StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);//查詢Student student = mapper.retrieveStudentById(1);System.out.println(student);//增加mapper.addStudent(new Student(null,"懷懷","女"));//更新//可使用的參數 are [0, 1, param1, param2]//原理:會封裝成一個hashmap,所以 map.put("parm1",8) map.put("parm2","鐘佳文說我來啦~~")/* 這樣寫會顯得比較不爽,我們可以在StudentMapper里面寫注解即可 @Param* 或者用另外一種方法 那就是 創建 HashMap* */ // mapper.updateStudentName(8,"鐘佳文說我是大胖胖~~");// Map方法Map<String,Object> params = new HashMap<>();params.put("id",8);params.put("newName","我getlog4j.xml~");mapper.updateStudentName(params);sqlSession.commit();//刪除mapper.deleteStudent(8);sqlSession.commit();}finally {sqlSession.close();}}public SqlSession getSqlSession(){String mybatisConfigFile = "mybatis-config.xml";try {InputStream inputStream = Resources.getResourceAsStream(mybatisConfigFile);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);return sqlSessionFactory.openSession();} catch (IOException e) {e.printStackTrace();}return null;} }總結
以上是生活随笔為你收集整理的MyBatis入门级(增删改查)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Centos7系统启动盘的正确安装姿势
- 下一篇: 关于python语言数值操作符_下列哪种