Springboot项目学习笔记——01

mapper.xml模板

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cloud.mapper.UserMapper">

数据库连接yml模板 

spring:
  datasource:
    username: root
    password: system
    url: jdbc:mysql://localhost:3306/cloud_test?useUnicode=true&characterEncoding=utf
    driver-class-name: com.mysql.cj.jdbc.Driver

resultMap与resultType

  • resultType:  表示查询的结果集。只需要指定对应类的类型,并且包含完整的报的接口
  • resultMap:当表的资 资源与类的对象属性的字段名称不一致时,自定义查询结果集的映射

resultMap 

  • id:分配一个唯一id,对应就是resultMap=“id”
  • type: 数据库中查询结果与Java实体类对应的结果映射集
  • 表字段与类属性不一致的进行配置
  • 定义映射规则时主键不能省
    <resultMap id="UserEntityMap" type="com.cloud.entity.User">
              <id column="uid" property="uid"></id>
              <result column="create_user" property="createUser"></result>
              <result column="is_delete" property="isDelete"></result>
              <result column="create_time" property="createUser"></result>
              <result column="modifed_user" property="modifedUser"></result>
              <result column="modifed_time" property="modifedTime"></result>
    </resultMap>
    
         <select id="FindByUsername" resultMap="UserEntityMap">
              select * from t_user where username = #{username}
         </select>

Dao层

 user.class

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User extends BaseEntity implements Serializable {

    private Integer uid;
    private String username;
    private String password;
    private String salt;
    private String phone;
    private String email;
    private Integer gender;
    private String avatar;
    private Integer isDelete;

}

BaseEntity

@Data
@AllArgsConstructor
@NoArgsConstructor
public class BaseEntity implements Serializable {

    private Date createTime;
    private String modifedUser;
    private Date modifedTime;
    private String createUser;
}

mapper层

 UserMapper.class

@Mapper
public interface UserMapper {

    /*
     * 功能描述:插入数据
     * @Author baoyunlong
     * @Description //TODO
     * @Date 16:03 2021/12/22
     * @Param
     * @return
     **/
    Integer insert(User user);

    /*
     * 功能描述:根据用户名查询
     * @Author baoyunlong
     * @Description //TODO
     * @Date 16:04 2021/12/22
     * @Param
     * @return
     **/
    User FindByUsername(String username);

}

UserMapper.xml 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cloud.mapper.UserMapper">
     <resultMap id="UserEntityMap" type="com.cloud.entity.User">
          <id column="uid" property="uid"></id>
          <result column="create_user" property="createUser"></result>
          <result column="is_delete" property="isDelete"></result>
          <result column="create_time" property="createUser"></result>
          <result column="modifed_user" property="modifedUser"></result>
          <result column="modifed_time" property="modifedTime"></result>
     </resultMap>

    <insert id="insert" useGeneratedKeys="true" keyProperty="uid">
        insert INTO t_user (
             username,
             password,
             salt,
             phone,
             email,
             gender,
             avatar,
             create_user,
             create_time,
             modifed_user,
             modifed_time
        ) values {
             #{username},
             #{password},
             #{salt},
             #{phone},
             #{email},
             #{gender},
             #{avatar},
             #{iselete},
             #{isDelete},
             #{createUser},
             #{createTime},
             #{modifedUser},
             #{modifedTime}
        }
    </insert>
     <select id="FindByUsername" resultMap="UserEntityMap">
          select * from t_user where username = #{username}
     </select>

</mapper>

Mapper测试类

@SpringBootTest:表示当前是一个测试类,不会随着项目一同打包

@RunWith:表示启动这个单元测试类(单元测试类时不能够运行的),需要传递一个参数,必须是SpringRunner实例类型

自动装配UserMapper接口自动装配报错问题

idea有接口检测功能,接口不能直接创建Bean(动态代理解决) 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值