mybatis 相关

基础知识

是什么: 

  • 是优秀的持久层框架,持久层(Persistence Layer)是指负责将应用程序的数据存储到持久存储介质(如数据库)中的部分。
  • 是ORM(Object Relational Mapping)框架,即对象关系映射
  • 通过 XML 或注解的方式提供了一种将数据库操作与 Java 对象之间的映射关系进行配置的方式
  • 封装了JDBC,去除了几乎所有的 JDBC 代码

怎么理解:

  • 一个表理解为一个类,一条记录理解为对象,列理解为对象的属性
  • 第一步:传入的数据(对象)——》转为原生的SQL
  • 第二步:SQL获得的结果集 ——》映射为对象

怎么使用:

第一步:在 mapper(interface)里面添加增删改方法的声明

public interface UserMapper {
    List<UserEntity> listUser(@Param("id") String id);
}

第二步:在 XMl 中添加 增删改标签和对应的 sql 代码 或者 直接在mapper接口中注解方式(@select,@param,@delete,@insert,@update)

  <select id="listUser" parameterType="java.lang.String" resultMap="BaseResultMap">
        select <include refid="BASE_COLUMN" />
        from t_user
        where expose = 1 and id = #{id}
 </select>
public interface UserMapper {
    @Select("select " + sql_columns + " from " + table + " where id= #{id}")
    CpfRecommendEntity selectById(@Param("id") long id);
 
}

第三步:在 UserMapper 中右键 Generate 点击 Test 生成测试类

提升:

  • 占位符#{} 和 ${}

                #{}:更安全,不会有sql注入;int string 都可以,自动加引号

                ${}:只适合于int;优点:可以排序 desc or  asc(可以穷举的尽)

  • resultMap标签:解决数据库中表字段 和 实体定义字段不一致
  • collection : eg:部门表,员工表 1对多
  • association:两个表关联,eg客户表,订单表,1对1

mybatis注解

@Mapper和@MapperScan

@Mapper注解

功能:在接口类上添加了@Mapper,在编译之后会生成相应的接口实现类。

添加位置:接口类上

@Mapper
public interface UserMapper {
   //
}

 @MapperScan注解

功能:指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类。

添加位置:是在Springboot启动类上面添加


@SpringBootApplication
@MapperScan("com.application.mapper")
public class SpringbootMybatisDemoApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisDemoApplication.class, args);
    }

}

mybatis 和 JDBC  

mybatis 数据库连接池 :Druid

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid-version}</version>
        </dependency>
import com.alibaba.druid.pool.DruidDataSource;

public DataSource dataSource() {
        DruidDataSource datasource = new DruidDataSource();

        //配置数据源属性
        datasource.setUrl(url);
        datasource.setUsername(userName);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);

        //配置统一属性
        datasource.setInitialSize(initialSize);
        datasource.setMinIdle(minIdle);
        datasource.setMaxActive(maxActive);
        datasource.setMaxWait(maxWait);
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        datasource.setValidationQuery(validationQuery);
        datasource.setTestWhileIdle(testWhileIdle);
        datasource.setTestOnBorrow(testOnBorrow);
        datasource.setTestOnReturn(testOnReturn);
        datasource.setPoolPreparedStatements(poolPreparedStatements);
        try {
            datasource.setFilters(filters);
        } catch (SQLException e) {
            log.error("DruidConfig 类---Druid configuration initialization filter error.", e);
        }
        return datasource;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值