@File
2019-09-07T07:23:32.000000Z
字数 5100
阅读 210
java
<!-- mysql数据库包 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.17</version></dependency><!-- mybatis包 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.2</version></dependency><!-- spring支持包 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.2</version></dependency><!-- 连接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.19</version></dependency>
db.username=rootdb.password=rootdb.url=jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
<?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 http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"><!-- spring 加载 properties 配置文件 --><context:property-placeholder location="classpath:/config/db.properties"/><!-- 初始化 SqlSessionFactory --><bean class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 初始化 配置数据库连接池 --><property name="dataSource" ref="dataSource"/><!-- 注册所有的mapper.xml--><property name="mapperLocations" value="classpath:mappers/**/*.xml" /><!-- 配置类型别名 --><property name="typeAliasesPackage" value="com.lidaye.ssm.entity"/></bean><!-- 注册的Mapper.java --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.lidaye.ssm.mapper"/></bean><!-- 配置 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>
mapper 影响的mappernamespace: mapper接口的包名和类名
<?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接口 --><mapper namespace="com.lidaye.ssm.mapper.HelloMapper"><!-- 执行的方法指向的sql --></mapper>
参数
id: 执行的方法名
parameterType: 参数类型(一般改用参数设定时的@Param声明)
parameterMap
flushCache:
timeout:
databaseId:
lang:
statementType:
语法
#{}: 获取参数(使用占位符替换)
${}: 获取参数(直接替换结果)
if
test: 条件表达式
choose
select 查询resultType: 指定映射类接口
resultMap:
useCache:
fetchSize:
resultSetType:
resultOrdered:
resultSets:
<select id="findById" resultType="Shop"><!-- 查询sql --></select>
insert 添加keyProperty: 指定主键属性名
keyColumn: 指定数据表主键所在的列
useGeneratedKeys: 是否要返回主键,默认false
<!-- 方法一 --><insert id="save" keyProperty="shop.sid" useGeneratedKeys="true"><!-- 新增sql --></insert><!-- 方法二 --><insert id="save" parameterType="Shop" keyProperty="sid" useGeneratedKeys="true"><!-- 新增sql --></insert><!-- 方法三 --><insert id="save"><!-- 新增sql -->INSERT INTO...<!-- 获取id并设置到映射对象中 --><selectKey keyProperty="shop.sid" resultType="integer" order="AFTER">SELECT last_insert_id() AS sid;</selectKey></insert>
update 修改
<update id="setName"><!-- 修改sql --></update>
delete 删除
<delete id="del"><!-- 删除sql --></delete>
sql 定义sql<include>在具体sql实现是调用 id: 声明注册名
<!-- 定义一个条件 --><sql id="select_where">`user` = '李大爷'</sql>
include 引入sqlrefid: 通过
<sql>注册的id
<!-- 在查询中引入已定义的sql --><select id="findAll" resultType="Shop">SELECT * FROM <include refid="select_where" /></select>
resultMapid: 当前map的注册名
type: 映射表
extends: 继承另一个resultMap
id 主键property: 属性名
column: 数据表字段名
jdbcType: 数据库类型
result 普通属性(和主键一样)constructor 构造方法<idAge> 主键参数<age> 普通参数collection 关联多条数据property: 属性名
ofType: 关联的映射类
select: 对应的查询方法(不用,用连biao)
column: 关联字段(一般是外键)
resultMap: 引入已注册的resultMap
<id> 和 resultMap 的id一样<result> 和 result 的id一样association 关联一条数据javaType: 关联的映射类
resultMap 示例
<resultMap id="userCartMap" type="User"><!-- 主键映射 --><id property="uid" column="uid" /><!-- 其他字段映射 --><result property="userName" column="username"/><result property="isSupper" column="is_supper"/><!-- 关联映射 --><collection property="carts" resultMap="com.lidaye.ssm.mapper.CartMapper.cartShopMap" /></resultMap>
@Param 声明参数
// 使用示例public interface ShopMapper {Shop findById(@Param("num") int num);}