1.连接数据库blog,新建user表
CREATE TABLE `user` (
`id` int(16) NOT NULL,
`name` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户名',
`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码',
`root` int(16) NULL DEFAULT NULL COMMENT '权限',
`create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`) USING BTREE
)
2.用idea自动创建springboot项目,勾选web
3.创建包和对应User类:domain mapper service impl:@Service controller:@RestController unit interceptor
在User类中写入对应实体字段 alt+insert get/set (以后写可以使用代码生成器)
4.整合mybatis
pom.xml:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!-- MySQL的JDBC驱动包 -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<!-- 引入第三方数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
配置文件:
spring.datasource.url=jdbc:mysql://**.**.***.***:3306/blog?useUnicode=true&characterEncoding=utf-8
spring.datasource.username =root
spring.datasource.password =****
#如果不使用默认的数据源 (com.zaxxer.hikari.HikariDataSource)
spring.datasource.type =com.alibaba.druid.pool.DruidDataSource
#增加打印sql语句,一般用于本地开发测试
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
启动类:
@MapperScan("com.sycamore.blog.mapper")
5.编写findById方法,用于Login