一、准备工作
1. 插件安装
在IDEA中安装(EasyCode)插件、(Lombok)插件。
2. 创建一个springboot项目
如果不会创建的话可以翻看我前面写过的文章。初识 Springboot
创建完成后的目录结构:
3. 准备数据库表
表结构:
CREATE TABLE `user` (
`id` int(11) NOT NULL,
`username` varchar(255) DEFAULT NULL,
`gender` varchar(11) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
4. 在IDEA配置连接数据库
① 按如下方式:选择数据源、选择mysql数据库(你也可以选择自己使用的数据库)
② 填写数据连接信息,点击测试。如果测试成功,点击OK。
注意:
idea关联mysql时报错Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezon'
解决办法:
进入cmd命令窗口,连接数据库 mysql -uroot -p,回车, 输入密码,回车,输入:show variables like’%time_zone’;
设置北京时区,set global time_zone=’+8:00’;
二、生成代码
1. IDEA中开始生成代码
① 找到所需要生成代码的表,然后点击Generate Code。
② 选择包路径以及生成的模板
③ 生成的代码结构
2. 导入需要的依赖
pom.xml
< java.version > 1.8 < / java.version >
< / properties >
< dependencies >
< dependency >
< groupId > org.springframework.boot groupId>
< artifactId > spring - boot - starter - web artifactId>
< / dependency >
< dependency >
< groupId > org.springframework.boot groupId>
< artifactId > spring - boot - starter artifactId>
< / dependency >
< dependency >
< groupId > org.springframework.boot groupId>
< artifactId > spring - boot - devtools artifactId>
< optional > true optional>
< / dependency >
< !--springboot 与 mybatis的整 合 包-- >
< dependency >
< groupId > org.mybatis.spring.boot groupId>
< artifactId > mybatis - spring - boot - starter artifactId>
< version > 2.1 .1 < / version >
< / dependency >
< !--mysql驱动 包-- >
< dependency >
< groupId > mysql groupId>
< artifactId > mysql - connector - java artifactId>
< version > 5.1 .46 < / version >
< / dependency >
< !--引 入druid-- >
< dependency >
< groupId > com.alibaba groupId>
< artifactId > druid artifactId>
< version > 1.1 .8 < / version >
< / dependency >
< !--引 入lombok 用来简 化 实体 类 -- >
< dependency >
< groupId > org.projectlombok groupId>
< artifactId > lombok artifactId>
< optional > true optional>
< / dependency >
< dependency >
< groupId > org.springframework.boot groupId>
< artifactId > spring - boot - starter - test artifactId>
< scope > test scope>
< exclusions >
< exclusion >
< groupId > org.junit.vintage groupId>
< artifactId > junit - vintage - engine artifactId>
< / exclusion >
< / exclusions >
< / dependency >
< / dependencies >
< build >
< plugins >
< plugin >
< groupId > org.springframework.boot groupId>
< artifactId > spring - boot - maven - plugin artifactId>
< / plugin >
< / plugins >
< / build>
3. 编写数据源的相关配置
application.properties
server.port = 8082
# 数据库配置
spring.datasource.driver - class - name= com.mysql.jdbc.Driver
spring.datasource.url= jdbc : mysql : /* 127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=UTF-8 */
spring.datasource.username= root
spring.datasource.password= root
spring.datasource.type= com.alibaba.druid.pool.DruidDataSource
mybatis.mapper - locations = classpath : / mapper /*Dao.xml
4. 在dao接口添加@Mapper注解
@Mapper
public interface UserDao {
......
}
三、测试
controller层
(不再展示其它层代码,展示controller是为了方便查看请求路径)
package com.parker.controller;
import com.parker.entity.User;
import com.parker.service.UserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* (User)表控制层
*
* @author 扬帆向海
* @since 2020-05-15 00:56:29
*/
@RestController
@RequestMapping( "user" )
public class UserController {
/**
* 服务对象
*/
@Resource
private UserService userService;
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping( "selectOne" )
public User selectOne( Integer id )
{
return(this.userService.queryById( id ) );
}
}
启动项目,输入URL