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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Mybatis入门学习---创建第一个Mybatis程序

發布時間:2025/3/21 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mybatis入门学习---创建第一个Mybatis程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在這里我先給出項目的目錄結構:

1.創建數據

在MySQL里面創建一個test數據庫,里面有student表,數據如下:

2.創建maven項目并導入相關依賴

maven依賴如下:

#pom.xml <!-- mybatis依賴包 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.1.1</version> </dependency><!-- mysql驅動包 --> <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.11</version> </dependency> 復制代碼

3.編寫mybatis核心配置文件

#mybatis-config.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.cj.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=false&amp;serverTimezone=GMT" /><property name="username" value="root" /><property name="password" value="123456" /></dataSource></environment></environments><!-- 存放映射文件 --><mappers><mapper resource="com/xgc/entity/user_mapper.xml"/></mappers> </configuration> 復制代碼

4.創建sqlSessionFactory,以及獲得SqlSession

#MyBatisUtil.java package com.xgc.util;import java.io.IOException; import java.io.InputStream;import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class MyBatisUtil {public static SqlSessionFactory getSqlSessionFactory() throws IOException {String resource = "mybatis-config.xml";InputStream inputStream=Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);return sqlSessionFactory;}public static SqlSession getSqlSession() throws IOException {SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();return sqlSessionFactory.openSession();} } 復制代碼

5.創建實體類

#User.java package com.xgc.entity;public class User {private int id;private String name;private int age;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;} } 復制代碼

6.編寫sql語句的映射文件

#user_mapper.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"> <mapper namespace="com.xgc.entity.UserMapper"><select id="selectUser" resultType="com.xgc.entity.User">select * from student where id = #{id}</select> </mapper> 復制代碼

7.開始查詢

#Test.java package com.xgc.test;import java.io.IOException;import org.apache.ibatis.session.SqlSession;import com.xgc.entity.User; import com.xgc.util.MyBatisUtil;public class Test {public static void main(String[] args) throws IOException {SqlSession session=MyBatisUtil.getSqlSession();User user=session.selectOne("com.xgc.entity.UserMapper.selectUser", 1);System.out.println("id="+user.getId()+",name="+user.getName()+",age="+user.getAge());session.close();} } 復制代碼

查詢結果為:

轉載于:https://juejin.im/post/5cd0c9316fb9a032453bc2e8

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的Mybatis入门学习---创建第一个Mybatis程序的全部內容,希望文章能夠幫你解決所遇到的問題。

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