MyBatis一些学习的记录

MyBatis是一款基于JDBC来开发的持久层框架.

一、配置

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=false//数据库的url
    username: root//用户名
    password: root//密码
    driver-class-name: com.mysql.cj.jdbc.Driver对应的依赖
mybatis:
  configuration: # 配置打印 MyBatis日志
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl//打印日志到控制台
    map-underscore-to-camel-case: true #配置驼峰自动转换
# 配置 mybatis xml 的文件路径,在 resources/mapper 创建所有表的 xml 文件
  mapper-locations: classpath:mapper/**Mapper.xml

  • 采用注释方式使用Mybatis
  1. @Mapper就是负责管理MyBatis代码的注解,他会生成一个对象交给spring.同时我们在定义的时候要使用接口.注意返回值要与SQL语句的返回对应起来,比如插入语句返回的是受影响语句的条数,要用Integer做返回值.可以没有但不能错.

@Mapper
public interface UserInfoMapper {
    @Select("select * from userinfo")
    public List<UserInfo> Select1();

  1. 参数的传递: 如果只有一个参数那么参数的名字不重要会自动匹配,不过建议写成一样的.同时也可以使用@Param注解对参数重命名注解里的新名字与查询语句对应上就行.

@Select("select * from userinfo where id = #{id}")
public List<UserInfo> select2(Integer id);

@Select("select * from userinfo where id = #{id}")
public List<UserInfo> select3(@Param("id") Integer userId);

同时如果参数是对象就要进行一些额外操作,首先参数作为对象不进行重命名的时候是可以正常传参的如果重命名了就要使用替代#{对象名.参数};

@Select("select * from userinfo where id = #{user.id}")
public List<UserInfo> select4(@Param("user") UserInfo userInfo);

当对象作为返回值的时候,MyBatis把返回的值根据名字,一一对应的赋值给返回值的对象,当名字不同的时候就不会进行赋值.但是java有java的命名规则,MySQL有MySQL的.此时我们就要做一些处理来告诉java或者告诉MySQL谁对应谁.这里有三种方法

a.要求MySQL返回的数据要用什么名字,也就是通过as修改

@Select("select username as zhangsan from userinfo")
public List<UserInfo> select5();

b.告诉MyBatis返回的结果谁对应谁,然后把返回的结果让根据这个对应关系填充进返回值.id表示把这个对应关系作为一个组,下一个语句想要就不用写里面的内容了而是写一个

@ResultMap(value = "res")

@Results(id = "res" ,value = {
        @Result(column = "delete_flag", property = "deleteFlag")
})
@Select("select * from userinfo where id = 20")
public List<UserInfo> select6();

c.虽然命名规则不同,但我们仔细观察还是有一定规律的,如果我们就可以直接告诉MyBatis我们要按照这个规定命名.

在mapper里配置这样一个规则.

mybatis: configuration: map-underscore-to-camel-case: true #配置驼峰⾃动转换

  1. 插入语句:要注意语句中的字符串要么用’引起来,要么用/”转义后引起来.也可以使用参数.

@Insert("insert into userinfo (username, `password`, age, gender, phone) values('zhaoliu','zhaoliu',19,1,'18700001234')")
public Integer insert1();

@Insert("insert into userinfo (username, `password`, age, gender, phone) values(#{username},'zhaoliu',19,1,'18700001234')")
public Integer insert2(String username);

Insert默认返回受影响的行数,但在有些场景下需要自增主键这时我们可以使用@Option注解,注意这个主键并不是把返回值改成主键而是注入到你的参数中所以参数要是个对象且包含id这个属性,并提供get和set.

@Insert("insert into userinfo (username, `password`, age, gender, phone) " +
        "values(#{username},'zhaoliu',19,1,'18700001234')")
public Integer Insert3(UserInfo userInfo);

@Test
void insert3() {
    UserInfo userInfo = new UserInfo();
    userInfo.setUsername("aaa");
    Integer result = userInfoMapper.Insert3(userInfo);
    System.out.println(result + "  " + userInfo.getId());
}

  1. Delete:通常数据时很重要的所以删除也只是逻辑上的删除,比如给他标识为已删除

@Delete("delete from userinfo where id = 1")
public Integer Delete();

  1. Update

@Update("update userinfo set username = 'lege' where id = 20")
public Integer update();

  • 使用XML来使用MyBatis
  1. 选择语句,首先与什么语句无关,namespace的值是对应接口的全限定类名下面的语句的id对应的是方法名,resultType对应的是返回值的类型,如果是集合那就是集合里的元素的类型.

<mapper namespace="com.example.demo.Mapper.UserInfoMapperByXml">
    <select id="select1" resultType="com.example.demo.model.UserInfo">
        select * from userinfo
    </select>
</mapper>

  1. 插入语句,如果有参数其实是和注释里的语句写法一样的

<insert id="insert1" useGeneratedKeys="true" keyProperty="id">
    insert into userinfo (username, `password`, age, gender, phone)
    values(#{username},'zhaoliu',19,1,'18700001234')
</insert>

  1. 删除语句

<delete id="delete1">
    delete from userinfo where id = 24
</delete>

  1. 修改语句

@Test
void update1() {
    userInfoMapperByXml.update1();
}

数据库连接池

减少创建与销毁的开销.

常见的数据库连接有池C3P0 DBCP  Druid  Hikari(SpringBoot默认的)

  • 18
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值