spring-boot留言板
spring-boot留言板
概要
目的
1 使用spring boot写个留言板2 熟悉spring boot的基本使用
详情
使用spring-boot 开发项目的前提
1 简单了解java语言
2 使用maven
一个spring-boot新手对spring-boot的理解
1 spring-boot提供了强大的功能,只需要使用maven加载依赖,很方便的开发各种应用,它集成了springmvc和spring,也可以很方便的加入orm软件
2 spring-boot 的bean都是实例化,实例化后通过spring管理
3 需要的时候可以通过依赖注入直接导入
4 常用的工具基本都已经通过框架集成,程序员只需要关注业务逻辑即可。
5 无现在还不会看dump日志,也你不太了解spring-boot和java虚拟机的运行机制,如果哪天突然挂掉,查找和定位问题会比较麻烦
项目的结构
mainjavaresources
testjava
我的pomx依赖包的说明
最前面依赖 spring boot的配置<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent>后面第依赖模块里面加上这些模块,模块我已经添加了注释<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><!--不使用默认的日志记录器--><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></exclusion></exclusions></dependency><!--测试框架--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--web模块--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--log4j2 记录日志--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-log4j2</artifactId></dependency><!--thymeleaf 模板功能--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!--引入MySQL连接的依赖包--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.21</version></dependency><!--引入jdbc支持--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!--注解自动处理bean--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.16.10</version></dependency><!--数据jpa的支持--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- 生成标准配置文档--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.2.2</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.2.2</version></dependency><!-- redis 的使用--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-redis</artifactId></dependency><!--spring boot的actuator监控--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>
入口的main文件
1 加入注解SpringBootApplication,指定类为应用启动类
2 ServletComponentScan 控制的过滤器和监听器
@SpringBootApplication
@ServletComponentScan
public class Application {public static void main(String[] args){SpringApplication springApplication = new SpringApplication(Application.class);springApplication.addListeners();springApplication.run(args);}
}
类文件的设计和简单说明
@RestController rest配置
@Autowired 自动加载@RequestMapping 直接定义了rest映射,是不是很方便
ModelAndView 定义模板类简单说就是从数据库取数据通过模板显示数据
@RestController
public class MsgBoardController extends SuperController{@Autowiredprotected HttpServletRequest request;@AutowiredJdbcMsgboardService jdbcMsgboardService;@RequestMapping(value = "/msgboard/add", method = RequestMethod.GET)public ModelAndView edit() throws Exception{ModelAndView modelAndView = new ModelAndView("msgboard/add");modelAndView.addObject("title","添加留言");return modelAndView;}@ApiOperation(value="创建留言", notes="根据用户名字,消息创建留言")@RequestMapping(value = "/msgboard/add", method = RequestMethod.POST)public void add(HttpServletResponse rsp) throws Exception{String name = request.getParameter("name");String msg = request.getParameter("msg");int retId = jdbcMsgboardService.createMsgboard(name, msg);if(retId > 0){rsp.sendRedirect("/msgboard/list");}else{rsp.sendRedirect("/msgboard/add");}}@RequestMapping(value = "/msgboard/list", method = RequestMethod.GET)public ModelAndView list() throws Exception{ModelAndView modelAndView = new ModelAndView("msgboard/list");int page = 1;String pageStr = request.getParameter("page");if(pageStr != null){page = Integer.parseInt(pageStr);}int pageSize = 20;String pageSizeStr = request.getParameter("pagesize");if(pageSizeStr != null){pageSize = Integer.parseInt(pageSizeStr);}List<Msgboard> list = jdbcMsgboardService.findByPage(page, pageSize);modelAndView.addObject("msgs",list);int prePage = page -1;if(prePage <= 0) prePage = 0;modelAndView.addObject("prePage", prePage);int nextPage = page + 1;if(list.size() < pageSize){nextPage = 0;}modelAndView.addObject("lastPage", nextPage);return modelAndView;}@RequestMapping(value = "/msgboard/del", method = RequestMethod.GET)public void del(HttpServletResponse rsp)throws Exception{String idStr = request.getParameter("id");if(idStr != null){int id = Integer.parseInt(idStr);jdbcMsgboardService.deleteMsgboardById(id);}rsp.sendRedirect("/msgboard/list");}
}
直接注入配置的数据
@Value("${msgboard.test}")
private String localData;
有两种方式
1 使用spring定义的jdbctemplate
2 使用jpa
使用jpa
1 配置代码
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.jpa.properties.hibernate.hbm2ddl.auto=update
2 实际代码jpa写好了常用的sql,我们只需要直接复用即可
public interface MsgboardJPARepository extends JpaRepository<MsgboardJPA, Long>{@Query(value="select* from msgboard order by mtime desc limit :start, :pagesize" ,nativeQuery=true)List<MsgboardJPA> findByPage(@Param("start") int start, @Param("pagesize") int pagesize);}
使用redis也很容易
1 配置redis
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
2 如果需要存储类,类需要可序列号,如果单独存储string不需要
@Configuration
public class RedisConfig extends CachingConfigurerSupport{@BeanJedisConnectionFactory jedisConnectionFactory(){return new JedisConnectionFactory();}@Beanpublic RedisTemplate<String, Msgboard> redisTemplate(RedisConnectionFactory redisConnectionFactory){RedisTemplate<String, Msgboard> template = new RedisTemplate<String, Msgboard>();template.setConnectionFactory(jedisConnectionFactory());template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new RedisObjectSerializer());return template;}}
项目详细说明
项目的github地址
spring-boot留言板相关推荐
- spring boot校园二手销售网站 毕业设计源码161417
目 录 摘要 1 1 绪论 1 1.1 研究背景 1 1.2国内外研究现状 1 1.3论文结构与章节安排 1 2开发工具及相关技术介绍 技术介绍 3 2.1 MVVM模式介绍 3 2.2B/S体系 ...
- spring boot清远旅游推荐网站的开发毕业设计-附源码211551
摘 要 清远自然生态环境特别优越,以水域风光.园林景观.地文景观﹑气候景观﹑生物景观.建筑景观.遗址遗迹.人文活动,旅游特产为主.旅游冬有温泉﹐夏有漂流﹐并拥有喀斯特地貌特有的地下溶洞及景观,形成了清 ...
- (附源码)spring boot基于微信小程序的口腔诊所预约系统 毕业设计 201738
小程序springboot口腔诊所预约系统 摘 要 随着我国经济迅速发展,人们对手机的需求越来越大,各种手机软件也都在被广泛应用,但是对于手机进行数据信息管理,对于手机的各种软件也是备受用户的喜爱, ...
- 【java毕业设计】基于Spring Boot+mysql的线上教学平台系统设计与实现(程序源码)-线上教学平台
基于Spring Boot+mysql的线上教学平台系统设计与实现(程序源码+毕业论文) 大家好,今天给大家介绍基于Spring Boot+mysql的线上教学平台系统设计与实现,本论文只截取部分文章 ...
- spring boot小说网站系统毕业设计源码041446
Springboot小说网站系统的设计与实现 摘 要 大数据时代下,数据呈爆炸式地增长.为了迎合信息化时代的潮流和信息化安全的要求,利用互联网服务于其他行业,促进生产,已经是成为一种势不可挡的趋势.在 ...
- (附源码)spring boot火车票订票系统 毕业设计171538
火车票订票系统的设计与实现 摘 要 信息化社会内需要与之针对性的信息获取途径,但是途径的扩展基本上为人们所努力的方向,由于站在的角度存在偏差,人们经常能够获得不同类型信息,这也是技术最为难以攻克的课题 ...
- spring boot火车票订票系统 毕业设计-附源码171538
火车票订票系统的设计与实现 摘 要 信息化社会内需要与之针对性的信息获取途径,但是途径的扩展基本上为人们所努力的方向,由于站在的角度存在偏差,人们经常能够获得不同类型信息,这也是技术最为难以攻克的课题 ...
- spring boot校园二手销售网站 毕业设计-附源码161417
目 录 摘要 1 1 绪论 1 1.1 研究背景 1 1.2国内外研究现状 1 1.3论文结构与章节安排 1 2开发工具及相关技术介绍 技术介绍 3 2.1 MVVM模式介绍 3 2.2B/S体系 ...
- (附源码)spring boot校园二手销售网站 毕业设计 161417
目 录 摘要 1 绪论 1.1 研究背景 1.2国内外研究现状 1.3论文结构与章节安排 2开发工具及相关技术介绍 技术介绍 2.1 MVVM模式介绍 2.2B/S体系工作原理 4 2.3spri ...
- (附源码)spring boot校园二手销售网站 附源码161417
目 录 摘要 1 1 绪论 1 1.1 研究背景 1 1.2国内外研究现状 1 1.3论文结构与章节安排 1 2开发工具及相关技术介绍 技术介绍 3 2.1 MVVM模式介绍 3 2.2 B/S体系工 ...
最新文章
- linux中exit()和 _exit()说明
- git常用命令,分支操作,子模块
- Java的基础方法Java的对象_java基础之 创建对象的几种方式
- Hoogle之装饰模式设计手机(下)
- DM9000调试记录
- java将图片转byte存入数据库_Java将byte[]转图片存储到本地的案例
- 互联网日报 | 5月3日 星期一 | 京东物流通过港交所上市聆讯;理想汽车累计交付破5万辆;拼多多年活跃商户达860万
- 转载一篇阅读文章(还算不错吧)
- 露天影院网站_为什么要在露天工作?
- C语言汇编pdf,c语言程序代码[汇编].pdf
- Codeforces Round #375 (Div. 2) F. st-Spanning Tree 生成树
- 《Kotlin进化之路》之【第二章:揭开Kotlin的基础面纱】(二)
- [转载] Java Challengers#1:JVM中的方法重载
- php中的上传全局变量 把全局变量的数组形式变得更简易
- 重庆ETC学员“食神大赛”
- 怎样查找计算机的ip mac地址,如何通过mac地址查ip,教您Mac怎么查看ip地址
- 那些黑天鹅教会我们的IT知识
- js 移动号码,座机号码,座机转分机号码验证
- trainning 2017-11-21
- 百度云曲显平:AIOps时代下如何用运维数据系统性地解决运维问题?
热门文章
- Ubutu16.04 环境下添加打印机,安装Brother兄弟打印机驱动
- 高级程序员的应用技巧之:HTML 速查列表的使用
- 如何将ManjaroLinux装进移动硬盘
- Android判断定位功能是否可用
- Vue+Node:商品列表的分页、排序、筛选,添加购物车
- MOTO ME525 港版水货GPRS设置
- OSS上传图片并获取相关链接
- 时代天使将在香港上市:吸金强悍、隐忧浮现,“C位”已被抢走
- 同是“211”高校 区别却这么大!
- 论文阅读(2)Classification of pit and fissure for caries risk based on 3D surface morphology analysis