MyBatisGenerator自动生成配置文件
MyBatisGenerator配置文件的内容有:
- 配置驱动的位置
- 是否序列化
- 是否去除自动生成的注释
- 配置数据源:url、用户名、密码、驱动
- 配置POJO(实体类)所在位置,通常配置在pojo包
- 配置Mapper接口所在位置,通常配置在mapper包
- 配置Mapper.xml所在位置,通常配置在mapper包
- 配置所需生成的数据库表
<generatorConfiguration>
<!-- context 用于设置生成一组对象的环境 -->
<!--驱动在磁盘中的位置-->
<classPathEntry location="C:/ProgramPlugins/mysql-connector-java-8.0.16.jar" />
<context id="softwaredb">
<!-- PO序列化 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin">
</plugin>
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="false" />
</commentGenerator>
<!-- 配置数据源 -->
<jdbcConnection connectionURL="jdbc:mysql://localhost:3306/softwaredb?characterEncoding=utf8"
driverClass="com.mysql.jdbc.Driver"
password="123" userId="softwaredb" />
<!-- 配置实体生成的位置 -->
<!-- targetProject项目名就是这个项目名 -->
<javaModelGenerator
targetPackage="team.softwarede.confersys.entity"
targetProject="ConferSys" />
<!-- 配置Mapper接口生成的位置 -->
<sqlMapGenerator
targetPackage="team.softwarede.confersys.mapper"
targetProject="ConferSys" />
<!-- 配置Mapper.xml生成的位置 -->
<javaClientGenerator
targetPackage="team.softwarede.confersys.mapper"
targetProject="ConferSys"
type="XMLMAPPER" />
<!-- 配置需要生成的表 -->
<!--生成对应表及类名-->
<table catalog="softwaredb" tableName="goods" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
如果是MySQL8.0,则生成数据库的表的配置中,需要配置catalog
,否则如果两个数据库中有同名的表,则有可能引用的表会是其中的某一个,而不是与url中对应的表;如果是MySQL5.0版本的话,则不需要
在自动生成的mapper中,会生成基础的方法接口,通常包括:
int deleteByPrimaryKey(Integer id);
int insert(Goods record);
int insertSelective(Goods record);
Goods selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Goods record);
int updateByPrimaryKey(Goods record);
Mapper.xml补充说明
rusultMap标签
resultMap表示自定义封装结果集,内容如下:
<resultMap id="BaseResultMap" type="mybatis02.pojo.Goods" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="num" property="num" jdbcType="INTEGER" />
</resultMap>
id
和result
:
id
标签是指结果集的主键result
标签是指主键之外的属性
column
和property
:
column
是指数据库中表的列property
是指实体类中的属性
作用:
在有返回值的时候,在resultMap
属性中引用自定义封装结果集的id,如:
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from ssmdb..goods
where id = #{id,jdbcType=INTEGER}
</select>
property
是指实体类中的属性
sql标签
sql标签主要是为了避免在项目开发的过程中重复编写大量相同的sql语句,它可以被包含在其他语句里面, 使用 <include>
节点来包含。而且, 它里面是可以使用 ${} 占位符参数化的(注意, 此处的参数不是调用时传进来的), 不同的属性值通过包含的实例而变化内容如下:
<sql id="userColumns"> ${alias}.id,${alias}.username,${alias}.password </sql>
引用方法:
使用include
标签进行引用
<select id="selectUsers" resultType="map">
select
<include refid="userColumns"><property name="alias" value="t1"/></include>,
<include refid="userColumns"><property name="alias" value="t2"/></include>
from some_table t1
cross join some_table t2
</select>
trim标签
mybatis的trim标签一般用于去除sql语句中多余的and关键字,逗号,或者给sql语句前拼接 “where“、“set“以及“values(“ 等前缀,或者添加“)“等后缀,可用于选择性插入、更新、删除或者条件查询等操作。
参考链接:mybatis trim标签的使用
<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=""></trim>
属性详解:
- prefix:在trim标签内sql语句加上前缀。
- suffix:在trim标签内sql语句加上后缀。
- suffixOverrides:指定去除多余的后缀内容,如:suffixOverrides=",",去除trim标签内sql语句多余的后缀","。
- prefixOverrides:指定去除多余的前缀内容