整合mybatis——使用纯注解整合、使用Mapper+Mapper.xml整合、使用mybatis.cfg.xml整合
生活随笔
收集整理的這篇文章主要介紹了
整合mybatis——使用纯注解整合、使用Mapper+Mapper.xml整合、使用mybatis.cfg.xml整合
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
引入druid
<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.21</version> </dependency>修改yml配置數(shù)據(jù)源
server:port: 8080 spring:datasource: #數(shù)據(jù)源配置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配置數(shù)據(jù)源
創(chuàng)建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; }使用純注解整合
創(chuàng)建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在每一個(gè)Mapper上加@Mapper
方式2在啟動(dòng)類上加@MapperScan(basePackage={“com.sxt.mapper”})
測(cè)試
使用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(); }創(chuàng)建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><!--查詢一個(gè)--><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
測(cè)試
創(chuàng)建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
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的整合mybatis——使用纯注解整合、使用Mapper+Mapper.xml整合、使用mybatis.cfg.xml整合的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用druid【使用官方的stater】
- 下一篇: 整合分页pageHelper||整合分页