Spring Boot学习之旅:(十五)使用JdbcTemplate
2017-11-05 18:49
411 查看
在原有的基础上添加 mysql 连接和jpa 依赖
创建一个model
dao
dao 实现
service
service 实现
controller
启动工程 浏览器输入
http://localhost:8080/user/1
查看查询结果
本片文章只做整合JdbcTemplate 只做了简单的查询 如果还要实现其他的方法可以参考
http://www.cnblogs.com/heyongjun1997/p/5964256.html 这篇博客
文章地址:http://www.haha174.top/article/details/257327
项目源码: https://github.com/haha174/boot.git
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
创建一个model
package com.wen.boot.model; import java.util.Date; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import com.alibaba.fastjson.annotation.JSONField; @Entity public class User { private int id; private String name; private int age; @JSONField(format="yyyy-mm-dd HH:mm") private Date createTime; private String remakes; /** serialize 是否需要序列化 * @return the remakes */ @JSONField(serialize=false) public String getRemakes() { return remakes; } public void setRemakes(String remakes) { this.remakes = remakes; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } @Id @GeneratedValue(strategy=GenerationType.AUTO) public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
dao
public interface IUserDao { public User getUserById(int id); }
dao 实现
@Repository("userDao") public class UserDaoImpl implements IUserDao { @Resource private JdbcTemplate jdbcTemplate; public User getUserById(int id) { String sql="select * from user where id=?"; RowMapper<User> user=new BeanPropertyRowMapper<User>(User.class); return jdbcTemplate.queryForObject(sql,user,id); } }
service
public interface IUserService { public User getUserById(int id); }
service 实现
@Service public class UserServiceImpl implements IUserService{ @Resource(name="userDao") private IUserDao userDao; public User getUserById(int id) { return userDao.getUserById(id); } }
controller
@RestController @RequestMapping("/user") public class HelloContreller { private Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired public IUserService userService; @GetMapping("/{id}") public User getUser(@PathVariable int id) { return userService.getUserById(id); } }
启动工程 浏览器输入
http://localhost:8080/user/1
查看查询结果
本片文章只做整合JdbcTemplate 只做了简单的查询 如果还要实现其他的方法可以参考
http://www.cnblogs.com/heyongjun1997/p/5964256.html 这篇博客
文章地址:http://www.haha174.top/article/details/257327
项目源码: https://github.com/haha174/boot.git
相关文章推荐
- springboot学习(2)springboot使用JdbcTemplate完成对数据库的增删改查
- SpringBoot学习笔记-使用jdbcTemplate访问mysql
- Spring Boot 学习9--使用JdbcTemplate
- REDIS学习(3.2)spring boot 使用redis作为缓存
- Spring Boot 官方文档学习(一)入门及使用
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- spring boot + JdbcTemplate 使用druid数据源
- 使用 Spring Boot 快速构建 Spring 框架应用---学习笔记
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- Spring Boot项目中使用jdbctemplate 操作MYSQL数据库
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 【Spring学习笔记】五、使用JDBCTemplate以对象方式操作数据库
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- Spring Boot学习总结(4)——使用Springloaded进行热部署
- (9)使用JdbcTemplate【从零开始学Spring Boot】
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- Spring学习笔记----JdbcTemplet和NamedParameterJdbcTemplate的使用
- 深入学习spring-boot系列(二)--使用spring-data-jpa