SpringBoot自学笔记(四)
特此声明:本自学笔记,主要是参照《从零开始学Spring Boot》(作者: 林祥纤)一书,并对部分例子进行了改动,便于理解,非盈利为目的,仅供学习交流,如有侵权,立即撤下!
历史笔记链接:《SpringBoot自学笔记(一)》
历史笔记链接:《SpringBoot自学笔记(二)》
历史笔记链接:《SpringBoot自学笔记(三)》
(七)Srping Boot——使用JdbcTemplate及修改SpringBoot默认配置
1.博主以“spring-boot-database”Maven工程为例。
2.修改pom.xml配置文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>yan.li</groupId> <artifactId>spring-boot-database</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 引入spring-boot-start-parent 依赖管理,引入以后在申明其它dependency的时候就不需要version了 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version> </parent> <dependencies> <!-- 引入spring-boot-starter-web 包含了spring webmvc和tomcat等web开发的特性 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 引入fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.7</version> </dependency> <!-- 引入mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 引入JPA规范 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> </dependencies> <!-- 如果我们要直接Main启动spring,那么以下plugin必须要添加,否则是无法启动的。如果使用maven 的spring-boot:run的话是不需要此配置的 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin </artifactId> </plugin> <dependencies> <!--springloaded hot deploy 热部署--> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.4.RELEASE</version> </dependency> </dependencies> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> </configuration> </execution> </executions> <!-- 配置JDK1.8 --> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project> |
3.在src/main/resources目录下修改application.properties配置文件(配置端口为80,修改默认拦截“/spring-boot”)
######################################################## ###datasource ######################################################## spring.datasource.url = jdbc:mysql://localhost:3306/test spring.datasource.username = root spring.datasource.password = root 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 ######################################################## ### Java Persistence Api ######################################################## # Specify the DBMS spring.jpa.database = MYSQL # Show or not log for each sql query spring.jpa.show-sql = true # Hibernate ddl auto (create, create-drop, update) spring.jpa.hibernate.ddl-auto = update # Naming strategy spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy # stripped before adding them to the entity manager) spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect ######################################################## ###EMBEDDED SERVER CONFIGURATION (ServerProperties) ######################################################## server.port=80 server.context-path=/spring-boot |
4.在yan.li.dao包下,创建DemoDao.java:
package yan.li.dao; import javax.annotation.Resource; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; import yan.li.pojo.Student; /** * 数据库操作类 * @author Liyan * @date 2016年11月29日 上午8:58:26 */ @Repository public class DemoDao { @Resource private JdbcTemplate jdbcTemplate; /** * 通过id获取Student对象 */ public Student getById(Long Id){ String sql = "select * from Student where id = ?"; RowMapper<Student> rowMapper = new BeanPropertyRowMapper<Student>(Student.class); return jdbcTemplate.queryForObject(sql, rowMapper ,Id); } } |
5.在yan.li.service包下,修改DemoService.java:
package yan.li.service; import javax.annotation.Resource; import javax.transaction.Transactional; import org.springframework.stereotype.Service; import yan.li.dao.DemoDao; import yan.li.dao.DemoRepositor; import yan.li.pojo.Student; /** * service层 * @author Liyan * @date 2016年11月24日 下午5:59:25 */ @Service public class DemoService { @Resource private DemoRepositor repositor; @Transactional /** * 使用Repository接口 * @author Liyan */ public void save(Student student) { repositor.save(student); }
@Resource private DemoDao dao; /** * 使用JdbcTemplate * @author Liyan */ public Student getById(Long Id){ //demoRepository.findOne(id); //在demoRepository可以直接使用findOne进行获取. return dao.getById(Id); //使用jdbcTemplate } } |
6.在yan.li.controller包下,修改DemoController.java:
package yan.li.controller; import javax.annotation.Resource; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import yan.li.pojo.Student; import yan.li.service.DemoService;
@RestController @RequestMapping("/demo") public class DemoController { @Resource private DemoService service; /** * 使用Repository接口,保存对象 * @author Liyan */ @RequestMapping("/save") public String save() { Student student = new Student(); student.setName("Tom"); service.save(student); return "成功保存:"+student.toString(); } /** * 使用JdbcTemplate,通过Id获取对象 * @author Liyan */ @RequestMapping("/getById") public Student getById(Long id){ return service.getById(id); } }
|
7.MainApp 启动类上右键Run As → Java Application启动(或是使用Maven的spring-boot:run),浏览器访问http://localhost/spring-boot/demo/getById?id=1,即可看到如下信息:
{"id":1,"name":"Tom"} |