Mybatis中的核心配置文件SqlMapConfig.xml詳細介紹

一、properties(屬性)

可以引用java屬性文件中的配置信息如下

在這里插入圖片描述
jdbc.properties代碼如下:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=beyond

sqlMapConfig.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><properties resource="jdbc.properties"/><!-- 和spring整合后 environments配置將廢除 --><environments default="development"><environment id="development"><!-- 使用jdbc事務管理 --><transactionManager type="JDBC" /><!-- 數據庫連接池 --><dataSource type="POOLED"><property name="driver" value="${jdbc.driver}" /><property name="url"value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" /><property name="username" value="root" /><property name="password" value="beyond" /></dataSource></environment></environments><!-- Mapper的位置 --><mappers><mapper resource="sqlmap/User.xml"/></mappers></configuration>

MybatisMapperTest代碼如下:

package com.pdsu.mybatis.mapper;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;
import org.junit.Test;import com.pdsu.mybatis.pojo.User;public class MybatisMapperTest {@Testpublic void testMapper() throws Exception {//加載核心配置文件,加載要使用IO流進行讀取String resource = "sqlMapConfig.xml";InputStream in = Resources.getResourceAsStream(resource);//創建SqlSessionFactorySqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);//SqlSessionFactoryBuilder這是一個實現類需要new一下//創建SQLSessionSqlSession sqlSession = sqlSessionFactory.openSession();	//SqlSession自動生成實現類(接口需要遵循四大原則)=== UserMapper.class為遵循四大原則的接口UserMapper userMapper = sqlSession.getMapper(UserMapper.class);User user = userMapper.findUserById(30);System.out.println(user);}
}

UserMapper接口如下:

package com.pdsu.mybatis.mapper;
import java.util.List;
import com.pdsu.mybatis.pojo.User;
public interface UserMapper {/*遵循四個原則對于:public User findUserById(Integer id);接口   方法名 == User.xml中id名 === findUserById返回值類型 與 Mapper.xml文件中返回值類型(resultType)要一致 === User === com.pdsu.mybatis.pojo.User方法的入參類型 與 Mapper.xml中入參類型(parameterType)要一致 === Integer id命名空間綁定此接口<mapper namespace="com.pdsu.mybatis.mapper.UserMapper">,User.xml中的路徑為該接口全路徑*/public User findUserById(Integer id);}

User代碼如下:

package com.pdsu.mybatis.pojo;import java.io.Serializable;
import java.util.Date;public class User implements Serializable {/*** */private static final long serialVersionUID = 1L;private Integer id;private String username;// 用戶姓名private String sex;// 性別private Date birthday;// 生日private String address;// 地址public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "User [id=" + id + ", username=" + username + ", sex=" + sex+ ", birthday=" + birthday + ", address=" + address + "]";}
}

至于數據庫創建,可以自己設計數據庫也可以參考如下數據庫:
https://github.com/beyondyanyu/Sayingyy/blob/master/JDBC2-數據庫sql建表語句

二、typeAliases(別名)

SQLMapConfig.xml:

	<typeAliases><!-- 別名<typeAlias type="com.pdsu.mybatis.pojo.User" alias="User"/> --><!-- 自動將com.pdsu.mybatis.pojo該包下的所有pojo,然后全部給配置給別名,不區分大小寫 --><package name="com.pdsu.mybatis.pojo"/></typeAliases>
properties必須在typeAliases之前!!!!!順序不可以顛倒

也就是說在User.xml里面
例如:<select id="findUserById" parameterType="Integer" resultType="user"> select * from user where id= #{sq} </select>這個:通過ID查詢一個用戶
resultType可以為pojo包下的實體,并不用再具體到那個包下的那個pojo了

sqlMapConfig.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><!-- 可以引用java屬性文件中的配置信息 --><properties resource="jdbc.properties"/><!-- 別名、包名若為包名的話,包和其子包下的所有類 頭字母大小寫都行 --><typeAliases><!-- 別名<typeAlias type="com.pdsu.mybatis.pojo.User" alias="User"/> --><!-- 自動將com.pdsu.mybatis.pojo該包下的所有pojo,然后全部給配置給別名,不區分大小寫 --><package name="com.pdsu.mybatis.pojo"/></typeAliases><!-- properties必須在typeAliases之前!!!!!順序不可以顛倒 --><!-- 和spring整合后 environments配置將廢除 --><environments default="development"><environment id="development"><!-- 使用jdbc事務管理 --><transactionManager type="JDBC" /><!-- 數據庫連接池 --><dataSource type="POOLED"><property name="driver" value="${jdbc.driver}" /><property name="url"value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" /><property name="username" value="root" /><property name="password" value="beyond" /></dataSource></environment></environments><!-- Mapper的位置 --><mappers><mapper resource="sqlmap/User.xml"/></mappers></configuration>

User.xml代碼如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:命名空間,用于隔離sql,還有一個很重要的作用,后面會講 -->
<!-- 
命名空間:user.findUserById
命名空間:order.findUserById 
--><mapper namespace="com.pdsu.mybatis.mapper.UserMapper"><!-- resultType:返回值parameterType:輸入參數 --><!-- 通過ID查詢一個用戶 --><select id="findUserById" parameterType="Integer" resultType="user">select * from user where id= #{sq}</select><!-- #{} === select * from user where username=     表示占位符?   sq可以隨意替代=== select * from user where username= '思琪'${} === select * from user where username like '%${value}%'  表示字符串拼接  value不可以隨意替代=== select * from user where username like '%琪%' 			sql 模糊語句查詢=== select * from user where username like "%"'琪%'"%" 		sql 模糊語句查詢=== select * from user where username like "%"#{sq}"%" --><!-- 根據用戶名稱模糊查詢用戶列表 --><select id="findUserByUsername" parameterType="String" resultType="com.pdsu.mybatis.pojo.User">select * from user where username like '%${value}%'</select><!-- 添加用戶 --><insert id="insertUser" parameterType="com.pdsu.mybatis.pojo.User"><selectKey keyProperty="id" resultType="Integer" order="AFTER" >select LAST_INSERT_ID()</selectKey>insert into user(username,birthday,address,sex) values (#{username},#{birthday},#{address},#{sex})</insert><!-- 更新用戶 --><update id="updateUserById" parameterType="com.pdsu.mybatis.pojo.User">update user set username = #{username},sex = #{sex},birthday = #{birthday},address = #{address}where id = #{id}</update>	<!-- 刪除用戶 --><delete id="deleteUserById" parameterType="Integer" >delete from user where id = #{sq}</delete></mapper>

在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379433.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379433.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379433.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

用Kotlin開發您的第一個應用程序| Android與Kotlin

In the previous article, we learned how to setup Kotlin in the android studio? Now moving to journey ahead we are going to develop our first app with Kotlin. It is the basic app, but it will let you know the structure of the program. 在上一篇文章中&#x…

數據結構與算法分析-第一章Java類(02)

編寫一個名為Person的類&#xff0c;它包含分別表示人的名字與年齡的兩個數據域。要求此類包含對其中任何一個數據域進行設置與獲取的方法。還要求包含可進行下列測試的方法&#xff1a; 兩個Person對象是否相等--即是否有相同的名稱與年齡一個人是否比另一個人年長 最后&#…

asp.net對于長篇文章進行分頁

對于文章篇幅比較長的&#xff0c;就必須采用分頁顯示。在.net中對長篇文章分頁一般有2種方法&#xff0c;第一種就是先計算好一頁的文字長度是多少&#xff0c;然后把文章總的長度除設置好的單頁文字長度及可&#xff0c;用這方法可以減少認為進行分頁的繁瑣&#xff0c;但是這…

匯編語言-004(LABEL 、間接尋址、變址操作數、指針使用、TypeDef、LOOP、DWORD變量交換高位低位字)

1&#xff1a; LABEL : 為一個標號定義大小屬性&#xff0c;但不分配內存與下一個變量共用內存&#xff0c;與C中UNION類似 .386 .model flat,stdcall.stack 4096 ExitProcess PROTO,dwExitCoed:DWORD.data val16 LABEL WORD val32 DWORD 12345678hLongValue LABEL DWORD val1…

(只需挨個復制粘貼命令即可部署)在Centos7下搭建文件服務器(VSFTPD)

觀看北京尚學堂-百戰程序員筆記一、VSFTPD簡介 Linux的組件&#xff08;一款軟件&#xff09;&#xff0c;安裝到Linux后可以通過java代碼&#xff08;FtpClient&#xff09;實現文件的上傳。基于FTP協議。 由于VSFTPD是基于FTP協議&#xff0c;客戶端瀏覽器是需要通過http協議…

POJ 2421 Constructing Roads MST kruskal

最近剛學的并查集所以用kruskal來試試最小生成樹~ kruskal其實用幾句話就能說完~ 1.貪心所有邊的權值,從小到大取值 2.取值時~將邊權非0的兩個頂點~進行并查操作~如果兩個點的祖先不同...邊權加入最小生成樹...并且將兩個點納入同一個集合中 3.判斷是否所有點都在同一個集合中…

c# 聲明類的時候初始化類_使用C#初始化的列表聲明

c# 聲明類的時候初始化類The task is to create/declare a list with an initializer list in C#. 任務是在C&#xff03;中使用初始化列表創建/聲明一個列表 。 C&#xff03;清單 (C# List) A list is used to represent the list of the objects, it is represented as Lis…

編寫程序計算所輸日期是當年的第幾天

/* 1.輸入年月日&#xff0c;編寫程序計算所輸日期是當年的第幾天 *//* 2.已知列車隔日發車&#xff0c;且1/1/2006不發車(無ticket),如果所輸入數據在此日期之后&#xff0c;則輸出有沒有車票&#xff0c;否則僅輸出上一步結果。*/ /* month/date/year is which day of the ye…

匯編語言-005(XCHG、標志位操作、算術操作、比例因子的變址尋址、多個常用運算符運用、大端轉小端、數組操作)

1: 用不超過3條XCHG指令對4個8位寄存器的值重新排序&#xff0c;A,B,C,D調整為D,C,B,A .386 .model flat,stdcall.stack 4096 ExitProcess PROTO,dwExitCode:DWORD.data.code main PROCmov al,Amov bl,Bmov cl,Cmov dl,Dxchg al,dlxchg bl,clINVOKE ExitProcess,0 main ENDP E…

bcd碼二進制轉十進制_二進制編碼的十進制(BCD碼)及其加法

bcd碼二進制轉十進制Prerequisite: Number systems 先決條件&#xff1a; 數字系統 BCD Code (8421 Code): In BCD 8421 code, each decimal digit is represented using a 4-bit binary number. The 4-bit binary numbers have their weights attached as 8, 4, 2, 1 from MS…

SVN服務器部署

一、SVN版本控制器 Subversion就是一款實現版本控制的工具軟件&#xff0c;通常也成為版本控制器&#xff0c;簡稱SVN。 Subversion是Apache軟件基金會組織下的一個項目 SVN基本操作&#xff1a; checkout&#xff08;檢出&#xff09;&#xff1a;將一個服務端創建好的項目…

rtmp流\http流測試地址

測試方式&#xff1a;ffplay rtmp://58.200.131.2:1935/livetv/cctv1 rtmp&#xff1a; CCTV-1綜合:rtmp://58.200.131.2:1935/livetv/cctv1 CCTV-2財經:rtmp://58.200.131.2:1935/livetv/cctv2 CCTV-3綜藝:rtmp://58.200.131.2:1935/livetv/cctv3 CCTV-4中文國際:rtmp://58.2…

LINQ to XML:如何讀寫XCData

using System;using System.Xml.Linq;namespace ConsoleApplication1 {class Program{static void Main(string[] args){//寫入CDATA元素塊var doc new XElement("Test",new XElement("User",new XAttribute("name", "chenxizhang"),…

C#中的結構和類之間的區別

C&#xff03;類和結構 (C# class and structure) In C# and other programming languages, structure and classes are used to define a custom data type, that we can organize according to our need with different types of variables, methods etc. 在C&#xff03;和其…

[轉載]SQL?Plus?一些使用技巧

原文地址&#xff1a;SQL Plus 一些使用技巧作者&#xff1a;☆水『若寒Sql*plus的使用 Sql*plus介紹 Sql*plus是oracle提供的一個工具程序&#xff0c;既可以在oracle服務器使用&#xff0c;也可以在oracle客戶端使用。在windows下分兩種&#xff0c;sqlplus.exe是命令行程序&…

云服務器(Centos)部署SVN

1&#xff0c;安裝svn yum install subversion 2&#xff0c;查看版本號 svnserve --version 3&#xff0c;創建SVN版本庫&#xff08;在var/svn 文件夾下&#xff09; 新建文件夾 mkdir -p /var/svn/svnrepos 創建版本庫 svnadmin create /var/svn/svnrepos 4&#xff0c;修改…

ffmpeg命令提取像素格式

1&#xff1a; 提取yuv格式&#xff1a;不修改寬高 取3秒 ffmpeg -i test_1920x1080.mp4 -t 3 yuv420p_orig.yuv ffmpeg -i test_1920x1080.mp4 -t 3 -pix_fmt yuv420p yuv420p_orig.yuv 可以使用ffplay播放&#xff1a;ffplay -video_size 1920x1080 yuv420p_orig.yuv 提取y…

Javascript(js)使用function定義構造函數

Javascript并不像Java、C#等語言那樣支持真正的類。但是在js中可以定義偽類。做到這一點的工具就是構造函數和原型對象。首先介紹js中的構造函數。 Javascript中創建對象的語法是在new運算符的后面跟著一個函數的調用。如 1 varobj newObject();2 vardate newDate();運算符new首…

錯誤:將字符串分配給C中的char變量| 常見的C程序錯誤

If you assign a string to the character variable, it may cause a warning or error (in some of the compilers) or segmentation fault error occurs. 如果將字符串分配給字符變量&#xff0c;則可能會導致警告或錯誤(在某些編譯器中)或發生分段錯誤。 Consider the code…

【轉】用BibTeX 寫 Reference

BibTeX 是一種格式和一個程序&#xff0c; 用于協調LaTeX的參考文獻處理&#xff0c;BibTeX 使用數據庫的的方式來管理參考文獻.&#xff0c;BibTeX 文件的后綴名為 .bib。 例子&#xff1a; article{name1, author {作者, 多個作者用 and 連接}, title {標題}, journal {期…