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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

mybatis --入门 单表增删改查-curd

發布時間:2023/12/10 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mybatis --入门 单表增删改查-curd 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

    • 1. mybatis 環境搭建
    • 2. 實體類映射文件配置(寫sql)
    • 3. mybatis核心配置文件 (環境配置)
    • 4. 測試

mybatis document
https://mybatis.org/mybatis-3/zh/

mybatis in github
https://github.com/mybatis/mybatis-3

1. mybatis 環境搭建

這里使用maven項目構建

<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!-- mysql 驅動包 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.20</version></dependency><!-- mybatis 框架 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.5</version></dependency> </dependencies>

2. 實體類映射文件配置(寫sql)

  • 實體類
package cn.bitqian.entity;/*** user 類* @author echo lovely* @date 2020/9/9 20:59*/ public class User {private Integer userId;private String userName;private String userPassword;public User() {}public User(Integer userId, String userName, String userPassword) {this.userId = userId;this.userName = userName;this.userPassword = userPassword;}// 省略set/get toString.. }
  • user-mapper.xml(注意命名空間,標簽 id,占位符)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- 實體類 與 sql解耦映射文件 --> <mapper namespace="userMapper"><!-- sql 語句 和 對應的查詢結果類型 --><select id="selectAll" resultType="cn.bitqian.entity.User">select * from users1</select><!-- parameterType 為接收參數的類型 #{實體類屬性名} 表示占位符 --><!-- 插入操作 需要user參數 --><insert id="insertUser" parameterType="cn.bitqian.entity.User">insert into users1 values (#{userId}, #{userName}, #{userPassword})</insert><!-- 修改操作 --><update id="updateUser" parameterType="cn.bitqian.entity.User">update users1 set username = #{userName}, userpassword = #{userPassword}where userid = #{userId}</update><!-- 根據id刪除某個user --><delete id="deleteUser" parameterType="java.lang.Integer">delete from users1 where userid = #{userId}</delete> </mapper>

3. mybatis核心配置文件 (環境配置)

  • jdbc 連接配置
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis_study?serverTimezone=GMT username=root password=123456
  • mybatis-config 配置 (注意頭文件的引入)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!-- 讀取jdbc.properties 配置信息 --><properties resource="jdbc.properties"></properties><environments default="development"><!--部署環境--><environment id="development"><!-- 配置事務管理器 --><transactionManager type="JDBC"></transactionManager><!-- 配置數據源 --><dataSource type="POOLED"><property name="driver" value="${driver}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/></dataSource></environment></environments><mappers><!-- 加載映射用戶 xml這里有個小問題,cn/bitqian/mapper, 看文件夾有沒三層 --><mapper resource="cn.bitqian.mapper/user-mapper.xml"/></mappers> </configuration>
  • 包名就叫cn.bitqian.mapper, 沒有分層

4. 測試

  • 由于初學,模板化的代碼我寫了四遍… 其中包含了單表curd
package DemoTest;import cn.bitqian.entity.User; 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.junit.Test;import java.io.IOException; import java.io.InputStream; import java.util.List;/*** mybatis 單表增刪改查操作* @author echo lovely* @date 2020/9/9 21:36*/ public class TestMybatisCurd {// 查詢單表@Testpublic void test1() {SqlSession sqlSession = null;try {// mybatis 讀取核心文件流InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");// 通過sql 會話工廠構建器 創建session 工廠SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);// 拿到sessionsqlSession = sqlSessionFactory.openSession();// 進行查詢List<User> userList = sqlSession.selectList("userMapper.selectAll");System.out.println(userList);} catch (IOException e) {e.printStackTrace();} finally {// 釋放session資源if (sqlSession != null)sqlSession.close();}}@Testpublic void test2() {SqlSession sqlSession = null;try {// 加載mybatis核心配置文件InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");// 獲得session工廠SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);// 通過session工廠獲得sessionsqlSession = sqlSessionFactory.openSession();User user = new User(null, "taylor swift", "gorgeous");// 通過user-mapper 命名空間和id進行相應的sql操作int count = sqlSession.insert("userMapper.insertUser", user);// 進行增刪改操作時 要提交事務sqlSession.commit();System.out.println("count = " + count);} catch (IOException e) {e.printStackTrace();} finally {if (sqlSession != null)sqlSession.close();}}// 修改單個用戶@Testpublic void test3() {SqlSession sqlSession = null;try {InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);sqlSession = sqlSessionFactory.openSession();User user = new User(1, "rose", "love jack");int count = sqlSession.update("userMapper.updateUser", user);sqlSession.commit();System.out.println(count);} catch (IOException e) {e.printStackTrace();} finally {if (sqlSession != null)sqlSession.close();}}// 刪除user@Testpublic void tes4() {SqlSession sqlSession = null;try {InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);sqlSession = sqlSessionFactory.openSession();sqlSession.delete("userMapper.deleteUser", 1);sqlSession.commit();} catch (IOException e) {e.printStackTrace();} finally {if (sqlSession != null)sqlSession.close();}}}

總結

以上是生活随笔為你收集整理的mybatis --入门 单表增删改查-curd的全部內容,希望文章能夠幫你解決所遇到的問題。

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