- 0.把数据导入数据库
- 1.下载依赖包
- 2.写配置文件
- 3.写代码访问数据库里的数据
- 3.1 实体类
- 3.2 dao数据访问层
- 3.3 xml配置文件
- 3.4 测试
C:Users9>mysql -uroot -p*** mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 10 Server version: 8.0.26 MySQL Community Server - GPL Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | bjpowernode | | community | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 6 rows in set (0.00 sec) mysql> drop database community ; Query OK, 5 rows affected (0.18 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | bjpowernode | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> create database community; Query OK, 1 row affected (0.02 sec) mysql> use community; Database changed mysql> source D:codeinit_schema.sql Query OK, 0 rows affected (0.00 sec) ... Query OK, 0 rows affected (0.00 sec) mysql> show tables; +---------------------+ | Tables_in_community | +---------------------+ | comment | | discuss_post | | login_ticket | | message | | user | +---------------------+ 5 rows in set (0.00 sec) mysql> source D:codeinit_data.sql; ERROR: Unknown command 'i'. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'odeinit_data.sql' at line 1 mysql> source D:/code/init_data.sql; Query OK, 0 rows affected (0.00 sec) ... Query OK, 0 rows affected (0.00 sec)1.下载依赖包
https://mvnrepository.com/
2.写配置文件# DataSourceProperties #驱动 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #连接格式 spring.datasource.url=jdbc:mysql://localhost:3306/community?characterEncoding=utf-8&useSSL=false&serverTimezone=Hongkong spring.datasource.username=root #1.写自己的密码 spring.datasource.password= spring.datasource.type=com.zaxxer.hikari.HikariDataSource spring.datasource.hikari.maximum-pool-size=15 spring.datasource.hikari.minimum-idle=5 spring.datasource.hikari.idle-timeout=30000 # MybatisProperties #2.resourcees下创建mapper包 mybatis.mapper-locations=classpath:mapper/*.xml #3.按路径创建entity包 mybatis.type-aliases-package=com.nowcoder.community.entity mybatis.configuration.useGeneratedKeys=true #header_url headerUrl这两种不同命名方式可以自动匹配 mybatis.configuration.mapUnderscoreToCamelCase=true3.写代码访问数据库里的数据
首先要写实体类,有个实体类能够封装表里的数据,然后才好用实体类对数据操作
3.1 实体类有多少个字段就对应多少个属性,不过属性按照驼峰式命名的方式命名
Alt+insert生成get/set方法、toString方法(方便打印对象看数据)
要在dao数据访问层写UserMapper接口,声明增删改查的方法,再写上对应的配置文件(下一步)
加注解@Mapper
声明增删改查的方法
声明好Mapper之后,要想实现它,需要写一个配置文件,配置文件里给每个方法提供它所需要的sql,mybatis底层会自动生成实现类
在mapper包下创建user-mapper.xml文件
配置文件的所有格式在MyBatis官网都有说明
https://mybatis.org/mybatis-3/zh/getting-started.html
注意:namespace写对应的Mapper的路径名+Mapper名,不然MyBatis也找不到对应的是谁呀
sql语句的格式(以select为例)官网都有,很详细
注意:sql语句里的属性名都是表格里的属性名,#{}里的属性名才是实体类里的属性名,不要搞混了
现在练习是一下子都写了,实际开发是用到什么方法再加
3.4 测试1.写注解
@RunWith(SpringRunner.class) @SpringBootTest @ContextConfiguration(classes = CommunityApplication.class)
2.注入Mapper
3.写测试类
3.5 调试着玩玩嘻嘻
Mybatis会自动实例化一个Mapper代理



