目的:
spring boot项目通过mybatis.generator插件方便的生成mysql数据表实体类和读取数据类。这样可以省去手动创建的麻烦。
步骤:
1 在pom.xml文件中引入mybatis.generator插件
2 在resources/generator/mybatis-generator.xml中修改连接的数据库信息并添加要生成的数据表,例如:
<!--数据库链接地址账号密码--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:port/volcano" userId="user" password="xxxxx"> </jdbcConnection>
<table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
3 点击idea->Edit Configuration...,点击‘+’,选择Maven,可命名为'generator'。
在'working directory'选择项目根目录,在'Command line'输入mybatis-generator:generate -e。点击apply。
4 点击启动generator。如果无报错,则会生成表所对应的类。
总结:
生成的表对应的实体类和操作数据类完成了。在实际使用时,我发现这样生成的实体类少了一个@Mapper,手动添加后发现OK了。
如果不加@Mapper, 启动报错时会报错,报错信息为:
*************************** APPLICATION FAILED TO START *************************** Description: Field xxxxxxMapper in com.xxxx.service.impl.xxxxServiceImpl required a bean of type 'com.xxxxx.repository.mapper.xxxxxMapper' that could not be found. Action: Consider defining a bean of type 'com.xxxx.repository.mapper.xxxxxxxMapper' in your configuration.