[关闭]
@File 2019-09-07T07:23:32.000000Z 字数 5100 阅读 134

mybatis 配置

java


依赖包

  1. <!-- mysql数据库包 -->
  2. <dependency>
  3. <groupId>mysql</groupId>
  4. <artifactId>mysql-connector-java</artifactId>
  5. <version>8.0.17</version>
  6. </dependency>
  7. <!-- mybatis包 -->
  8. <dependency>
  9. <groupId>org.mybatis</groupId>
  10. <artifactId>mybatis</artifactId>
  11. <version>3.5.2</version>
  12. </dependency>
  13. <!-- spring支持包 -->
  14. <dependency>
  15. <groupId>org.mybatis</groupId>
  16. <artifactId>mybatis-spring</artifactId>
  17. <version>2.0.2</version>
  18. </dependency>
  19. <!-- 连接池 -->
  20. <dependency>
  21. <groupId>com.alibaba</groupId>
  22. <artifactId>druid</artifactId>
  23. <version>1.1.19</version>
  24. </dependency>

db.properties 数据库连接配置(根据配置文件定义)

  1. db.username=root
  2. db.password=root
  3. db.url=jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai

创建配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. 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 http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
  6. <!-- spring 加载 properties 配置文件 -->
  7. <context:property-placeholder location="classpath:/config/db.properties"/>
  8. <!-- 初始化 SqlSessionFactory -->
  9. <bean class="org.mybatis.spring.SqlSessionFactoryBean">
  10. <!-- 初始化 配置数据库连接池 -->
  11. <property name="dataSource" ref="dataSource"/>
  12. <!-- 注册所有的mapper.xml-->
  13. <property name="mapperLocations" value="classpath:mappers/**/*.xml" />
  14. <!-- 配置类型别名 -->
  15. <property name="typeAliasesPackage" value="com.lidaye.ssm.entity"/>
  16. </bean>
  17. <!-- 注册的Mapper.java -->
  18. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  19. <property name="basePackage" value="com.lidaye.ssm.mapper"/>
  20. </bean>
  21. <!-- 配置 druid -->
  22. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  23. <property name="url" value="${db.url}" />
  24. <property name="username" value="${db.username}" />
  25. <property name="password" value="${db.password}" />
  26. <!-- 常用配置 大量的数据库连接超时的 适当的增加 -->
  27. <property name="maxActive" value="20" />
  28. <property name="minIdle" value="1" />
  29. <property name="initialSize" value="1" />
  30. <property name="filters" value="stat" />
  31. <property name="maxWait" value="60000" />
  32. <property name="timeBetweenEvictionRunsMillis" value="60000" />
  33. <property name="minEvictableIdleTimeMillis" value="300000" />
  34. <property name="testWhileIdle" value="true" />
  35. <property name="testOnBorrow" value="false" />
  36. <property name="testOnReturn" value="false" />
  37. <property name="poolPreparedStatements" value="true" />
  38. <property name="maxOpenPreparedStatements" value="20" />
  39. <property name="asyncInit" value="true" />
  40. </bean>
  41. </beans>

创建sql管理文件

mapper 影响的mapper

namespace: mapper接口的包名和类名

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <!-- 指定mapper接口 -->
  4. <mapper namespace="com.lidaye.ssm.mapper.HelloMapper">
  5. <!-- 执行的方法指向的sql -->
  6. </mapper>

公共

  1. 参数

    id: 执行的方法名
    parameterType: 参数类型(一般改用参数设定时的@Param声明)
    parameterMap
    flushCache:
    timeout:
    databaseId:
    lang:
    statementType:

  2. 语法

    #{}: 获取参数(使用占位符替换)
    ${}: 获取参数(直接替换结果)

  3. if

    test: 条件表达式

  4. choose

  5. trim
  6. foreach
  7. where
  8. set

select 查询

resultType: 指定映射类接口
resultMap:
useCache:
fetchSize:
resultSetType:
resultOrdered:
resultSets:

  1. <select id="findById" resultType="Shop">
  2. <!-- 查询sql -->
  3. </select>

insert 添加

keyProperty: 指定主键属性名
keyColumn: 指定数据表主键所在的列
useGeneratedKeys: 是否要返回主键,默认false

  1. <!-- 方法一 -->
  2. <insert id="save" keyProperty="shop.sid" useGeneratedKeys="true">
  3. <!-- 新增sql -->
  4. </insert>
  5. <!-- 方法二 -->
  6. <insert id="save" parameterType="Shop" keyProperty="sid" useGeneratedKeys="true">
  7. <!-- 新增sql -->
  8. </insert>
  9. <!-- 方法三 -->
  10. <insert id="save">
  11. <!-- 新增sql -->
  12. INSERT INTO...
  13. <!-- 获取id并设置到映射对象中 -->
  14. <selectKey keyProperty="shop.sid" resultType="integer" order="AFTER">
  15. SELECT last_insert_id() AS sid;
  16. </selectKey>
  17. </insert>

update 修改

  1. <update id="setName">
  2. <!-- 修改sql -->
  3. </update>

delete 删除

  1. <delete id="del">
  2. <!-- 删除sql -->
  3. </delete>

sql 定义sql

  1. <!-- 定义一个条件 -->
  2. <sql id="select_where">`user` = '李大爷'</sql>

include 引入sql

refid: 通过 <sql> 注册的id

  1. <!-- 在查询中引入已定义的sql -->
  2. <select id="findAll" resultType="Shop">
  3. SELECT * FROM <include refid="select_where" />
  4. </select>

resultMap

id: 当前map的注册名
type: 映射表
extends: 继承另一个resultMap

1. id 主键

property: 属性名
column: 数据表字段名
jdbcType: 数据库类型

2. result 普通属性(和主键一样)
3. constructor 构造方法
4. collection 关联多条数据

property: 属性名
ofType: 关联的映射类
select: 对应的查询方法(不用,用连biao)
column: 关联字段(一般是外键)
resultMap: 引入已注册的resultMap

5. association 关联一条数据

javaType: 关联的映射类

resultMap 示例
  1. <resultMap id="userCartMap" type="User">
  2. <!-- 主键映射 -->
  3. <id property="uid" column="uid" />
  4. <!-- 其他字段映射 -->
  5. <result property="userName" column="username"/>
  6. <result property="isSupper" column="is_supper"/>
  7. <!-- 关联映射 -->
  8. <collection property="carts" resultMap="com.lidaye.ssm.mapper.CartMapper.cartShopMap" />
  9. </resultMap>

注解

@Param 声明参数

  1. // 使用示例
  2. public interface ShopMapper {
  3. Shop findById(@Param("num") int num);
  4. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注