mybaits的基本使用
导入相关包
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
配置文件
主要配置文件中的参数
- mappers配置(必填)
- environments(必填)
- settiong配置(非必要)
- properties加载(非必要)
- 别名配置(非必要)
配置模板(以阿里Durid链接池为例)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--加载 property-->
<context:property-placeholder location="classpath:config/db.properties"/>
<!--自动扫描包-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 初始化 配置数据库连接池-->
<property name="dataSource" ref="dataSource"/>
<!-- 3 加载主配置文件-->
<property name="configLocation" value="classpath:mybaits-config.xml"/>
<!-- 4 注册 所有的mapper.xml-->
<property name="mapperLocations" value="classpath:mappers/**/*.xml" />
</bean>
<!-- 5 注册 注册的Mapper.java -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.qf.ssm.mapper"/>
</bean>
<!-- 2 配置 druid -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<!-- 常用配置 大量的数据库连接超时的 适当的增加 -->
<property name="maxActive" value="20" />
<property name="minIdle" value="1" />
<property name="initialSize" value="1" />
<property name="filters" value="stat" />
<property name="maxWait" value="60000" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="20" />
<property name="asyncInit" value="true" />
</bean>
</beans>
配置连接池驱动(以阿里Durid)
db.username=root
db.password=root
db.url=jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
映射使用
在mapper层写出相应的数据库操作接口
public interface ShopMapper {
public List<Shop> findShopbyCartId(@Param("shop_id") int shopId);
}
@Param注解
用于mapper.xml中调用
在mapper实现层写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.qf.ssm.mapper.ShopMapper">
<select id="findShopbyCartId" resultType="com.qf.ssm.entity.Shop">
SELECT shop_id, title, name, price, old_price, img, num, create_date, status FROM shop WHERE shop_id = #{shop_id}
</select>
</mapper>
SQL映射文件的几个元素
cache – 给定命名空间的缓存配置。
cache-ref – 其他命名空间缓存配置的引用。
resultMap – 是最复杂也是最强大的元素,用来描述如何从数据库结果集中来加载对象。
sql – 可被其他语句引用的可重用语句块。
insert – 映射插入语句
update – 映射更新语句
delete – 映射删除语句