Springboot学习笔记(五)JdbcTemplate
1、引入依赖。(最好引用jdbc依赖包)
2、使用JdbcTemplate
3、创建一个Dao层,即为数据访问层。创建一个CatDao。因为Cat使用@Entity注解,是一个持久化的对象。如图,Jdbc依赖包可不用注解@Entity,及@Id等主键。
在public class CatDao上使用注解@Repository,标注这是一个持久化操作对象。使用jdbc,在private JdbcTemplate jdbc;上注解@Resource,可使用JdbcTemplate。
写一个查询方法。方法的实现:
(1)定义一个sql语句;
(2)定义一个RowMapper;
(3)执行查询方法
4、创建Service层,即业务层。在这里创建一个CatService,在public class CatService上注解@Service。在private CatDao catdaodao;上注解@Resource引用,创建好的Dao层,即CatDao。
5、Controller控制层,private CatService catser;加入注解@Resource,引用Service层的CatService。@RequestMapping(“/cat”)仅为路径,为:/cat/selectdao
6、在浏览器输入http://127.0.0.1:8080/cat/selectdao?na=mbz,即可查询名字为mbz的对象信息。
7、每个文件只能有一个主入口。
8、数据库配置文件:
########################################################
###datasource -- 指定mysql数据库链接信息
########################################################
spring.datasource.url = jdbc:mysql://192.168.1.110:3306/demo1?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = [email protected]
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10