Mybatis初體驗(二)
一、代理開發規則
① mapper.xml檔案中namespace與mapper介面的全限定名相同。
② mapper介面方法名和mapper.xml中定義的每個statement的id相同。
③ mapper介面方法的傳入引數型別和mapper.xml中定義的每個sql的parameterType的型別相同,並且返回引數型別的resultType的型別相同。
如圖:
二、Mybatis對映檔案配置
- select:查詢
<select id="findAll" resultType="com.itcast.domain.user"> select * from user </select>
- insert:插入
<insert id="save" parameterType="com.itcast.domain.user" > insert into user values (#{Id},#{userName},#{passWord}) </insert>
- update:修改
<update id="edit" parameterType="com.itcast.domain.user"> update user set username=#{userName},password=#{passWord} where id=#{Id} </update>
- delete:刪除
<delete id="remove" parameterType="int"> delete from user where id=#{id} </delete>
- where:where條件
<mapper namespace="com.itcast.dao.UserMapper"> <sql id="query"> select * from userInfo </sql> <select id="queryByWhere" resultType="com.itcast.domain.user" parameterType="com.itcast.domain.user"> <include refid="query"></include> <where> <if test="id!=0"> and id=#{id} </if> <if test="userName!=null"> and username=#{userName} </if> <if test="passWord!=null"> and password=#{passWord} </if> </where> </select> </mapper>
- if:判斷,同上
- foreach:迴圈
<?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"> <mapper namespace="com.itcast.dao.UserMapper"> <sql id="query"> select * from user </sql> <select id="queryByIds" parameterType="list" resultType="com.itcast.domain.user"> <include refid="query"></include> <where> <foreach collection="list" open=" id in(" close=")" separator="," item="id"> #{id} </foreach> </where> </select> </mapper>
- sql:sql片段抽取 通常與 include 標籤一起進行使用
三、自定義型別轉換處理器(typeHandlers)
- 定義型別轉換處理器類繼承 org.apache.ibatis.type.BaseTypeHandler 下的 BaseTypeHandler<T>類
- 實現BaseTypeHandler<T>類中的四個抽象方法,其中 setNonNullParameter 為java程式設定資料到資料庫的回撥方法,getNullableResult為查詢資料庫的字串型別轉換成java的Type型別的方法。
- Mybatis核心配置檔案中註冊
public class DateTypeHandler extends BaseTypeHandler<Date> {
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) throws SQLException {
long time = date.getTime();
preparedStatement.setLong(i, time);
}
@Override
public Date getNullableResult(ResultSet resultSet, String s) throws SQLException {
long aLong = resultSet.getLong(s);
Date date = new Date(aLong);
return date;
}
@Override
public Date getNullableResult(ResultSet resultSet, int i) throws SQLException {
long aLong = resultSet.getLong(i);
Date date = new Date(aLong);
return date;
}
@Override
public Date getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
long aLong = callableStatement.getLong(i);
Date date = new Date(aLong);
return date;
}
}
<typeHandlers>
<typeHandler handler="com.itcast.handler.DateTypeHandler"></typeHandler>
</typeHandlers>
四、分頁外掛pageHelper
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>3.2</version>
</dependency>
sqlMapConfig.xml中註冊:
<plugins>
<!-- 配置分頁助手 4.0.0版本後不實現PageHelper,自動匹配資料庫 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 指定方言 -->
<!-- <property name="dialect" value="mysql"/>-->
</plugin>
</plugins>
五、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"></properties>
<typeAliases>
<typeAlias type="com.itcast.domain.UserInfo" alias="user"></typeAlias>
</typeAliases>
<typeHandlers>
<typeHandler handler="com.itcast.handler.DateTypeHandler"></typeHandler>
</typeHandlers>
<plugins>
<!-- 配置分頁助手 4.0.0版本後不實現PageHelper,自動匹配資料庫 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 指定方言 -->
<!-- <property name="dialect" value="mysql"/>-->
</plugin>
</plugins>
<environments default="developement">
<environment id="developement">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/itcast/mapper/UserInfoMapper.xml"></mapper>
</mappers>
</configuration>
相關文章
- 【springboot】【java】【MySQL】【mybatis】【db】mybatis初體驗Spring BootJavaMySqlMyBatis
- Angular 初體驗Angular
- http初體驗HTTP
- AQS初體驗AQS
- golang 初體驗Golang
- OpenCV 初體驗OpenCV
- Nuxt 初體驗UX
- indexedDB 初體驗Index
- laravel初體驗Laravel
- Flutter初體驗Flutter
- jQuery初體驗jQuery
- ollama 初體驗
- Electron初體驗
- go modules 初體驗Go
- ReactNative初體驗React
- react hooks初體驗ReactHook
- Laravel Octane 初體驗Laravel
- gRPC初體驗RPC
- Vue 初體驗(上)Vue
- Canvas 動畫初體驗Canvas動畫
- html初體驗#2HTML
- Docker容器初體驗Docker
- Docker for Mac 初體驗DockerMac
- Docker 容器初體驗Docker
- 表分析初體驗
- .Net Aspire初體驗
- SpringMVC初體驗SpringMVC
- web assembly 初體驗Web
- Argo CD初體驗Go
- 微服務新體驗之Aspire初體驗微服務
- strimzi實戰之二:部署和訊息功能初體驗
- 初識MybatisMyBatis
- PyQt5 初體驗QT
- golang 與 docker 初體驗GolangDocker
- Python初體驗——列表Python
- Vue.js初體驗Vue.js
- React Native 初體驗React Native
- Go初體驗|基礎Go