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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

整合mybatis——使用纯注解整合、使用Mapper+Mapper.xml整合、使用mybatis.cfg.xml整合

發布時間:2025/4/16 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 整合mybatis——使用纯注解整合、使用Mapper+Mapper.xml整合、使用mybatis.cfg.xml整合 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

引入druid

<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.21</version> </dependency>

修改yml配置數據源

server:port: 8080 spring:datasource: #數據源配置driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/dd?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTCusername: rootpassword: rootdruid:max-active: 10min-idle: 5max-wait: 5000initial-size: 5validation-query: select 1stat-view-servlet:enabled: truelogin-username: adminlogin-password: adminallow:deny:url-pattern: "/druid/*"

排除DataSourceAutoConfiguration配置數據源



創建User

package com.sxt.domain;import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;import java.io.Serializable; import java.util.Date;@Data @AllArgsConstructor @NoArgsConstructor public class User implements Serializable {private Integer id;private String name;private String address;private Date birth; }

使用純注解整合

創建UserMapper

package com.sxt.mapper;import com.sxt.domain.User; import org.apache.ibatis.annotations.*;import java.util.List;//@Mapper public interface UserMapper {@Delete("delete from sys_user where id=#{id}")int deleteByPrimaryKey(@Param("id")Integer id);@Insert("insert into sys_user(name,address,birth) values(#{name},#{address},#{birth})")int insert(User user);@Select("select * from sys_user where id=#{value}")User selectByPrimaryKey(@Param("id")Integer id);@Update("update sys_user set name=#{name},address=#{address},birth=#{birth} where id=#{id}")int updateByPrimaryKey(User user);@Select("select * from sys_user")List<User> queryAllUser(); }

配置掃描

方式1在每一個Mapper上加@Mapper

方式2在啟動類上加@MapperScan(basePackage={“com.sxt.mapper”})


測試



使用Mapper+Mapper.xml整合

修改UserMapper去掉注解

package com.sxt.mapper;import com.sxt.domain.User; import org.apache.ibatis.annotations.*;import java.util.List;public interface UserMapper {int deleteByPrimaryKey(@Param("id")Integer id);int insert(User user);User selectByPrimaryKey(@Param("id")Integer id);int updateByPrimaryKey(User user);List<User> queryAllUser(); }

創建resources/mapper/UserMapper.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" > <mapper namespace="com.sxt.mapper.UserMapper"><!--刪除--><delete id="deleteByPrimaryKey" >delete from sys_user where id=#{id}</delete><!--添加--><insert id="insert">insert into sys_user(name,address,birth) values(#{name},#{address},#{birth})</insert><!--查詢一個--><select id="selectByPrimaryKey" resultType="com.sxt.domain.User">select * from sys_user where id=#{value}</select><!--修改--><update id="updateByPrimaryKey">update sys_user set name=#{name},address=#{address},birth=#{birth} where id=#{id}</update><!--全查詢--><select id="queryAllUser" resultType="com.sxt.domain.User">select * from sys_user</select> </mapper>

修改yml

測試



創建mybatis.cfg.xml

<?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><settings><setting name="logImpl" value="LOG4J"/></settings><mappers><mapper resource="mapper/UserMapper.xml"></mapper></mappers> </configuration>

修改yml

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

總結

以上是生活随笔為你收集整理的整合mybatis——使用纯注解整合、使用Mapper+Mapper.xml整合、使用mybatis.cfg.xml整合的全部內容,希望文章能夠幫你解決所遇到的問題。

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