生成器
- maven
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<!-- Specifies whether the mojo writes progress messages to the log.
-->
<!-- 指定mojo是否将进度消息写入日志。-->
<verbose>true</verbose>
<!-- Specifies whether the mojo overwrites existing Java files. Default is false.
Note that XML files are always merged.-->
<!-- 是否覆盖原生成JAVA文件, xml文件永远都是合并 -->
<overwrite>true</overwrite>
</configuration>
</plugin>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
- 生成器配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC
"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
<classPathEntry
location="C:\Users\fenqi\.m2\repository\mysql\mysql-connector-java\8.0.11\mysql-connector-java-8.0.11.jar"/>
<context id="context" targetRuntime="MyBatis3Simple">
<commentGenerator>
<property name="suppressAllComments" value="false"/>
<property name="suppressDate" value="false"/>
</commentGenerator>
<jdbcConnection userId="root" password="pwd" driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/databaseName?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&nullCatalogMeansCurrent=true"/>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
<property name="useJSR310Types" value="true"/>
</javaTypeResolver>
<javaModelGenerator targetPackage="cn.tiantianquan.springant.mybatis.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="cn.tiantianquan.springant.mybatis.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<javaClientGenerator targetPackage="cn.tiantianquan.springant.mybatis.mapper" type="XMLMAPPER" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<table schema="spring_ant_demo" tableName="pm_authority" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
<table schema="spring_ant_demo" tableName="pm_role" enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"/>
<table schema="spring_ant_demo" tableName="pm_role_has_pm_authority" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
<table schema="spring_ant_demo" tableName="pm_user" enableCountByExample="true" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
<columnOverride column="create_time" javaType="java.time.LocalDateTime"/>
<columnOverride column="update_time" javaType="java.time.LocalDateTime"/>
</table>
<table schema="spring_ant_demo" tableName="pm_user_has_pm_role" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
<table schema="spring_ant_demo" tableName="param_sex" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
</context>
</generatorConfiguration>
-
<columnOverride column="create_time" javaType="java.time.LocalDateTime"/>
转换数据库时间类型 - 启用注释生成, 生成器再次生成 会根据 @mbg.generated 标识 来判断是不是要重新生成
-
<columnOverride column="row_version" javaType="java.time.LocalDateTime" isGeneratedAlways="true"/>
中isGeneratedAlways="true"
会忽略数据库自动生成字段的insert,update,通常用于row_version
字段
<commentGenerator>
<property name="suppressAllComments" value="false"/>
<property name="suppressDate" value="false"/>
</commentGenerator>
Mybatis
- maven
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.4</version>
</dependency>
-
@MapperScan({"cn.tiantianquan.springant.mybatis.modelMapper", "cn.tiantianquan.springant.mybatis.mapper"})
应用启动程序添加注解,扫描所有mapper注入,相当于在Mapper接口上注解@Mapper
-
<sql>
替换模板
<sql id="table">
pm_user
</sql>
<select id="selectWithParam" resultMap="WithParam">
select *, ps.name as sex_str
from <include refid="table"/> pu
left join param_sex ps on pu.sex = ps.id
</select>
- ResultMap 继承
<resultMap id="WithParam" type="cn.tiantianquan.springant.model.PmUserModel" extends="cn.tiantianquan.springant.mybatis.mapper.PmUserMapper.BaseResultMap">
<result column="sex_str" jdbcType="VARCHAR" property="sexStr"/>
</resultMap>
- where语句
使用 where 标签包裹,没有条件会自动删除where
<select id="select" resultMap="WithParam">
select *, ps.name as sex_str
from pm_user pu
left join param_sex ps on pu.sex = ps.id
<where>
<if test="trueName != null">
and true_name concat( '%',#{trueName},'%')
</if>
<if test="loginName != null">
and login_name concat ('%',#{loginName},'%')
</if>
</where>
</select>
- 传入参数
使用@Param
注解指定传入参数名称
List<PmUserModel> select(
@Param(value = "loginName") String loginName,
@Param(value = "trueName") String trueName
);
- 自动映射
application.properties 中设置, 映射数据库的下划线模式到驼峰模式
自动映射默认开启
有三种自动映射等级:
NONE
- 禁用自动映射。仅设置手动映射属性。
PARTIAL
- 将自动映射结果除了那些有内部定义内嵌结果映射的(joins).
FULL
- 自动映射所有。
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.auto-mapping-behavior=partial #默认设置
官方文档:http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#Auto-mapping
- 防注入
mybatis中的#和$的区别
- #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号。如:order by #user_id#,如果传入的值是111,那么解析成sql时的值为order by "111", 如果传入的值是id,则解析成的sql为order by "id".
- 将传入的数据直接显示生成在sql中。如:order byuser_id$,如果传入的值是111,那么解析成sql时的值为order by user_id, 如果传入的值是id,则解析成的sql为order by id.
- #方式能够很大程度防止sql注入。
- $方式无法防止Sql注入。
- $方式一般用于传入数据库对象,例如传入表名.
- 一般能用#的就别用$.
网友评论