日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MyBatis入门级(增删改查)

發布時間:2024/1/8 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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入门级(增删改查)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。