概述
该Demo旨在部署一个简单spring boot工程,包含数据编辑和查看功能
POM配置
<?xml version="1.0" encoding="UTF-8"?> <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>com.roncoo.education</groupId> <artifactId>spring-boot-demo-15-1</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-demo-15-1</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>2.1.4</version> </dependency> <!-- 数据库 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.1.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
属性配置
server.port=8080 spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/postgis_24_sample?currentSchema=lilei_1 spring.datasource.username=postgres spring.datasource.password=lilei spring.datasource.driver-class-name=org.postgresql.Driver spring.jpa.hibernate.ddl-auto= update spring.jpa.show-sql=true
工程结构图
核心代码
dao层
package com.lilei.spring_boot_db.boot.dao; import java.util.List; import com.lilei.spring_boot_db.boot.bean.User; public interface UserDao { public User getUser(int id); public int updateUser(User u); public int insertUser(User u); public int deleteUser(int id); public List<User> queryUser(int pageSize,int pageNo); }
dao实现层
package com.lilei.spring_boot_db.boot.dao.impl; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import com.lilei.spring_boot_db.boot.bean.User; import com.lilei.spring_boot_db.boot.dao.UserDao; @Repository public class UserDaoImpl implements UserDao { @Autowired protected JdbcTemplate jdbc; public User getUser(int id) { String sql = "select name,create_date from user where id=?"; List<Map<String,Object>> list = jdbc.queryForList(sql, id); if (list.size()>0){ String name = list.get(0).get("name").toString(); Date createDate = (Date) list.get(0).get("create_date"); User u = new User(id,name); u.setCreateTime(createDate); return u; } return null; } public int updateUser(User u) { String sql = "update lilei_1.user set name=?,create_date=? where id=?"; int result = jdbc.update(sql, u.getName(),u.getCreateTime(),u.getId()); return result; } public int insertUser(User u) { String sql = "insert into lilei_1.user(id,name,create_date) values (?,?,?)"; int result = jdbc.update(sql, u.getId(),u.getName(),u.getCreateTime()); return result; } public int deleteUser(int id) { String sql = "delete from lilei_1.user where id=?"; int result = jdbc.update(sql, id); return result; } public List<User> queryUser(int pageSize, int pageNo) { String sql = "select id,name,create_date from lilei_1.user limit ? offset ?"; List<Map<String,Object>> list = jdbc.queryForList(sql, pageSize,((pageNo-1) * pageSize)); List<User> us = new ArrayList<User>(); for(Map<String,Object> m:list){ int id = Integer.parseInt(m.get("id").toString()); String name = m.get("name").toString(); Date createDate = (Date) m.get("create_date"); User u = new User(id,name); u.setCreateTime(createDate); us.add(u); } return us; } }
控制器
package com.lilei.spring_boot_db.boot.controller; import java.util.Date; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.lilei.spring_boot_db.boot.bean.User; import com.lilei.spring_boot_db.boot.dao.UserDao; @RestController @RequestMapping("/user") public class UserService { @Autowired protected UserDao userDao; @RequestMapping("insert") public String insertUser(String name, int id) { User u = new User(id, name); userDao.insertUser(u); return "success"; } @RequestMapping("delete") public String deleteUser(int id) { userDao.deleteUser(id); return "success"; } @RequestMapping("update") public String updateUser(String name,int id) { User u = new User(id,name); u.setCreateTime(new Date()); userDao.updateUser(u); return "success"; } @RequestMapping("query") public List<User> query(int page_size, int page_no) { return userDao.queryUser(page_size, page_no); } }