1.(构建maven项目)查看本机仓库settings-->Build,Execution,Deloyment--->Build Tools--->Repositories(可以查看本机仓库)
如图:
2. org.mybatis.generator插件的引入
这个插件帮你创建entity(实体类),dao(接口)和mapper(xml文件),这与你数据库中对应的表一一对应。
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
3. 在resources目录下导入generatorConfig.xml
application.properties(在resources包下,数据库配置文件)
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mall?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123abcABC
mybatis.mapper-locations=classpath:mapper/*.xml
1)核对application.properties文件中对应的配置信息是否有误
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/mall?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimeZone=Asia/Shanghai"
userId="root"
password="123abcABC">
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
2)
数据库驱动的位置
<classPathEntry
location="C:\Users\sd\.m2\repository\mysql\mysql-connector-java\8.0.23\mysql-connector-java-8.0.23.jar"/>
3)自动生成 entity 层和 dao 层文件(双击选中的文件)
4)控制台看到运行成功
5)跑通接口
1.service(UserService)
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUserByPK(int id){
return userMapper.selectByPrimaryKey(id);
}
}
2.controller(UserController)
@RestController
public class UserController {
@Autowired
private UserService userService;
}
3.测试接口