10. 让springboot扫描mybatis配置文件
添加UserDOMapper
10. 让springboot扫描mybatis配置文件
将@EnableAutoConfiguration换成@SpringBootApplication
都是将App类变成Spring的托管类,并且指定App是主启动类
加入scanBasePackages={} 用于扫描配置文件
加入MapperScan用于注入dao
9. 配置数据源配置
5. 编写mybatis-generator.xml
官网下载
model表示数据库对应到JavaDTO的类
6. 创建dataobject目录,用于存放生成的DTO对象
7. 创建dao目录,配置到XMLMAPPER
8. 生成表及类名对应关系
<table tableName="user_info" domainObjectName="UserDO">
...
9, 新建Maven命令,用于指向mybatis-generator插件
mybatis-generator:generate
5. 编写mybatis-generator.xml
官网下载
2.4 Mybatis自动生成器的使用方式 1. mybatis-core版本要和mybatis-maven-plugin版本要一致 2. mybatis-generator.xml 中的配置 3. 配置 maven Command line : mybatis-generator:generate -e -X 注意 : generate 前是 : 4. 生成后,需要给 mapper 接口添加注解 5. application.properties 中添加数据源配置 # 接入 mybatis 对应的数据源 spring.datasource.name=miaosha spring.datasource.url=jdbc:mysql://127.0.0.1:3306/miaosha?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&useSSL=false spring.datasource.username=root spring.datasource.password=1234 # 使用 druid 数据源 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
让SpringBoot去扫瞄mybatis的配置文件以及之后的service的封装
让SpringBoot去扫瞄mybatis的配置文件以及之后的service的封装
application.properties文件配置mybatis接入就完成了
mybatis接入dao层
复杂的两个文件通过一些设置让它不生成
完善mybatis-generator.xml配置文件启动mybatis就能自动生成两个表对数据库的映射
新建maven项目mybatis-generator指向插件mybatis-generator:generate
mybatis-generator.xml配置文件从官网下载
数据库创建user_info user_password
mybatis.genertor的方式可以自动生成Mapper及xml
在resources文件夹中新建mybatis-generator.xml文件,该文件可以从官网上下载。
<?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\lhf\.m2\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar" />--> <!--数据库连接--> <context id="DB2Tables" targetRuntime="MyBatis3"> <!--注释--> <commentGenerator> <property name="suppressAllComments" value="true"/> <property name="suppressDate" value="true"/> </commentGenerator> <!--数据库连接地址及账号密码--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/seckill" userId="root" password="root"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!--生成DataObject类存放位置--> <javaModelGenerator targetPackage="com.lhf.springboot.dataobject" targetProject="src/main/java"> <!--是否对model添加构造函数--> <property name="constructorBased" value="false"/> <!--是否允许子包--> <property name="enableSubPackages" value="true"/> <!--建立的model对象是否不可变,也就是生成的model没有setter方法--> <property name="immutable" value="false"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--生成映射文件存放位置--> <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!--生成Dao类的存放位置--> <!-- 客户端代码,生成易于使用的正对Model对象和XML配置文件的代码 type="ANNOTATEDMAPPER", 生成Java Model和基于注解的Mapper对象 type="MIXEDMAPPER", 生成基于注解的Java Model和相应的Mapper对象 type="XMLMAPPER", 生成SQLMap XML文件和独立的Mapper接口 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.lhf.springboot.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!--生成对应表及类名--> <!--<table schema="mybatis" tableName="user_info" domainObjectName="UserDO" enableInsert="true" enableSelectByExample="false" enableDeleteByPrimaryKey="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false" enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"/>--> <table tableName="user_info" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" enableInsert="true" enableDeleteByPrimaryKey="false" ></table> <table tableName="user_password" domainObjectName="UserPassword" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" enableInsert="true" enableDeleteByPrimaryKey="false" ></table> </context> </generatorConfiguration>
密码不与主表信息放在一起,单独放在一个系统中
禁止自动生成Example
自己出现的问题
mybatis.mapperLocations=classpath:mapping/*.xml location后面的s漏掉了,导致找不到select
密码一般与信息主表分开
@Mapperscan还需要事先在mapper接口上添加@Reposity注解才能被扫描到
自动生成代码的插件里面添加依赖,而不是外面的依赖里面
属性文件配置
<?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\lhf\.m2\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar" />--> <!--数据库连接--> <context id="DB2Tables" targetRuntime="MyBatis3"> <!--注释--> <commentGenerator> <property name="suppressAllComments" value="true"/> <property name="suppressDate" value="true"/> </commentGenerator> <!--数据库连接地址及账号密码--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/seckill" userId="root" password="root"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!--生成DataObject类存放位置--> <javaModelGenerator targetPackage="com.lhf.springboot.dataobject" targetProject="src/main/java"> <!--是否对model添加构造函数--> <property name="constructorBased" value="false"/> <!--是否允许子包--> <property name="enableSubPackages" value="true"/> <!--建立的model对象是否不可变,也就是生成的model没有setter方法--> <property name="immutable" value="false"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--生成映射文件存放位置--> <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!--生成Dao类的存放位置--> <!-- 客户端代码,生成易于使用的正对Model对象和XML配置文件的代码 type="ANNOTATEDMAPPER", 生成Java Model和基于注解的Mapper对象 type="MIXEDMAPPER", 生成基于注解的Java Model和相应的Mapper对象 type="XMLMAPPER", 生成SQLMap XML文件和独立的Mapper接口 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.lhf.springboot.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!--生成对应表及类名--> <!--<table schema="mybatis" tableName="user_info" domainObjectName="UserDO" enableInsert="true" enableSelectByExample="false" enableDeleteByPrimaryKey="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false" enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"/>--> <table tableName="user_info" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" enableInsert="true" enableDeleteByPrimaryKey="false" ></table> <table tableName="user_password" domainObjectName="UserPassword" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" enableInsert="true" enableDeleteByPrimaryKey="false" ></table> </context> </generatorConfiguration>
运行不了可能是xml文件有重复?删除mapping下xml重新生成
逆向工程生成文件,配置application.properties,spring接入数据库
看一看数据库密码是不是正确的
该配置文件可以从MyBatis官网下载:
创建MyBatis-generator.xml文件
mybatis-generator:generate