本文主要是介绍springboot+jdbcTemplate的整合实现crud,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一 搭建
可以参考另一种方式:https://blog.csdn.net/u011066470/article/details/88086589
1.1 项目结构
1.2 pom文件
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13</version><scope>test</scope></dependency><!-- 以下是>spring boot依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- springboot-jdbc--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!-- springboot-mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency>
1.3 controller
package com.ljf.spring.boot.demo.controller;import com.ljf.spring.boot.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @ClassName: UserController* @Description: TODO* @Author: liujianfu* @Date: 2021/08/18 09:31:48 * @Version: V1.0**/
@RestController
public class UserController {@Autowiredprivate UserService userService;@RequestMapping("/query")public Object query(String name){return userService.queryUser(name);}
}
1.4 service
1.接口层
package com.ljf.spring.boot.demo.service;import com.ljf.spring.boot.demo.dao.UserDao;
import com.ljf.spring.boot.demo.model.UserDto;public interface UserService {public UserDto queryUser(String name);
}
2.实现层
package com.ljf.spring.boot.demo.service.impl;import com.ljf.spring.boot.demo.dao.UserDao;
import com.ljf.spring.boot.demo.model.UserDto;
import com.ljf.spring.boot.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** @ClassName: UserServiceImpl* @Description: TODO* @Author: liujianfu* @Date: 2021/08/18 09:37:56 * @Version: V1.0**/
@Service
public class UserServiceImpl implements UserService {@AutowiredUserDao userDao;@Overridepublic UserDto queryUser(String name) {return userDao.getUserByUsername(name);}
}
1.5 dao层
package com.ljf.spring.boot.demo.dao;import com.ljf.spring.boot.demo.model.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;import java.util.List;/*** @ClassName: UserDao* @Description: TODO* @Author: liujianfu* @Date: 2021/08/18 09:33:06 * @Version: V1.0**/
@Repository
public class UserDao {@AutowiredJdbcTemplate jdbcTemplate;//根据账号查询用户信息public UserDto getUserByUsername(String username){String sql = "select id,username,password,fullname,mobile from t_user where username = ?";//连接数据库查询用户List<UserDto> list = jdbcTemplate.query(sql, new Object[]{username}, new BeanPropertyRowMapper<>(UserDto.class));if(list !=null && list.size()==1){return list.get(0);}return null;}
}
1.6 controller层
package com.ljf.spring.boot.demo.controller;import com.ljf.spring.boot.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @ClassName: UserController* @Description: TODO* @Author: liujianfu* @Date: 2021/08/18 09:31:48 * @Version: V1.0**/
@RestController
public class UserController {@Autowiredprivate UserService userService;@RequestMapping("/query")public Object query(String name){return userService.queryUser(name);}
}
1.7 model层
package com.ljf.spring.boot.demo.model;/*** @author Administrator* @version 1.0**/
public class UserDto {private String id;private String username;private String password;private String fullname;private String mobile;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getFullname() {return fullname;}public void setFullname(String fullname) {this.fullname = fullname;}public String getMobile() {return mobile;}public void setMobile(String mobile) {this.mobile = mobile;}@Overridepublic String toString() {return "UserDto{" +"id='" + id + '\'' +", username='" + username + '\'' +", password='" + password + '\'' +", fullname='" + fullname + '\'' +", mobile='" + mobile + '\'' +'}';}
}
1.8 启动类
package com.ljf.spring.boot.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** Hello world!**/
@SpringBootApplication
public class App
{public static void main( String[] args ){SpringApplication.run(App.class);System.out.println( "Hello World!" );System.out.println("springboot的template模块启动完成!!!");}
}
1.9 测试
这篇关于springboot+jdbcTemplate的整合实现crud的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!