|
| 1 | +# SpringBoot 整合 JDBC Templete 实现 CRUD 案例 |
| 2 | + |
| 3 | +今天是 2019/6/15,写这个案例的主要目的是,因为我是应届生,第一天上班拿到的项目采用的就是 SpringMVC + JDBC Templete + Bootstrap + easyUI ,通过前面的学习,我们知道还有更多,更好的框架来进行持久层的开发,例如Hibernate,SpringData JPA ,Mybatis 等,但是因为工作需要,我还是决定自己动手写一个SpringBoot整合JDBC Templete的案例。俗话说的好,技多不压身! |
| 4 | + |
| 5 | +## 解惑 |
| 6 | + |
| 7 | +- 为什么不是采用SSM进行整合? |
| 8 | + |
| 9 | +你可以这么理解,其实SpringBoot和SSM是同一个东西,表达的意思也是相同的,例如写一篇文章,采用白话文的方式和文言文的方式,结果都能完成文章,但是文言文更加的简洁。 |
| 10 | + |
| 11 | +- 体验一下SSM与SpringBoot的区别有多大? |
| 12 | + |
| 13 | +SSM 客户管理系统:这个项目采用的是传统的SSM的方式,写的一个客户关系管理系统,仔细观察可以发现配置文件多,而且偏复杂,[SSM 客户管理系统源码示例](https://github.com/tellsea/ssm) |
| 14 | + |
| 15 | +SpringBoot入门案例:这个项目就是采用SpringBoot的方式,输出Hello World的案例,仔细观察可以发现只有一个配置文件,而且还是空的,其实全部都由SpringBoot帮我们配置好了,[SpringBoot入门案例Hello World](https://github.com/Tellsea/springboot-learn/tree/master/springboot-hello) |
| 16 | + |
| 17 | +下面正式准备我们本次实现的案例,觉得项目有用的话,顺便 Star 和 Fork 一下噢 🎉🎉 |
| 18 | + |
| 19 | + |
| 20 | +## 准备工作 |
| 21 | +### 数据库 |
| 22 | +找到项目的`doc/sql/springboot-jdbc-templete.sql`文件,导入到自己的数据库中,也可以根据下面的sql进行创建 |
| 23 | +```sql |
| 24 | +CREATE TABLE `tb_user` ( |
| 25 | + `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID', |
| 26 | + `username` varchar(255) DEFAULT NULL COMMENT '用户名', |
| 27 | + `password` varchar(255) DEFAULT NULL COMMENT '密码', |
| 28 | + `ctime` datetime DEFAULT NULL COMMENT '创建时间', |
| 29 | + PRIMARY KEY (`id`) |
| 30 | +) ENGINE=InnoDB DEFAULT CHARSET=utf8; |
| 31 | +``` |
| 32 | + |
| 33 | +### 依赖 |
| 34 | +刚开始搭建项目的话,很容易迷,所以这里我贴一个项目目录结构 |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +创建SpringBoot项目,完整的依赖我这里粘贴一份 |
| 39 | +```xml |
| 40 | +<?xml version="1.0" encoding="UTF-8"?> |
| 41 | +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 42 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 43 | + <modelVersion>4.0.0</modelVersion> |
| 44 | + <parent> |
| 45 | + <groupId>org.springframework.boot</groupId> |
| 46 | + <artifactId>spring-boot-starter-parent</artifactId> |
| 47 | + <version>2.1.3.RELEASE</version> |
| 48 | + <relativePath/> <!-- lookup parent from repository --> |
| 49 | + </parent> |
| 50 | + <groupId>cn.tellsea</groupId> |
| 51 | + <artifactId>springboot-jdbc-templete</artifactId> |
| 52 | + <version>0.0.1-SNAPSHOT</version> |
| 53 | + <name>springboot-jdbc-templete</name> |
| 54 | + <description>Demo project for Spring Boot</description> |
| 55 | + |
| 56 | + <properties> |
| 57 | + <java.version>1.8</java.version> |
| 58 | + </properties> |
| 59 | + |
| 60 | + <dependencies> |
| 61 | + <!-- spring boot --> |
| 62 | + <dependency> |
| 63 | + <groupId>org.springframework.boot</groupId> |
| 64 | + <artifactId>spring-boot-starter</artifactId> |
| 65 | + </dependency> |
| 66 | + |
| 67 | + <dependency> |
| 68 | + <groupId>org.springframework.boot</groupId> |
| 69 | + <artifactId>spring-boot-starter-test</artifactId> |
| 70 | + <scope>test</scope> |
| 71 | + </dependency> |
| 72 | + |
| 73 | + <!-- jdbc --> |
| 74 | + <dependency> |
| 75 | + <groupId>org.springframework.boot</groupId> |
| 76 | + <artifactId>spring-boot-starter-jdbc</artifactId> |
| 77 | + </dependency> |
| 78 | + |
| 79 | + <!-- 数据库驱动,我是MySQL5.7,所以使用5.1.x的驱动,如果你是MySQL8,则改成8.0.x的版本 --> |
| 80 | + <dependency> |
| 81 | + <groupId>mysql</groupId> |
| 82 | + <artifactId>mysql-connector-java</artifactId> |
| 83 | + <version>5.1.46</version> |
| 84 | + </dependency> |
| 85 | + |
| 86 | + <dependency> |
| 87 | + <groupId>org.projectlombok</groupId> |
| 88 | + <artifactId>lombok</artifactId> |
| 89 | + <version>1.18.8</version> |
| 90 | + </dependency> |
| 91 | + </dependencies> |
| 92 | + |
| 93 | + <build> |
| 94 | + <plugins> |
| 95 | + <plugin> |
| 96 | + <groupId>org.springframework.boot</groupId> |
| 97 | + <artifactId>spring-boot-maven-plugin</artifactId> |
| 98 | + </plugin> |
| 99 | + </plugins> |
| 100 | + </build> |
| 101 | + |
| 102 | +</project> |
| 103 | + |
| 104 | +``` |
| 105 | + |
| 106 | +### 配置文件 |
| 107 | +注意改成自己的相关属性值 |
| 108 | +```yml |
| 109 | +server: |
| 110 | + port: 8080 |
| 111 | +spring: |
| 112 | + datasource: |
| 113 | + driver-class-name: com.mysql.jdbc.Driver |
| 114 | + url: jdbc:mysql://localhost:3306/springboot-jdbc-templete |
| 115 | + username: root |
| 116 | + password: 123456 |
| 117 | +``` |
| 118 | +### SpringBoot启动类 |
| 119 | +创建SpringBoot项目的话,这个应该是自动生成的,这里给通过Maven创建项目的同学粘贴一个启动类 |
| 120 | +```java |
| 121 | +@SpringBootApplication |
| 122 | +public class SpringbootJdbcTempleteApplication { |
| 123 | + |
| 124 | + public static void main(String[] args) { |
| 125 | + SpringApplication.run(SpringbootJdbcTempleteApplication.class, args); |
| 126 | + } |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +## 编写代码 |
| 131 | +- 实体类,采用了Lombok表达式,不清楚的同学可以看我 [SSM的项目文档](https://github.com/Tellsea/ssm) 有简单的说明 |
| 132 | +```java |
| 133 | +@Data |
| 134 | +@AllArgsConstructor |
| 135 | +@NoArgsConstructor |
| 136 | +public class User { |
| 137 | + |
| 138 | + private Long id; |
| 139 | + |
| 140 | + private String username; |
| 141 | + |
| 142 | + private String password; |
| 143 | + |
| 144 | + private Date ctime; |
| 145 | +} |
| 146 | +``` |
| 147 | +- dao接口 |
| 148 | +```java |
| 149 | +public interface UserDao { |
| 150 | + |
| 151 | + User getUserById(Integer id); |
| 152 | + |
| 153 | + List<User> getUserList(); |
| 154 | + |
| 155 | + int add(User user); |
| 156 | + |
| 157 | + int update(User user); |
| 158 | + |
| 159 | + int delete(Integer id); |
| 160 | +} |
| 161 | +``` |
| 162 | +- dao 实现类,jdbcTemplate 是注入的对象 |
| 163 | +```java |
| 164 | +@Repository // 用于标注数据访问组件 |
| 165 | +public class UserDaoImpl implements UserDao { |
| 166 | + |
| 167 | + @Autowired |
| 168 | + private JdbcTemplate jdbcTemplate; |
| 169 | + |
| 170 | + @Override |
| 171 | + public User getUserById(Long id) { |
| 172 | + List<User> list = jdbcTemplate.query("select * from tb_user where id = ?", new Object[]{id}, new BeanPropertyRowMapper(User.class)); |
| 173 | + if (list != null && list.size() > 0) { |
| 174 | + return list.get(0); |
| 175 | + } else { |
| 176 | + return null; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public List<User> getUserList() { |
| 182 | + List<User> list = jdbcTemplate.query("select * from tb_user", new Object[]{}, new BeanPropertyRowMapper(User.class)); |
| 183 | + if (list != null && list.size() > 0) { |
| 184 | + return list; |
| 185 | + } else { |
| 186 | + return null; |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public int add(User user) { |
| 192 | + return jdbcTemplate.update("insert into tb_user(username, password, ctime) values(?, ?, ?)", |
| 193 | + user.getUsername(), user.getPassword(), new Date()); |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public int update(User user) { |
| 198 | + return jdbcTemplate.update("update tb_user SET username = ? , password = ? WHERE id=?", |
| 199 | + user.getUsername(), user.getPassword(), user.getId()); |
| 200 | + } |
| 201 | + |
| 202 | + @Override |
| 203 | + public int delete(Long id) { |
| 204 | + return jdbcTemplate.update("delete from tb_user where id = ? ", id); |
| 205 | + } |
| 206 | + |
| 207 | +} |
| 208 | +``` |
| 209 | +- service 层接口 |
| 210 | +```java |
| 211 | +public interface UserService { |
| 212 | + |
| 213 | + User getUserById(Integer id); |
| 214 | + |
| 215 | + List<User> getUserList(); |
| 216 | + |
| 217 | + int add(User user); |
| 218 | + |
| 219 | + int update(User user); |
| 220 | + |
| 221 | + int delete(Integer id); |
| 222 | +} |
| 223 | +``` |
| 224 | +- service 层实现类,很简单,直接调用dao层就行了 |
| 225 | +```java |
| 226 | +@Service |
| 227 | +public class UserServiceImpl implements UserService { |
| 228 | + |
| 229 | + @Autowired |
| 230 | + private UserDao userDao; |
| 231 | + |
| 232 | + @Override |
| 233 | + public User getUserById(Integer id) { |
| 234 | + return userDao.getUserById(id); |
| 235 | + } |
| 236 | + |
| 237 | + @Override |
| 238 | + public List<User> getUserList() { |
| 239 | + return userDao.getUserList(); |
| 240 | + } |
| 241 | + |
| 242 | + @Override |
| 243 | + public int add(User user) { |
| 244 | + return userDao.add(user); |
| 245 | + } |
| 246 | + |
| 247 | + @Override |
| 248 | + public int update(User user) { |
| 249 | + return userDao.update(user); |
| 250 | + } |
| 251 | + |
| 252 | + @Override |
| 253 | + public int delete(Integer id) { |
| 254 | + return userDao.delete(id); |
| 255 | + } |
| 256 | +} |
| 257 | +``` |
| 258 | +- 下面这个是一个简单的公共数据返回类,通常项目中都是必须存在的,用于指定统一的响应数据格式,更复杂的写法在SpringBoot的基础模块学习,[SpringBoot 搭建全局异常处理](https://github.com/Tellsea/springboot-learn/tree/master/springboot-global) |
| 259 | +```java |
| 260 | +@Data |
| 261 | +@AllArgsConstructor |
| 262 | +@NoArgsConstructor |
| 263 | +public class ResponseResult { |
| 264 | + |
| 265 | + private Integer code; |
| 266 | + |
| 267 | + private String msg; |
| 268 | + |
| 269 | + private Object data; |
| 270 | +} |
| 271 | +``` |
| 272 | +- 控制层 |
| 273 | +```java |
| 274 | +@RestController |
| 275 | +@RequestMapping("/user") |
| 276 | +public class UserController { |
| 277 | + |
| 278 | + @Autowired |
| 279 | + private UserService userService; |
| 280 | + |
| 281 | + /** |
| 282 | + * 根据id查询用户 |
| 283 | + * |
| 284 | + * @param id |
| 285 | + * @return |
| 286 | + */ |
| 287 | + @GetMapping("/get/{id}") |
| 288 | + public ResponseResult getUserById(@PathVariable(value = "id") Long id) { |
| 289 | + User user = userService.getUserById(id); |
| 290 | + if (user == null) { |
| 291 | + return new ResponseResult(200, "查询结果为空", null); |
| 292 | + } |
| 293 | + return new ResponseResult(200, "查询成功", user); |
| 294 | + } |
| 295 | + |
| 296 | + /** |
| 297 | + * 查询用户列表 |
| 298 | + * |
| 299 | + * @return |
| 300 | + */ |
| 301 | + @GetMapping("/list") |
| 302 | + public ResponseResult getUserList() { |
| 303 | + List<User> list = userService.getUserList(); |
| 304 | + if (list == null || list.isEmpty()) { |
| 305 | + return new ResponseResult(200, "查询结果为空", null); |
| 306 | + } |
| 307 | + return new ResponseResult(200, "查询成功", list); |
| 308 | + } |
| 309 | + |
| 310 | + /** |
| 311 | + * 新增用户 |
| 312 | + * |
| 313 | + * @param user |
| 314 | + * @return |
| 315 | + */ |
| 316 | + @PostMapping("/add") |
| 317 | + public ResponseResult add(@RequestBody User user) { |
| 318 | + int count = userService.add(user); |
| 319 | + if (count == 0) { |
| 320 | + return new ResponseResult(500, "新增失败", null); |
| 321 | + } |
| 322 | + return new ResponseResult(200, "新增成功", null); |
| 323 | + } |
| 324 | + |
| 325 | + /** |
| 326 | + * 根据id删除用户 |
| 327 | + * |
| 328 | + * @param id |
| 329 | + * @return |
| 330 | + */ |
| 331 | + @GetMapping("/delete/{id}") |
| 332 | + public ResponseResult delete(@PathVariable(value = "id") Long id) { |
| 333 | + int count = userService.delete(id); |
| 334 | + if (count == 0) { |
| 335 | + return new ResponseResult(500, "删除失败", null); |
| 336 | + } |
| 337 | + return new ResponseResult(200, "删除成功", null); |
| 338 | + } |
| 339 | + |
| 340 | + /** |
| 341 | + * 根据id修改用户信息 |
| 342 | + * |
| 343 | + * @param user |
| 344 | + * @return |
| 345 | + */ |
| 346 | + @PostMapping("/update") |
| 347 | + public ResponseResult update(@RequestBody User user) { |
| 348 | + int count = userService.update(user); |
| 349 | + if (count == 0) { |
| 350 | + return new ResponseResult(500, "更新失败", null); |
| 351 | + } |
| 352 | + return new ResponseResult(200, "更新成功", null); |
| 353 | + } |
| 354 | +} |
| 355 | +``` |
| 356 | + |
| 357 | +## 测试 |
| 358 | +我这里直接使用Postman测试工具进行测试,不会使用的话你也可以直接使用SpringBoot带的单元测试 |
| 359 | + |
| 360 | +- 新增用户 |
| 361 | + |
| 362 | + |
| 363 | + |
| 364 | +- 根据ID查询 |
| 365 | + |
| 366 | + |
| 367 | + |
| 368 | +- 查询列表 |
| 369 | + |
| 370 | + |
| 371 | + |
| 372 | +- 更新用户 |
| 373 | + |
| 374 | + |
| 375 | + |
| 376 | +- 删除用户 |
| 377 | + |
| 378 | + |
| 379 | + |
| 380 | +# 交流学习 |
| 381 | + |
| 382 | +QQ:3210054449 |
| 383 | + |
0 commit comments